0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00

refactor(core): fix dashboard queries

This commit is contained in:
Gao Sun 2022-12-16 17:31:26 +08:00
parent 9c04da0ffe
commit 854e1e5751
No known key found for this signature in database
GPG key ID: 13EBE123E4773688

View file

@ -1,6 +1,5 @@
import type { CreateLog, Log } from '@logto/schemas';
import { Logs } from '@logto/schemas';
import type { LogType } from '@logto/schemas/lib/types/log-legacy.js';
import { token, Logs } from '@logto/schemas';
import { conditionalSql, convertToIdentifiers } from '@logto/shared';
import { sql } from 'slonik';
@ -51,10 +50,6 @@ export const findLogs = async (limit: number, offset: number, logCondition: LogC
export const findLogById = buildFindEntityById<CreateLog, Log>(Logs);
// The active user should exchange the tokens by the authorization code (i.e. sign-in)
// or exchange the access token, which will expire in 2 hours, by the refresh token.
const activeUserLogTypes: LogType[] = ['CodeExchangeToken', 'RefreshTokenExchangeToken'];
export const getDailyActiveUserCountsByTimeInterval = async (
startTimeExclusive: number,
endTimeInclusive: number
@ -64,7 +59,7 @@ export const getDailyActiveUserCountsByTimeInterval = async (
from ${table}
where ${fields.createdAt} > to_timestamp(${startTimeExclusive}::double precision / 1000)
and ${fields.createdAt} <= to_timestamp(${endTimeInclusive}::double precision / 1000)
and ${fields.type} in (${sql.join(activeUserLogTypes, sql`, `)})
and ${fields.type} like ${`${token.Flow.ExchangeTokenBy}.%`}
and ${fields.payload}->>'result' = 'Success'
group by date(${fields.createdAt})
`);
@ -78,6 +73,6 @@ export const countActiveUsersByTimeInterval = async (
from ${table}
where ${fields.createdAt} > to_timestamp(${startTimeExclusive}::double precision / 1000)
and ${fields.createdAt} <= to_timestamp(${endTimeInclusive}::double precision / 1000)
and ${fields.type} in (${sql.join(activeUserLogTypes, sql`, `)})
and ${fields.type} like ${`${token.Flow.ExchangeTokenBy}.%`}
and ${fields.payload}->>'result' = 'Success'
`);