@@ -22,25 +22,25 @@ const ONE_WEEK_IN_MS = 7 * ONE_DAY_IN_MS
22
22
const ONE_MONTH_IN_MS = 30 * ONE_DAY_IN_MS
23
23
24
24
const TODAY_USER = {
25
- creation_ts : 1 ,
25
+ creation_ts : new Date ( ) . getTime ( ) / 1000 ,
26
26
last_seen_ts : new Date ( ) . getTime ( ) ,
27
27
name : 'user1'
28
28
}
29
29
30
30
const PRE_TODAY_USER = {
31
- creation_ts : 1 ,
31
+ creation_ts : ( new Date ( ) . getTime ( ) - ONE_DAY_IN_MS - 1 ) / 1000 ,
32
32
last_seen_ts : new Date ( ) . getTime ( ) - ONE_DAY_IN_MS - 1 ,
33
33
name : 'user2'
34
34
}
35
35
36
36
const PRE_WEEK_USER = {
37
- creation_ts : 1 ,
37
+ creation_ts : ( new Date ( ) . getTime ( ) - ONE_WEEK_IN_MS - 1 ) / 1000 ,
38
38
last_seen_ts : new Date ( ) . getTime ( ) - ONE_WEEK_IN_MS - 1 ,
39
39
name : 'user3'
40
40
}
41
41
42
42
const PRE_MONTH_USER = {
43
- creation_ts : 1 ,
43
+ creation_ts : ( new Date ( ) . getTime ( ) - ONE_MONTH_IN_MS - 1 ) / 1000 ,
44
44
last_seen_ts : new Date ( ) . getTime ( ) - ONE_MONTH_IN_MS - 1 ,
45
45
name : 'user4'
46
46
}
@@ -103,11 +103,21 @@ describe('the Metrics API Service', () => {
103
103
const result = await metricsService . getUserActivityStats ( )
104
104
105
105
expect ( result ) . toEqual ( {
106
- dailyActiveUsers : [ TODAY_USER ] ,
107
- weeklyActiveUsers : [ TODAY_USER ] ,
108
- monthlyActiveUsers : [ TODAY_USER ] ,
109
- weeklyNewUsers : [ ] ,
110
- monthlyNewUsers : [ ]
106
+ dailyActiveUsers : [
107
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
108
+ ] ,
109
+ weeklyActiveUsers : [
110
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
111
+ ] ,
112
+ monthlyActiveUsers : [
113
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
114
+ ] ,
115
+ weeklyNewUsers : [
116
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
117
+ ] ,
118
+ monthlyNewUsers : [
119
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
120
+ ]
111
121
} )
112
122
} )
113
123
@@ -155,11 +165,25 @@ describe('the Metrics API Service', () => {
155
165
const result = await metricsService . getUserActivityStats ( )
156
166
157
167
expect ( result ) . toEqual ( {
158
- dailyActiveUsers : [ TODAY_USER ] ,
159
- weeklyActiveUsers : [ TODAY_USER , PRE_TODAY_USER ] ,
160
- monthlyActiveUsers : [ TODAY_USER , PRE_TODAY_USER ] ,
161
- weeklyNewUsers : [ ] ,
162
- monthlyNewUsers : [ ]
168
+ dailyActiveUsers : [
169
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
170
+ ] ,
171
+ weeklyActiveUsers : [
172
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
173
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
174
+ ] ,
175
+ monthlyActiveUsers : [
176
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
177
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
178
+ ] ,
179
+ weeklyNewUsers : [
180
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
181
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
182
+ ] ,
183
+ monthlyNewUsers : [
184
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
185
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
186
+ ]
163
187
} )
164
188
} )
165
189
@@ -205,11 +229,27 @@ describe('the Metrics API Service', () => {
205
229
const result = await metricsService . getUserActivityStats ( )
206
230
207
231
expect ( result ) . toEqual ( {
208
- dailyActiveUsers : [ TODAY_USER ] ,
209
- weeklyActiveUsers : [ TODAY_USER , PRE_TODAY_USER ] ,
210
- monthlyActiveUsers : [ TODAY_USER , PRE_TODAY_USER , PRE_WEEK_USER ] ,
211
- weeklyNewUsers : [ ] ,
212
- monthlyNewUsers : [ ]
232
+ dailyActiveUsers : [
233
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
234
+ ] ,
235
+ weeklyActiveUsers : [
236
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
237
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
238
+ ] ,
239
+ monthlyActiveUsers : [
240
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
241
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 } ,
242
+ { ...PRE_WEEK_USER , creation_ts : PRE_WEEK_USER . creation_ts * 1000 }
243
+ ] ,
244
+ weeklyNewUsers : [
245
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
246
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
247
+ ] ,
248
+ monthlyNewUsers : [
249
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
250
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 } ,
251
+ { ...PRE_WEEK_USER , creation_ts : PRE_WEEK_USER . creation_ts * 1000 }
252
+ ]
213
253
} )
214
254
} )
215
255
@@ -263,11 +303,101 @@ describe('the Metrics API Service', () => {
263
303
const result = await metricsService . getUserActivityStats ( )
264
304
265
305
expect ( result ) . toEqual ( {
266
- dailyActiveUsers : [ TODAY_USER ] ,
267
- weeklyActiveUsers : [ TODAY_USER , PRE_TODAY_USER ] ,
268
- monthlyActiveUsers : [ TODAY_USER , PRE_TODAY_USER , PRE_WEEK_USER ] ,
269
- weeklyNewUsers : [ ] ,
270
- monthlyNewUsers : [ ]
306
+ dailyActiveUsers : [
307
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
308
+ ] ,
309
+ weeklyActiveUsers : [
310
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
311
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
312
+ ] ,
313
+ monthlyActiveUsers : [
314
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
315
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 } ,
316
+ { ...PRE_WEEK_USER , creation_ts : PRE_WEEK_USER . creation_ts * 1000 }
317
+ ] ,
318
+ weeklyNewUsers : [
319
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
320
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
321
+ ] ,
322
+ monthlyNewUsers : [
323
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
324
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 } ,
325
+ { ...PRE_WEEK_USER , creation_ts : PRE_WEEK_USER . creation_ts * 1000 }
326
+ ]
327
+ } )
328
+ } )
329
+
330
+ it ( 'should return the weekly and monthly new users list' , async ( ) => {
331
+ dbMock . getAll . mockResolvedValue ( [
332
+ TODAY_USER ,
333
+ PRE_TODAY_USER ,
334
+ PRE_WEEK_USER ,
335
+ PRE_MONTH_USER
336
+ ] )
337
+
338
+ dbMock . get . mockImplementation (
339
+ async (
340
+ _table : string ,
341
+ _fields : string [ ] ,
342
+ filters : Record < string , string | number >
343
+ ) => {
344
+ switch ( filters . user_id ) {
345
+ case TODAY_USER . name :
346
+ return await Promise . resolve ( [
347
+ {
348
+ user_id : TODAY_USER . name ,
349
+ last_seen : TODAY_USER . last_seen_ts
350
+ }
351
+ ] )
352
+ case PRE_TODAY_USER . name :
353
+ return await Promise . resolve ( [
354
+ {
355
+ user_id : PRE_TODAY_USER . name ,
356
+ last_seen : PRE_TODAY_USER . last_seen_ts
357
+ }
358
+ ] )
359
+ case PRE_WEEK_USER . name :
360
+ return await Promise . resolve ( [
361
+ {
362
+ user_id : PRE_WEEK_USER . name ,
363
+ last_seen : PRE_WEEK_USER . last_seen_ts
364
+ }
365
+ ] )
366
+ case PRE_MONTH_USER . name :
367
+ return await Promise . resolve ( [
368
+ {
369
+ user_id : PRE_MONTH_USER . name ,
370
+ last_seen : PRE_MONTH_USER . last_seen_ts
371
+ }
372
+ ] )
373
+ }
374
+ }
375
+ )
376
+
377
+ const result = await metricsService . getUserActivityStats ( )
378
+
379
+ expect ( result ) . toEqual ( {
380
+ dailyActiveUsers : [
381
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 }
382
+ ] ,
383
+ weeklyActiveUsers : [
384
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
385
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
386
+ ] ,
387
+ monthlyActiveUsers : [
388
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
389
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 } ,
390
+ { ...PRE_WEEK_USER , creation_ts : PRE_WEEK_USER . creation_ts * 1000 }
391
+ ] ,
392
+ weeklyNewUsers : [
393
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
394
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 }
395
+ ] ,
396
+ monthlyNewUsers : [
397
+ { ...TODAY_USER , creation_ts : TODAY_USER . creation_ts * 1000 } ,
398
+ { ...PRE_TODAY_USER , creation_ts : PRE_TODAY_USER . creation_ts * 1000 } ,
399
+ { ...PRE_WEEK_USER , creation_ts : PRE_WEEK_USER . creation_ts * 1000 }
400
+ ]
271
401
} )
272
402
} )
273
403
} )
0 commit comments