Skip to content

Commit

Permalink
save delta
Browse files Browse the repository at this point in the history
  • Loading branch information
neSpecc committed Jan 22, 2025
1 parent 209f052 commit 66047f0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/utils/unsafeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export const unsafeFields = ['context', 'addons'] as const;
export function decodeUnsafeFields(event: GroupedEventDBScheme | RepetitionDBScheme): void {
unsafeFields.forEach((field) => {
try {
const fieldValue = event.payload[field];
const fieldValue = event.delta[field];

if (typeof fieldValue === 'string') {
event.payload[field] = JSON.parse(fieldValue);
event.delta[field] = JSON.parse(fieldValue);
}
} catch {
console.error(`Failed to parse field ${field} in event ${event._id}`);
Expand All @@ -32,7 +32,7 @@ export function decodeUnsafeFields(event: GroupedEventDBScheme | RepetitionDBSch
*/
export function encodeUnsafeFields(event: GroupedEventDBScheme | RepetitionDBScheme): void {
unsafeFields.forEach((field) => {
const fieldValue = event.payload[field];
const fieldValue = event.delta[field];

/**
* Repetition diff can omit these fields if they are not changed
Expand All @@ -52,6 +52,6 @@ export function encodeUnsafeFields(event: GroupedEventDBScheme | RepetitionDBSch
console.error(`Failed to stringify field ${field} in event ${event._id}`);
newValue = undefined;
}
event.payload[field] = newValue;
event.delta[field] = newValue;
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"test:release": "jest workers/release",
"test:slack": "jest workers/slack",
"test:limiter": "jest workers/limiter",
"test:grouper": "jest workers/grouper --spec ",
"test:grouper": "jest workers/grouper",
"test:diff": "jest ./workers/grouper/tests/diff.test.ts",
"test:paymaster": "jest workers/paymaster",
"test:notifier": "jest workers/notifier",
Expand Down
9 changes: 6 additions & 3 deletions workers/grouper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,22 @@ export default class GrouperWorker extends Worker {
*
*/

// console.log('diff new', diff);
console.log('old diff', diff);
console.log('new diff', delta);
} catch (e) {
console.error(e);
throw new DiffCalculationError(e, existedEvent.payload, task.event);
}

const newRepetition = {
groupHash: uniqueEventHash,
payload: diff,
// payload: diff,
delta,
// timestamp: task.event.timestamp
/**
* Temporarily store the whole repetition for diff/merge debugging
*/
__raw: JSON.stringify(task.event),
// __raw: JSON.stringify(task.event),
} as RepetitionDBScheme;


Expand Down
7 changes: 7 additions & 0 deletions workers/grouper/types/group-worker-task.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EventDataAccepted, EventAddons } from '@hawk.so/types';
import { WorkerTask } from '../../../lib/types/worker-task';
import type { Delta } from "@n1ru4l/json-patch-plus";


/**
* Language-workers adds tasks for Group Worker in this format.
Expand All @@ -21,3 +23,8 @@ export interface GroupWorkerTask extends WorkerTask {
*/
event: EventDataAccepted<EventAddons>;
}

/**
* Delta of the original event and the repetition
*/
export type RepetitionDelta = Delta;
1 change: 1 addition & 0 deletions workers/limiter/src/test.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"query":"\n query fetchWorkspaces($ids: [ID!]) {\n workspaces(ids: $ids) {\n id\n name\n description\n image\n subscriptionId\n lastChargeDate\n inviteHash\n billingPeriodEventsCount\n ...WorkspacePlan\n ...WorkspaceWithTeam\n }\n }\n\n \n fragment WorkspaceWithTeam on Workspace {\n team {\n __typename\n ...on ConfirmedMember {\n id\n user {\n id\n email\n image\n }\n isAdmin\n }\n\n ...on PendingMember {\n id\n email\n }\n }\n }\n\n \n fragment WorkspacePlan on Workspace {\n plan {\n id\n name\n monthlyCharge\n monthlyChargeCurrency\n eventsLimit\n }\n }\n\n","variables":{"ids":["5e5581b63e3a9dbfa833738a"]}}
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2489,7 +2489,7 @@ delegates@^1.0.0:
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=

denque@^1.4.1:
denque@^1.4.1, denque@^1.5.0:
version "1.5.1"
resolved "https://registry.yarnpkg.com/denque/-/denque-1.5.1.tgz#07f670e29c9a78f8faecb2566a1e2c11929c5cbf"
integrity sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==
Expand Down Expand Up @@ -6462,6 +6462,33 @@ realpath-native@^2.0.0:
resolved "https://registry.yarnpkg.com/realpath-native/-/realpath-native-2.0.0.tgz#7377ac429b6e1fd599dc38d08ed942d0d7beb866"
integrity sha512-v1SEYUOXXdbBZK8ZuNgO4TBjamPsiSgcFr0aP+tEKpQZK8vooEUqV6nm6Cv502mX4NF2EfsnVqtNAHG+/6Ur1Q==

redis-commands@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.7.0.tgz#15a6fea2d58281e27b1cd1acfb4b293e278c3a89"
integrity sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==

redis-errors@^1.0.0, redis-errors@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad"
integrity sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==

redis-parser@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4"
integrity sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==
dependencies:
redis-errors "^1.0.0"

redis@^3.1.1:
version "3.1.2"
resolved "https://registry.yarnpkg.com/redis/-/redis-3.1.2.tgz#766851117e80653d23e0ed536254677ab647638c"
integrity sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==
dependencies:
denque "^1.5.0"
redis-commands "^1.7.0"
redis-errors "^1.2.0"
redis-parser "^3.0.0"

redis@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/redis/-/redis-4.7.0.tgz#b401787514d25dd0cfc22406d767937ba3be55d6"
Expand Down

0 comments on commit 66047f0

Please sign in to comment.