diff --git a/packages/tom-server/src/metrics-api/services/index.ts b/packages/tom-server/src/metrics-api/services/index.ts index 3ef66d3c..0e8b6d8c 100644 --- a/packages/tom-server/src/metrics-api/services/index.ts +++ b/packages/tom-server/src/metrics-api/services/index.ts @@ -92,6 +92,7 @@ class MetricsService implements IMetricsService { matrixUsers.push({ ...user, + creation_ts: user.creation_ts * 1000, last_seen_ts: lastSeenTs }) }) @@ -227,11 +228,11 @@ class MetricsService implements IMetricsService { } )) as unknown as MatrixUserIpInfo[] - const latestSeenTime = queryResult.reduce((latestTime, userIpInfo) => { - const parsedLastSeen = parseInt(userIpInfo.last_seen, 10) - - return parsedLastSeen > latestTime ? parsedLastSeen : latestTime - }, 0) + const latestSeenTime = queryResult.reduce( + (latestTime, userIpInfo) => + userIpInfo.last_seen > latestTime ? userIpInfo.last_seen : latestTime, + 0 + ) return latestSeenTime } catch (error) { diff --git a/packages/tom-server/src/metrics-api/tests/controller.test.ts b/packages/tom-server/src/metrics-api/tests/controller.test.ts index 9facd992..7e5c6d6a 100644 --- a/packages/tom-server/src/metrics-api/tests/controller.test.ts +++ b/packages/tom-server/src/metrics-api/tests/controller.test.ts @@ -12,25 +12,25 @@ const ONE_WEEK_IN_MS = 7 * ONE_DAY_IN_MS const ONE_MONTH_IN_MS = 30 * ONE_DAY_IN_MS const TODAY_USER = { - creation_ts: 1, + creation_ts: new Date().getTime() / 1000, last_seen_ts: new Date().getTime(), name: 'user1' } satisfies MatrixUserInfo const PRE_TODAY_USER = { - creation_ts: 1, + creation_ts: (new Date().getTime() - ONE_DAY_IN_MS - 1) / 1000, last_seen_ts: new Date().getTime() - ONE_DAY_IN_MS - 1, name: 'user2' } const PRE_WEEK_USER = { - creation_ts: 1, + creation_ts: (new Date().getTime() - ONE_WEEK_IN_MS - 1) / 1000, last_seen_ts: new Date().getTime() - ONE_WEEK_IN_MS - 1, name: 'user3' } const PRE_MONTH_USER = { - creation_ts: 1, + creation_ts: (new Date().getTime() - ONE_MONTH_IN_MS - 1) / 1000, last_seen_ts: new Date().getTime() - ONE_MONTH_IN_MS - 1, name: 'user4' } @@ -129,11 +129,21 @@ describe('the mectrics API controller', () => { expect(response.status).toBe(200) expect(response.body).toEqual({ - dailyActiveUsers: [TODAY_USER], - weeklyActiveUsers: [TODAY_USER], - monthlyActiveUsers: [TODAY_USER], - weeklyNewUsers: [], - monthlyNewUsers: [] + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ] }) }) @@ -167,11 +177,25 @@ describe('the mectrics API controller', () => { expect(response.status).toBe(200) expect(response.body).toEqual({ - dailyActiveUsers: [TODAY_USER], - weeklyActiveUsers: [TODAY_USER, PRE_TODAY_USER], - monthlyActiveUsers: [TODAY_USER, PRE_TODAY_USER], - weeklyNewUsers: [], - monthlyNewUsers: [] + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ] }) }) @@ -206,11 +230,23 @@ describe('the mectrics API controller', () => { expect(response.status).toBe(200) expect(response.body).toEqual({ - dailyActiveUsers: [TODAY_USER], - weeklyActiveUsers: [TODAY_USER], - monthlyActiveUsers: [TODAY_USER, PRE_WEEK_USER], - weeklyNewUsers: [], - monthlyNewUsers: [] + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ] }) }) @@ -247,9 +283,13 @@ describe('the mectrics API controller', () => { expect(response.body).toEqual({ dailyActiveUsers: [], weeklyActiveUsers: [], - monthlyActiveUsers: [PRE_WEEK_USER], + monthlyActiveUsers: [ + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ], weeklyNewUsers: [], - monthlyNewUsers: [] + monthlyNewUsers: [ + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ] }) }) diff --git a/packages/tom-server/src/metrics-api/tests/service.test.ts b/packages/tom-server/src/metrics-api/tests/service.test.ts index 1edf4dbc..52092807 100644 --- a/packages/tom-server/src/metrics-api/tests/service.test.ts +++ b/packages/tom-server/src/metrics-api/tests/service.test.ts @@ -22,25 +22,25 @@ const ONE_WEEK_IN_MS = 7 * ONE_DAY_IN_MS const ONE_MONTH_IN_MS = 30 * ONE_DAY_IN_MS const TODAY_USER = { - creation_ts: 1, + creation_ts: new Date().getTime() / 1000, last_seen_ts: new Date().getTime(), name: 'user1' } const PRE_TODAY_USER = { - creation_ts: 1, + creation_ts: (new Date().getTime() - ONE_DAY_IN_MS - 1) / 1000, last_seen_ts: new Date().getTime() - ONE_DAY_IN_MS - 1, name: 'user2' } const PRE_WEEK_USER = { - creation_ts: 1, + creation_ts: (new Date().getTime() - ONE_WEEK_IN_MS - 1) / 1000, last_seen_ts: new Date().getTime() - ONE_WEEK_IN_MS - 1, name: 'user3' } const PRE_MONTH_USER = { - creation_ts: 1, + creation_ts: (new Date().getTime() - ONE_MONTH_IN_MS - 1) / 1000, last_seen_ts: new Date().getTime() - ONE_MONTH_IN_MS - 1, name: 'user4' } @@ -103,11 +103,21 @@ describe('the Metrics API Service', () => { const result = await metricsService.getUserActivityStats() expect(result).toEqual({ - dailyActiveUsers: [TODAY_USER], - weeklyActiveUsers: [TODAY_USER], - monthlyActiveUsers: [TODAY_USER], - weeklyNewUsers: [], - monthlyNewUsers: [] + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ] }) }) @@ -155,11 +165,25 @@ describe('the Metrics API Service', () => { const result = await metricsService.getUserActivityStats() expect(result).toEqual({ - dailyActiveUsers: [TODAY_USER], - weeklyActiveUsers: [TODAY_USER, PRE_TODAY_USER], - monthlyActiveUsers: [TODAY_USER, PRE_TODAY_USER], - weeklyNewUsers: [], - monthlyNewUsers: [] + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ] }) }) @@ -205,11 +229,27 @@ describe('the Metrics API Service', () => { const result = await metricsService.getUserActivityStats() expect(result).toEqual({ - dailyActiveUsers: [TODAY_USER], - weeklyActiveUsers: [TODAY_USER, PRE_TODAY_USER], - monthlyActiveUsers: [TODAY_USER, PRE_TODAY_USER, PRE_WEEK_USER], - weeklyNewUsers: [], - monthlyNewUsers: [] + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ] }) }) @@ -263,11 +303,101 @@ describe('the Metrics API Service', () => { const result = await metricsService.getUserActivityStats() expect(result).toEqual({ - dailyActiveUsers: [TODAY_USER], - weeklyActiveUsers: [TODAY_USER, PRE_TODAY_USER], - monthlyActiveUsers: [TODAY_USER, PRE_TODAY_USER, PRE_WEEK_USER], - weeklyNewUsers: [], - monthlyNewUsers: [] + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ] + }) + }) + + it('should return the weekly and monthly new users list', async () => { + dbMock.getAll.mockResolvedValue([ + TODAY_USER, + PRE_TODAY_USER, + PRE_WEEK_USER, + PRE_MONTH_USER + ]) + + dbMock.get.mockImplementation( + async ( + _table: string, + _fields: string[], + filters: Record + ) => { + switch (filters.user_id) { + case TODAY_USER.name: + return await Promise.resolve([ + { + user_id: TODAY_USER.name, + last_seen: TODAY_USER.last_seen_ts + } + ]) + case PRE_TODAY_USER.name: + return await Promise.resolve([ + { + user_id: PRE_TODAY_USER.name, + last_seen: PRE_TODAY_USER.last_seen_ts + } + ]) + case PRE_WEEK_USER.name: + return await Promise.resolve([ + { + user_id: PRE_WEEK_USER.name, + last_seen: PRE_WEEK_USER.last_seen_ts + } + ]) + case PRE_MONTH_USER.name: + return await Promise.resolve([ + { + user_id: PRE_MONTH_USER.name, + last_seen: PRE_MONTH_USER.last_seen_ts + } + ]) + } + } + ) + + const result = await metricsService.getUserActivityStats() + + expect(result).toEqual({ + dailyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 } + ], + weeklyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyActiveUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ], + weeklyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 } + ], + monthlyNewUsers: [ + { ...TODAY_USER, creation_ts: TODAY_USER.creation_ts * 1000 }, + { ...PRE_TODAY_USER, creation_ts: PRE_TODAY_USER.creation_ts * 1000 }, + { ...PRE_WEEK_USER, creation_ts: PRE_WEEK_USER.creation_ts * 1000 } + ] }) }) }) diff --git a/packages/tom-server/src/metrics-api/types.ts b/packages/tom-server/src/metrics-api/types.ts index 4f9de3de..10a6cf3b 100644 --- a/packages/tom-server/src/metrics-api/types.ts +++ b/packages/tom-server/src/metrics-api/types.ts @@ -39,5 +39,5 @@ export interface UserMessageEvent { export interface MatrixUserIpInfo { user_id: string device_id: string - last_seen: string + last_seen: number }