Skip to content

Commit b9aeb41

Browse files
authored
imp(paymaster): add paid until field for workspace (#400)
* feat(paymaster): support paid until workspace field * chore(paymaster): update types and test * chore(): lint fix * chore(): lint fix * imp(CI): update ubuntu version * test(paymaster): add new testcase * chore(paymaster): lint fix
1 parent 8fb660d commit b9aeb41

15 files changed

Lines changed: 207 additions & 86 deletions

File tree

.github/workflows/build-and-push-docker-image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ env:
77

88
jobs:
99
build:
10-
runs-on: ubuntu-20.04
10+
runs-on: ubuntu-22.04
1111
if: endsWith(github.ref, '/prod') || endsWith(github.ref, '/stage') || endsWith(github.ref, '/migration')
1212
steps:
1313
- name: Checkout repository

.github/workflows/check-build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
build:
10-
runs-on: ubuntu-20.04
10+
runs-on: ubuntu-22.04
1111
steps:
1212
- name: Checkout repository
1313
uses: actions/checkout@v2

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on: [push]
55
jobs:
66
lint:
77
name: Unit testing
8-
runs-on: ubuntu-20.04
8+
runs-on: ubuntu-22.04
99
steps:
1010
- uses: actions/checkout@v2
1111
- name: Use Node.js 16.x

.github/workflows/update-monorepository.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ env:
77
REPOSITORY_MONO_PATH: workers
88
jobs:
99
build:
10-
runs-on: ubuntu-20.04
10+
runs-on: ubuntu-22.04
1111
steps:
1212
- name: Mono repository update
1313
uses: peter-evans/repository-dispatch@v1

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ module.exports = {
2727

2828
setupFiles: [ './jest.setup.js' ],
2929

30-
setupFilesAfterEnv: [ './jest.setup.redis-mock.js', './jest.setup.mongo-repl-set.js' ],
30+
setupFilesAfterEnv: ['./jest.setup.redis-mock.js', './jest.setup.mongo-repl-set.js'],
3131
};

jest.setup.mongo-repl-set.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let admin;
44
let connection;
55

66
beforeAll(async () => {
7-
connection = await MongoClient.connect("mongodb://127.0.0.1:55010/hawk?", {
7+
connection = await MongoClient.connect('mongodb://127.0.0.1:55010/hawk?', {
88
useNewUrlParser: true,
99
useUnifiedTopology: true,
1010
});
@@ -13,11 +13,12 @@ beforeAll(async () => {
1313

1414
try {
1515
let status = await admin.command({ replSetGetStatus: 1 }).catch(() => null);
16+
1617
if (status && status.ok) {
17-
console.log("✅ Replica set already initialized");
18+
console.log('✅ Replica set already initialized');
1819
} else {
1920
await admin.command({ replSetInitiate: {} });
20-
console.log("✅ Replica set initiated");
21+
console.log('✅ Replica set initiated');
2122
}
2223

2324
const startTime = Date.now();
@@ -30,15 +31,15 @@ beforeAll(async () => {
3031
await new Promise(resolve => setTimeout(resolve, 1000));
3132
status = await admin.command({ replSetGetStatus: 1 });
3233

33-
const primary = status.members.find(member => member.stateStr === "PRIMARY");
34-
const secondary = status.members.find(member => member.stateStr === "SECONDARY");
34+
const primary = status.members.find(member => member.stateStr === 'PRIMARY');
35+
const secondary = status.members.find(member => member.stateStr === 'SECONDARY');
3536

36-
if (primary && secondary) break;
37+
if (primary && secondary) {
38+
break;
39+
}
3740
} while (Date.now() - startTime < timeout);
3841

39-
40-
console.log("✅ Replica set is stable");
41-
42+
console.log('✅ Replica set is stable');
4243
} catch (err) {
4344
console.error('❌ Failed to initiate replica set:', err);
4445
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"dependencies": {
4949
"@hawk.so/nodejs": "^3.1.1",
50-
"@hawk.so/types": "^0.1.28",
50+
"@hawk.so/types": "^0.1.29",
5151
"@types/amqplib": "^0.8.2",
5252
"@types/jest": "^29.2.3",
5353
"@types/mongodb": "^3.5.15",

workers/javascript/src/index.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export default class JavascriptEventWorker extends EventWorker {
228228
* Fixes bug: https://github.com/codex-team/hawk.workers/issues/121
229229
*/
230230
if (originalLocation.source) {
231-
console.log('original location source found')
231+
console.log('original location source found');
232232
/**
233233
* Get 5 lines above and 5 below
234234
*/
@@ -247,40 +247,43 @@ export default class JavascriptEventWorker extends EventWorker {
247247
sourceCode: lines,
248248
}) as BacktraceFrame;
249249
}
250-
250+
251251
/**
252252
* Method that is used to parse full function context of the code position
253+
*
253254
* @param sourceCode - content of the source file
254255
* @param line - number of the line from the stack trace
255256
* @returns - string of the function context or null if it could not be parsed
256257
*/
257258
private getFunctionContext(sourceCode: string, line: number): string | null {
258259
let functionName: string | null = null;
259260
let className: string | null = null;
260-
let isAsync: boolean = false;
261+
let isAsync = false;
261262

262263
try {
263264
const ast = parse(sourceCode, {
264-
sourceType: "module",
265+
sourceType: 'module',
265266
plugins: [
266-
"typescript",
267-
"jsx",
268-
"classProperties",
269-
"decorators",
270-
"optionalChaining",
271-
"nullishCoalescingOperator",
272-
"dynamicImport",
273-
"bigInt",
274-
"topLevelAwait"
275-
]
267+
'typescript',
268+
'jsx',
269+
'classProperties',
270+
'decorators',
271+
'optionalChaining',
272+
'nullishCoalescingOperator',
273+
'dynamicImport',
274+
'bigInt',
275+
'topLevelAwait',
276+
],
276277
});
277278

278279
traverse(ast as any, {
279280
/**
280281
* It is used to get class decorator of the position, it will save class that is related to original position
282+
*
283+
* @param path
281284
*/
282285
ClassDeclaration(path) {
283-
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`)
286+
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);
284287

285288
if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
286289
className = path.node.id.name || null;
@@ -289,9 +292,11 @@ export default class JavascriptEventWorker extends EventWorker {
289292
/**
290293
* It is used to get class and its method decorator of the position
291294
* It will save class and method, that are related to original position
295+
*
296+
* @param path
292297
*/
293298
ClassMethod(path) {
294-
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`)
299+
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);
295300

296301
if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
297302
// Handle different key types
@@ -303,24 +308,28 @@ export default class JavascriptEventWorker extends EventWorker {
303308
},
304309
/**
305310
* It is used to get function name that is declared out of class
311+
*
312+
* @param path
306313
*/
307314
FunctionDeclaration(path) {
308-
console.log(`function declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`)
309-
315+
console.log(`function declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);
316+
310317
if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
311318
functionName = path.node.id.name || null;
312319
isAsync = path.node.async;
313320
}
314321
},
315322
/**
316323
* It is used to get anonimous function names in function expressions or arrow function expressions
324+
*
325+
* @param path
317326
*/
318327
VariableDeclarator(path) {
319-
console.log(`variable declaration: node.type: ${path.node.init.type}, line: ${line}, `)
328+
console.log(`variable declaration: node.type: ${path.node.init.type}, line: ${line}, `);
320329

321330
if (
322331
path.node.init &&
323-
(path.node.init.type === "FunctionExpression" || path.node.init.type === "ArrowFunctionExpression") &&
332+
(path.node.init.type === 'FunctionExpression' || path.node.init.type === 'ArrowFunctionExpression') &&
324333
path.node.loc &&
325334
path.node.loc.start.line <= line &&
326335
path.node.loc.end.line >= line
@@ -331,14 +340,14 @@ export default class JavascriptEventWorker extends EventWorker {
331340
}
332341
isAsync = (path.node.init as any).async;
333342
}
334-
}
343+
},
335344
});
336345
} catch (e) {
337-
console.error(`Failed to parse source code: ${e.message}`);
346+
console.error(`Failed to parse source code: ${e.message}`);
338347
}
339348

340-
return functionName ? `${isAsync ? "async " : ""}${className ? `${className}.` : ""}${functionName}` : null;
341-
}
349+
return functionName ? `${isAsync ? 'async ' : ''}${className ? `${className}.` : ''}${functionName}` : null;
350+
}
342351

343352
/**
344353
* Downloads source map file from Grid FS

workers/notifier/tests/redisHelper.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,16 @@ describe('RedisHelper', () => {
103103
const thresholdPeriod = 2000; // 2 seconds in milliseconds
104104
const key = `${projectId}:${ruleId}:${groupHash}:${thresholdPeriod}:times`;
105105

106-
/**
107-
* Call computeEventCountForPeriod to set the key
108-
*/
109-
await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod);
106+
/**
107+
* Call computeEventCountForPeriod to set the key
108+
*/
109+
await redisHelper.computeEventCountForPeriod(projectId, ruleId, groupHash, thresholdPeriod);
110110

111111
/**
112112
* Verify, that key exists
113113
*/
114114
let value = await redisClient.hGet(key, 'eventsCount');
115+
115116
expect(value).not.toBeNull();
116117

117118
/**

workers/paymaster/src/index.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ export default class PaymasterWorker extends Worker {
6363
* Pay day is calculated by formula: last charge date + 30 days
6464
*
6565
* @param date - last charge date
66-
* @param isDebug
66+
* @param paidUntil - paid until date
67+
* @param isDebug - flag for debug purposes
6768
*/
68-
private static isTimeToPay(date: Date, isDebug = false): boolean {
69-
const expectedPayDay = new Date(date);
69+
private static isTimeToPay(date: Date, paidUntil: Date, isDebug = false): boolean {
70+
const expectedPayDay = paidUntil ? new Date(paidUntil) : new Date(date);
7071

7172
if (isDebug) {
7273
expectedPayDay.setDate(date.getDate() + 1);
73-
} else {
74+
} else if (!paidUntil) {
7475
expectedPayDay.setMonth(date.getMonth() + 1);
7576
}
7677

@@ -82,17 +83,18 @@ export default class PaymasterWorker extends Worker {
8283
/**
8384
* Returns difference between now and payday in days
8485
*
85-
* Pay day is calculated by formula: last charge date + 30 days
86+
* Pay day is calculated by formula: paidUntil date or last charge date + 1 month
8687
*
8788
* @param date - last charge date
88-
* @param isDebug
89+
* @param paidUntil - paid until date
90+
* @param isDebug - flag for debug purposes
8991
*/
90-
private static daysBeforePayday(date: Date, isDebug = false): number {
91-
const expectedPayDay = new Date(date);
92+
private static daysBeforePayday(date: Date, paidUntil: Date = null, isDebug = false): number {
93+
const expectedPayDay = paidUntil ? new Date(paidUntil) : new Date(date);
9294

9395
if (isDebug) {
9496
expectedPayDay.setDate(date.getDate() + 1);
95-
} else {
97+
} else if (!paidUntil) {
9698
expectedPayDay.setMonth(date.getMonth() + 1);
9799
}
98100

@@ -104,17 +106,18 @@ export default class PaymasterWorker extends Worker {
104106
/**
105107
* Returns difference between payday and now in days
106108
*
107-
* Pay day is calculated by formula: last charge date + 30 days
109+
* Pay day is calculated by formula: paidUntil date or last charge date + 1 month
108110
*
109111
* @param date - last charge date
110-
* @param isDebug
112+
* @param paidUntil - paid until date
113+
* @param isDebug - flag for debug purposes
111114
*/
112-
private static daysAfterPayday(date: Date, isDebug = false): number {
113-
const expectedPayDay = new Date(date);
115+
private static daysAfterPayday(date: Date, paidUntil: Date = null, isDebug = false): number {
116+
const expectedPayDay = paidUntil ? new Date(paidUntil) : new Date(date);
114117

115118
if (isDebug) {
116119
expectedPayDay.setDate(date.getDate() + 1);
117-
} else {
120+
} else if (!paidUntil) {
118121
expectedPayDay.setMonth(date.getMonth() + 1);
119122
}
120123

@@ -209,19 +212,19 @@ export default class PaymasterWorker extends Worker {
209212
* Is it time to pay
210213
*/
211214
// @ts-expect-error debug
212-
const isTimeToPay = PaymasterWorker.isTimeToPay(workspace.lastChargeDate, workspace.isDebug);
215+
const isTimeToPay = PaymasterWorker.isTimeToPay(workspace.lastChargeDate, workspace.paidUntil, workspace.isDebug);
213216

214217
/**
215218
* How many days have passed since payments the expected day of payments
216219
*/
217220
// @ts-expect-error debug
218-
const daysAfterPayday = PaymasterWorker.daysAfterPayday(workspace.lastChargeDate, workspace.isDebug);
221+
const daysAfterPayday = PaymasterWorker.daysAfterPayday(workspace.lastChargeDate, workspace.paidUntil, workspace.isDebug);
219222

220223
/**
221224
* How many days left for the expected day of payments
222225
*/
223226
// @ts-expect-error debug
224-
const daysLeft = PaymasterWorker.daysBeforePayday(workspace.lastChargeDate, workspace.isDebug);
227+
const daysLeft = PaymasterWorker.daysBeforePayday(workspace.lastChargeDate, workspace.paidUntil, workspace.isDebug);
225228

226229
/**
227230
* Do we need to ask for money

0 commit comments

Comments
 (0)