(admin) hopefully decrease peak memory usage of /admin/stats/user-charts/:locale by streaming rows

This commit is contained in:
Valentyne Stigloher 2024-06-03 19:39:39 +02:00
parent 1a73ddf760
commit 7c25397a9f
3 changed files with 8 additions and 2 deletions

View File

@ -6,6 +6,7 @@ const __dirname = new URL('.', import.meta.url).pathname;
export type SQLQuery = sqlite.ISqlite.SqlType;
export interface Database {
get<T = unknown>(sql: SQLQuery, ...args: unknown[]): Promise<T | undefined>;
each<T = unknown>(sql: SQLQuery, callback: (err: unknown, row: T) => void): Promise<number>;
all<T = unknown>(sql: SQLQuery, ...args: unknown[]): Promise<T[]>;
close(): Promise<void>;
}

View File

@ -108,6 +108,11 @@ export class LazyDatabase implements Database {
return Sentry.startSpan(this.buildSpanOptions(sql), () => this.db!.get(sql, ...args));
}
async each<T = unknown>(sql: SQLQuery, callback: (err: unknown, row: T) => void): Promise<number> {
await this.init();
return Sentry.startSpan(this.buildSpanOptions(sql), () => this.db!.each(sql, callback));
}
async all<T = unknown>(sql: SQLQuery, ...args: unknown[]): Promise<T[]> {
await this.init();
return Sentry.startSpan(this.buildSpanOptions(sql), () => this.db!.all(sql, ...args));

View File

@ -224,10 +224,10 @@ router.get('/admin/stats/users-chart/:locale', handleErrorAsync(async (req, res)
.padStart(2, '0')}`;
const stats = {};
for (const { id, users } of await req.db.all(SQL`SELECT id, users FROM stats WHERE locale = ${req.params.locale} ORDER BY id ASC`)) {
await req.db.each(SQL`SELECT id, users FROM stats WHERE locale = ${req.params.locale} ORDER BY id ASC`, (_err, { id, users }) => {
const date = formatDate(new Date(decodeTime(id)));
stats[date] = users; // overwrite with the latest one for the day
}
});
const incrementsChart = {};
let prevUsers = null;