Skip to content

Commit 986cd37

Browse files
committed
fix(): types fix
1 parent c3d2145 commit 986cd37

12 files changed

Lines changed: 192 additions & 157 deletions

File tree

lib/event-worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Worker } from './worker';
22
import * as WorkerNames from './workerNames';
33
import { GroupWorkerTask } from 'hawk-worker-grouper/types/group-worker-task';
4-
import { CatcherMessageType, CatcherMessagePayload, CatcherMessageAccepted, ErrorsCatcherType } from '@hawk.so/types'
4+
import { CatcherMessageType, CatcherMessagePayload, CatcherMessageAccepted, ErrorsCatcherType } from '@hawk.so/types';
55

66
/**
77
* Defines a Worker that handles events from Catcher.
@@ -26,14 +26,14 @@ export abstract class EventWorker extends Worker {
2626
projectId: task.projectId,
2727
catcherType: this.type as CatcherMessageType,
2828
payload: task.payload as CatcherMessagePayload<ErrorsCatcherType>,
29-
timestamp: task.timestamp
29+
timestamp: task.timestamp,
3030
} as GroupWorkerTask<ErrorsCatcherType>);
3131
}
3232

3333
/**
3434
* Validate passed event data
3535
*
36-
* @param {EventWorkerTask} task - task to be validated
36+
* @param {CatcherMessageAccepted<CatcherMessageType>} task - task to be validated
3737
*/
3838
protected validate(task: CatcherMessageAccepted<CatcherMessageType>): void {
3939
if (!task.projectId || !task.payload || !task.timestamp) {

migrations/20250707160120-move-timestamp-to-event-level.js

Lines changed: 108 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,124 +4,123 @@
44
*/
55

66
module.exports = {
7-
async up(db) {
8-
const collections = await db.listCollections({}, {
9-
authorizedCollections: true,
10-
nameOnly: true,
11-
}).toArray();
12-
13-
// Separate collections by prefix
14-
const eventCollections = collections
15-
.filter(col => /^events:/.test(col.name))
16-
.map(col => col.name);
17-
18-
const repetitionCollections = collections
19-
.filter(col => /^repetitions:/.test(col.name))
20-
.map(col => col.name);
21-
22-
// Step 1: Process event collections
23-
for (const collectionName of eventCollections) {
24-
await db.collection(collectionName).updateMany(
25-
{ 'payload.timestamp': { $exists: true } },
26-
[
27-
{
28-
$set: {
29-
timestamp: '$payload.timestamp',
30-
},
7+
async up(db) {
8+
const collections = await db.listCollections({}, {
9+
authorizedCollections: true,
10+
nameOnly: true,
11+
}).toArray();
12+
13+
// Separate collections by prefix
14+
const eventCollections = collections
15+
.filter(col => /^events:/.test(col.name))
16+
.map(col => col.name);
17+
18+
const repetitionCollections = collections
19+
.filter(col => /^repetitions:/.test(col.name))
20+
.map(col => col.name);
21+
22+
// Step 1: Process event collections
23+
for (const collectionName of eventCollections) {
24+
await db.collection(collectionName).updateMany(
25+
{ 'payload.timestamp': { $exists: true } },
26+
[
27+
{
28+
$set: {
29+
timestamp: '$payload.timestamp',
3130
},
31+
},
32+
{
33+
$unset: 'payload.timestamp',
34+
},
35+
]
36+
);
37+
}
38+
39+
// Step 2: Process repetition collections
40+
for (const collectionName of repetitionCollections) {
41+
const collection = db.collection(collectionName);
42+
const cursor = collection.find({});
43+
44+
while (await cursor.hasNext()) {
45+
const doc = await cursor.next();
46+
47+
if (doc.payload?.timestamp) {
48+
// Move timestamp from payload to root
49+
await collection.updateOne(
50+
{ _id: doc._id },
3251
{
33-
$unset: 'payload.timestamp',
34-
},
35-
]
36-
);
37-
}
38-
39-
// Step 2: Process repetition collections
40-
for (const collectionName of repetitionCollections) {
41-
const collection = db.collection(collectionName);
42-
const cursor = collection.find({});
43-
44-
while (await cursor.hasNext()) {
45-
const doc = await cursor.next();
46-
47-
if (doc.payload?.timestamp) {
48-
// Move timestamp from payload to root
52+
$set: { timestamp: doc.payload.timestamp },
53+
$unset: { 'payload.timestamp': '' },
54+
}
55+
);
56+
} else if (doc.groupHash) {
57+
// Attempt to find matching event
58+
let eventDoc = null;
59+
60+
const projectId = collectionName.split(':')[1];
61+
const eventsCollectionName = `events:${projectId}`;
62+
63+
eventDoc = await db.collection(eventsCollectionName).findOne({ groupHash: doc.groupHash });
64+
65+
if (eventDoc?.timestamp) {
4966
await collection.updateOne(
5067
{ _id: doc._id },
5168
{
52-
$set: { timestamp: doc.payload.timestamp },
53-
$unset: { 'payload.timestamp': '' },
69+
$set: { timestamp: eventDoc.timestamp },
5470
}
5571
);
56-
} else if (doc.groupHash) {
57-
// Attempt to find matching event
58-
let eventDoc = null;
59-
60-
const projectId = collectionName.split(":")[1];
61-
const eventsCollectionName = `events:${projectId}`;
62-
63-
eventDoc = await db.collection(eventsCollectionName).findOne({ groupHash: doc.groupHash });
64-
65-
if (eventDoc?.timestamp) {
66-
await collection.updateOne(
67-
{ _id: doc._id },
68-
{
69-
$set: { timestamp: eventDoc.timestamp },
70-
}
71-
);
72-
}
7372
}
7473
}
7574
}
76-
},
77-
78-
async down(db) {
79-
const collections = await db.listCollections({}, {
80-
authorizedCollections: true,
81-
nameOnly: true,
82-
}).toArray();
83-
84-
const eventCollections = collections
85-
.filter(col => /^events:/.test(col.name))
86-
.map(col => col.name);
87-
88-
const repetitionCollections = collections
89-
.filter(col => /^repetitions:/.test(col.name))
90-
.map(col => col.name);
91-
92-
// Revert event collections
93-
for (const collectionName of eventCollections) {
94-
await db.collection(collectionName).updateMany(
95-
{ timestamp: { $exists: true } },
96-
[
97-
{
98-
$set: {
99-
'payload.timestamp': '$timestamp',
100-
},
101-
},
102-
{
103-
$unset: 'timestamp',
104-
},
105-
]
106-
);
107-
}
108-
109-
// Revert repetition collections
110-
for (const collectionName of repetitionCollections) {
111-
await db.collection(collectionName).updateMany(
112-
{ timestamp: { $exists: true } },
113-
[
114-
{
115-
$set: {
116-
'payload.timestamp': '$timestamp',
117-
},
75+
}
76+
},
77+
78+
async down(db) {
79+
const collections = await db.listCollections({}, {
80+
authorizedCollections: true,
81+
nameOnly: true,
82+
}).toArray();
83+
84+
const eventCollections = collections
85+
.filter(col => /^events:/.test(col.name))
86+
.map(col => col.name);
87+
88+
const repetitionCollections = collections
89+
.filter(col => /^repetitions:/.test(col.name))
90+
.map(col => col.name);
91+
92+
// Revert event collections
93+
for (const collectionName of eventCollections) {
94+
await db.collection(collectionName).updateMany(
95+
{ timestamp: { $exists: true } },
96+
[
97+
{
98+
$set: {
99+
'payload.timestamp': '$timestamp',
118100
},
119-
{
120-
$unset: 'timestamp',
101+
},
102+
{
103+
$unset: 'timestamp',
104+
},
105+
]
106+
);
107+
}
108+
109+
// Revert repetition collections
110+
for (const collectionName of repetitionCollections) {
111+
await db.collection(collectionName).updateMany(
112+
{ timestamp: { $exists: true } },
113+
[
114+
{
115+
$set: {
116+
'payload.timestamp': '$timestamp',
121117
},
122-
]
123-
);
124-
}
125-
},
126-
};
127-
118+
},
119+
{
120+
$unset: 'timestamp',
121+
},
122+
]
123+
);
124+
}
125+
},
126+
};

workers/archiver/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export default class ArchiverWorker extends Worker {
168168
const repetitionsBulk = repetitionsCollection.initializeUnorderedBulkOp();
169169

170170
repetitionsBulk.find({
171-
'timestamp': {
171+
timestamp: {
172172
$lt: maxOldTimestamp,
173173
},
174174
}).delete();

workers/archiver/tests/repetitions.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const mockedRepetitions = [
1212
},
1313
{
1414
groupHash: '10a964aeb1ca8daa7a041c249b1c4d9cc351afe86ee7e13c5aa441c42c3f9b12',
15-
timestamp: 1586892999,
15+
timestamp: 1586892999,
1616
},
1717
{
1818
groupHash: '10a964aeb1ca8daa7a041c249b1c4d9cc351afe86ee7e13c5aa441c42c3f9b12',

workers/default/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CatcherMessageType } from '@hawk.so/types';
1+
import { CatcherMessageType, ErrorsCatcherType } from '@hawk.so/types';
22
import { EventWorker } from '../../../lib/event-worker';
33
import * as pkg from '../package.json';
44
import { DefaultEventWorkerTask } from '../types/default-event-worker-task';
@@ -10,7 +10,7 @@ export default class DefaultEventWorker extends EventWorker {
1010
/**
1111
* Worker type (will pull tasks from Registry queue with the same name)
1212
*/
13-
public type: CatcherMessageType = pkg.workerType as CatcherMessageType;
13+
public type: ErrorsCatcherType = pkg.workerType as ErrorsCatcherType;
1414

1515
/**
1616
* Message handle function

workers/grouper/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Worker } from '../../../lib/worker';
66
import * as WorkerNames from '../../../lib/workerNames';
77
import * as pkg from '../package.json';
88
import type { GroupWorkerTask, RepetitionDelta } from '../types/group-worker-task';
9-
import type {
9+
import type {
1010
EventAddons,
1111
EventData,
1212
GroupedEventDBScheme,
@@ -272,8 +272,8 @@ export default class GrouperWorker extends Worker {
272272
* Get unique hash based on event type and title
273273
*
274274
* @param task - worker task to create hash
275-
*/
276-
private getUniqueEventHash<type extends ErrorsCatcherType>(task: GroupWorkerTask<type>): Promise<string> {
275+
*/
276+
private getUniqueEventHash<Type extends ErrorsCatcherType>(task: GroupWorkerTask<Type>): Promise<string> {
277277
return this.cache.get(`groupHash:${task.projectId}:${task.catcherType}:${task.payload.title}`, () => {
278278
return crypto.createHmac('sha256', process.env.EVENT_SECRET)
279279
.update(task.catcherType + task.payload.title)
@@ -428,7 +428,7 @@ export default class GrouperWorker extends Worker {
428428
* @param existedEvent - original event to get its user
429429
* @returns {[boolean, boolean]} - whether to increment affected users for the repetition and the daily aggregation
430430
*/
431-
private async shouldIncrementAffectedUsers<type extends ErrorsCatcherType>(task: GroupWorkerTask<type>, existedEvent: GroupedEventDBScheme): Promise<[boolean, boolean]> {
431+
private async shouldIncrementAffectedUsers<Type extends ErrorsCatcherType>(task: GroupWorkerTask<Type>, existedEvent: GroupedEventDBScheme): Promise<[boolean, boolean]> {
432432
const eventUser = task.payload.user;
433433

434434
/**
@@ -498,7 +498,7 @@ export default class GrouperWorker extends Worker {
498498
.findOne({
499499
groupHash: existedEvent.groupHash,
500500
'payload.user.id': eventUser.id,
501-
'timestamp': {
501+
timestamp: {
502502
$gte: eventMidnight,
503503
$lt: eventNextMidnight,
504504
},

0 commit comments

Comments
 (0)