Skip to content

Commit

Permalink
πŸ› fix monthly and weekly new users not being calculated correctly (#152)
Browse files Browse the repository at this point in the history
* πŸ› fix: monthly and weekly new users not being calculated correctly

* πŸ§ͺ chore: fix tests

* 🏷️ chore: fix types

* πŸ§ͺ chore: fix controller unit test

* πŸ§ͺ chore: fix tests

* πŸ§ͺ chore: fix tests
  • Loading branch information
rezk2ll authored Dec 12, 2024
1 parent 4c8e9b9 commit 943a592
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 51 deletions.
11 changes: 6 additions & 5 deletions packages/tom-server/src/metrics-api/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class MetricsService implements IMetricsService {

matrixUsers.push({
...user,
creation_ts: user.creation_ts * 1000,
last_seen_ts: lastSeenTs
})
})
Expand Down Expand Up @@ -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) {
Expand Down
82 changes: 61 additions & 21 deletions packages/tom-server/src/metrics-api/tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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 }
]
})
})

Expand Down Expand Up @@ -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 }
]
})
})

Expand Down Expand Up @@ -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 }
]
})
})

Expand Down Expand Up @@ -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 }
]
})
})

Expand Down
178 changes: 154 additions & 24 deletions packages/tom-server/src/metrics-api/tests/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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 }
]
})
})

Expand Down Expand Up @@ -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 }
]
})
})

Expand Down Expand Up @@ -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 }
]
})
})

Expand Down Expand Up @@ -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<string, string | number>
) => {
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 }
]
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/tom-server/src/metrics-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ export interface UserMessageEvent {
export interface MatrixUserIpInfo {
user_id: string
device_id: string
last_seen: string
last_seen: number
}

0 comments on commit 943a592

Please sign in to comment.