forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.scheduler.ts
More file actions
81 lines (73 loc) · 2.82 KB
/
Copy pathanalytics.scheduler.ts
File metadata and controls
81 lines (73 loc) · 2.82 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Injectable, Logger } from "@nestjs/common";
import { Cron, CronExpression } from "@nestjs/schedule";
import { DataSource } from "typeorm";
import { AnalyticsService } from "./analytics.service";
@Injectable()
export class AnalyticsScheduler {
private readonly logger = new Logger(AnalyticsScheduler.name);
constructor(
private readonly dataSource: DataSource,
private readonly analyticsService: AnalyticsService,
) {}
// Refresh materialized views hourly to keep analytics data relatively fresh
@Cron(CronExpression.EVERY_HOUR)
async refreshMaterializedViews() {
try {
this.logger.debug(
"Refreshing analytics materialized views (concurrently)",
);
// Refresh concurrently where possible to avoid locking reads in production
await this.dataSource.query(
"REFRESH MATERIALIZED VIEW CONCURRENTLY analytics_spending_trends_monthly",
);
// Category view now has a unique index (migration added); refresh concurrently as well
await this.dataSource.query(
"REFRESH MATERIALIZED VIEW CONCURRENTLY analytics_category_spend",
);
this.logger.debug("Analytics materialized views refreshed");
} catch (err) {
this.logger.error("Failed to refresh materialized views", err as any);
}
}
// Schedule monthly generation of reports for previous month on the 1st at 02:00 UTC
@Cron("0 2 1 * *")
async enqueueMonthlyReports() {
try {
// compute previous month YYYY-MM
const now = new Date();
const prev = new Date(now.getFullYear(), now.getMonth() - 1, 1);
const month = `${prev.getFullYear()}-${String(prev.getMonth() + 1).padStart(2, "0")}`;
this.logger.debug(`Enqueuing monthly reports for month=${month}`);
const users = await this.dataSource.query("SELECT id FROM users");
for (const u of users) {
// Best-effort: enqueue per user; analyticsService will create report records
await this.analyticsService.enqueueExport({
type: "monthly-report",
month,
userId: u.id,
format: "csv",
} as any);
}
this.logger.debug(`Enqueued monthly reports for ${users.length} users`);
} catch (err) {
this.logger.error("Failed to enqueue monthly reports", err as any);
}
}
// Daily cleanup of old reports (runs at 03:00 UTC)
@Cron("0 3 * * *")
async cleanupOldReports() {
try {
const retentionDays = parseInt(
process.env.ANALYTICS_REPORT_RETENTION_DAYS || "30",
10,
);
this.logger.debug(
`Running cleanup of analytics reports older than ${retentionDays} days`,
);
await this.analyticsService.cleanupOldReports(retentionDays);
this.logger.debug("Cleanup job completed");
} catch (err) {
this.logger.error("Failed to cleanup old reports", err as any);
}
}
}