From c65730bcd22380f82d91c3b64d2a6deb80d86c90 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Fri, 1 Mar 2024 12:43:55 +0800 Subject: [PATCH] refactor(core, schemas): enhance the time accuracy of dau data (#5451) --- .../src/event-listeners/record-active-users.ts | 2 -- ...ext-1709190131-enhance-dau-data-accuracy.ts | 18 ++++++++++++++++++ packages/schemas/tables/daily_active_users.sql | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 packages/schemas/alterations/next-1709190131-enhance-dau-data-accuracy.ts diff --git a/packages/core/src/event-listeners/record-active-users.ts b/packages/core/src/event-listeners/record-active-users.ts index 937a0177f..2a78e76f1 100644 --- a/packages/core/src/event-listeners/record-active-users.ts +++ b/packages/core/src/event-listeners/record-active-users.ts @@ -1,6 +1,5 @@ import { generateStandardId } from '@logto/shared'; -import { getUtcStartOfTheDay } from '#src/oidc/utils.js'; import type Queries from '#src/tenants/Queries.js'; export const recordActiveUsers = async (accessToken: { accountId?: string }, queries: Queries) => { @@ -16,6 +15,5 @@ export const recordActiveUsers = async (accessToken: { accountId?: string }, que await insertActiveUser({ id: generateStandardId(), userId: accountId, - date: getUtcStartOfTheDay(new Date()).getTime(), }); }; diff --git a/packages/schemas/alterations/next-1709190131-enhance-dau-data-accuracy.ts b/packages/schemas/alterations/next-1709190131-enhance-dau-data-accuracy.ts new file mode 100644 index 000000000..8895f037d --- /dev/null +++ b/packages/schemas/alterations/next-1709190131-enhance-dau-data-accuracy.ts @@ -0,0 +1,18 @@ +import { sql } from 'slonik'; + +import type { AlterationScript } from '../lib/types/alteration.js'; + +const alteration: AlterationScript = { + up: async (pool) => { + await pool.query(sql` + alter table daily_active_users alter column date set default now(); + `); + }, + down: async (pool) => { + await pool.query(sql` + alter table daily_active_users alter column date drop default; + `); + }, +}; + +export default alteration; diff --git a/packages/schemas/tables/daily_active_users.sql b/packages/schemas/tables/daily_active_users.sql index 9f816d159..719ccc6b6 100644 --- a/packages/schemas/tables/daily_active_users.sql +++ b/packages/schemas/tables/daily_active_users.sql @@ -3,7 +3,7 @@ create table daily_active_users ( tenant_id varchar(21) not null references tenants (id) on update cascade on delete cascade, user_id varchar(21) not null, - date timestamptz not null, + date timestamptz not null default (now()), primary key (id), constraint daily_active_users__user_id_date unique (user_id, date)