Skip to content

Commit cc10a13

Browse files
authored
feat: add aggregated daily active users table (#8026)
* feat: add aggregated daily active users table * refactor: add comments
1 parent 4ff61fc commit cc10a13

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { sql } from '@silverhand/slonik';
2+
3+
import type { AlterationScript } from '../lib/types/alteration.js';
4+
5+
import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';
6+
7+
/**
8+
* Create aggregated_daily_active_users table for MAU-based billing system.
9+
* This table consolidates daily user activities for efficient billing calculations.
10+
*/
11+
12+
const alteration: AlterationScript = {
13+
up: async (pool) => {
14+
// Create the aggregated daily active users table
15+
await pool.query(sql`
16+
create table aggregated_daily_active_users (
17+
tenant_id varchar(21) not null,
18+
activity_date date not null,
19+
user_id varchar(21) not null,
20+
activity_count integer not null,
21+
primary key (tenant_id, activity_date, user_id)
22+
);
23+
`);
24+
25+
// Index for billing cycle range queries
26+
await pool.query(sql`
27+
create index aggregated_daily_active_users__tenant_date
28+
on aggregated_daily_active_users (tenant_id, activity_date);
29+
`);
30+
31+
// Index for tenant-specific user activity queries
32+
await pool.query(sql`
33+
create index aggregated_daily_active_users__tenant_user_date
34+
on aggregated_daily_active_users (tenant_id, user_id, activity_date desc);
35+
`);
36+
37+
await applyTableRls(pool, 'aggregated_daily_active_users');
38+
},
39+
40+
down: async (pool) => {
41+
// Drop RLS policies first
42+
await dropTableRls(pool, 'aggregated_daily_active_users');
43+
44+
// Drop the table and all its indexes
45+
await pool.query(sql`
46+
drop table aggregated_daily_active_users;
47+
`);
48+
},
49+
};
50+
51+
export default alteration;

packages/schemas/src/gen/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export const getType = (
145145
case 'int2':
146146
case 'int4':
147147
case 'int8':
148+
case 'integer':
148149
case 'bigint':
149150
case 'float4':
150151
case 'float8':
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- This table is used to store aggregated data of daily active users for each tenant.
2+
-- A daily job summarizes data from the daily active users table and inserts it into this table, or removes expired data.
3+
-- Therefore, we should not directly manipulate this table, except for "read" operations.
4+
create table aggregated_daily_active_users (
5+
tenant_id varchar(21) not null,
6+
activity_date date not null,
7+
user_id varchar(21) not null,
8+
activity_count integer not null,
9+
primary key (tenant_id, activity_date, user_id)
10+
);
11+
12+
-- Index for billing cycle range queries
13+
create index aggregated_daily_active_users__tenant_date
14+
on aggregated_daily_active_users (tenant_id, activity_date);
15+
16+
-- Index for tenant-specific user activity queries
17+
create index aggregated_daily_active_users__tenant_user_date
18+
on aggregated_daily_active_users (tenant_id, user_id, activity_date desc);

0 commit comments

Comments
 (0)