mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
refactor(core): migrate dashboard/users/active
api data deps (#5457)
This commit is contained in:
parent
cc01acbd0e
commit
a0a19b13f9
3 changed files with 33 additions and 5 deletions
|
@ -1,14 +1,42 @@
|
|||
import { DailyActiveUsers } from '@logto/schemas';
|
||||
import type { CommonQueryMethods } from 'slonik';
|
||||
import { convertToIdentifiers } from '@logto/shared';
|
||||
import { sql, type CommonQueryMethods } from 'slonik';
|
||||
|
||||
import { buildInsertIntoWithPool } from '#src/database/insert-into.js';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(DailyActiveUsers);
|
||||
|
||||
export const createDailyActiveUsersQueries = (pool: CommonQueryMethods) => {
|
||||
const insertActiveUser = buildInsertIntoWithPool(pool)(DailyActiveUsers, {
|
||||
onConflict: { ignore: true },
|
||||
});
|
||||
|
||||
const countActiveUsersByTimeInterval = async (
|
||||
startTimeExclusive: number,
|
||||
endTimeInclusive: number
|
||||
) =>
|
||||
pool.one<{ count: number }>(sql`
|
||||
select count(distinct(${fields.userId}))
|
||||
from ${table}
|
||||
where ${fields.date} > to_timestamp(${startTimeExclusive}::double precision / 1000)
|
||||
and ${fields.date} <= to_timestamp(${endTimeInclusive}::double precision / 1000)
|
||||
`);
|
||||
|
||||
const getDailyActiveUserCountsByTimeInterval = async (
|
||||
startTimeExclusive: number,
|
||||
endTimeInclusive: number
|
||||
) =>
|
||||
pool.any<{ date: string; count: number }>(sql`
|
||||
select date(${fields.date}), count(distinct(${fields.userId}))
|
||||
from ${table}
|
||||
where ${fields.date} > to_timestamp(${startTimeExclusive}::double precision / 1000)
|
||||
and ${fields.date} <= to_timestamp(${endTimeInclusive}::double precision / 1000)
|
||||
group by date(${fields.date})
|
||||
`);
|
||||
|
||||
return {
|
||||
insertActiveUser,
|
||||
countActiveUsersByTimeInterval,
|
||||
getDailyActiveUserCountsByTimeInterval,
|
||||
};
|
||||
};
|
||||
|
|
|
@ -41,13 +41,13 @@ const users = {
|
|||
};
|
||||
const { countUsers, getDailyNewUserCountsByTimeInterval } = users;
|
||||
|
||||
const logs = {
|
||||
const dailyActiveUsers = {
|
||||
getDailyActiveUserCountsByTimeInterval: jest.fn().mockResolvedValue(mockDailyActiveUserCounts),
|
||||
countActiveUsersByTimeInterval: jest.fn().mockResolvedValue({ count: mockActiveUserCount }),
|
||||
};
|
||||
const { getDailyActiveUserCountsByTimeInterval, countActiveUsersByTimeInterval } = logs;
|
||||
const { getDailyActiveUserCountsByTimeInterval, countActiveUsersByTimeInterval } = dailyActiveUsers;
|
||||
|
||||
const tenantContext = new MockTenant(undefined, { logs, users });
|
||||
const tenantContext = new MockTenant(undefined, { dailyActiveUsers, users });
|
||||
const dashboardRoutes = await pickDefault(import('./dashboard.js'));
|
||||
|
||||
describe('dashboardRoutes', () => {
|
||||
|
|
|
@ -17,7 +17,7 @@ export default function dashboardRoutes<T extends AuthedRouter>(
|
|||
...[router, { queries }]: RouterInitArgs<T>
|
||||
) {
|
||||
const {
|
||||
logs: { countActiveUsersByTimeInterval, getDailyActiveUserCountsByTimeInterval },
|
||||
dailyActiveUsers: { countActiveUsersByTimeInterval, getDailyActiveUserCountsByTimeInterval },
|
||||
users: { countUsers, getDailyNewUserCountsByTimeInterval },
|
||||
} = queries;
|
||||
|
||||
|
|
Loading…
Reference in a new issue