forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20260129000000-CreateAnalyticsMaterializedViews.ts
More file actions
53 lines (48 loc) · 2.01 KB
/
Copy path20260129000000-CreateAnalyticsMaterializedViews.ts
File metadata and controls
53 lines (48 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { MigrationInterface, QueryRunner } from "typeorm";
export class CreateAnalyticsMaterializedViews20260129000000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
// Spending trends materialized view (monthly aggregates)
await queryRunner.query(`
CREATE MATERIALIZED VIEW IF NOT EXISTS analytics_spending_trends_monthly AS
SELECT
p.userId::varchar AS user_id,
date_trunc('month', payment."createdAt") AS period,
SUM(payment.amount::numeric) AS total_spent,
COUNT(*) AS tx_count,
AVG(payment.amount::numeric) AS avg_tx_amount
FROM payments payment
INNER JOIN participants p ON p.id = payment."participantId"
WHERE payment.status = 'confirmed'
GROUP BY user_id, period;
`);
// Unique index required for CONCURRENTLY refresh
await queryRunner.query(`
CREATE UNIQUE INDEX IF NOT EXISTS idx_analytics_spending_trends_monthly_user_period
ON analytics_spending_trends_monthly (user_id, period);
`);
// Category breakdown materialized view (monthly)
await queryRunner.query(`
CREATE MATERIALIZED VIEW IF NOT EXISTS analytics_category_spend AS
SELECT
p.userId::varchar AS user_id,
COALESCE(i.category, 'uncategorized') AS category,
date_trunc('month', sp."createdAt") AS period,
SUM(i.totalPrice::numeric) AS total_by_category
FROM items i
INNER JOIN splits sp ON i."splitId" = sp.id
INNER JOIN participants p ON p."splitId" = sp.id
GROUP BY user_id, category, period;
`);
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS idx_analytics_category_spend_user_period ON analytics_category_spend (user_id, period);
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
"DROP MATERIALIZED VIEW IF EXISTS analytics_spending_trends_monthly",
);
await queryRunner.query(
"DROP MATERIALIZED VIEW IF EXISTS analytics_category_spend",
);
}
}