mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-22 12:03:25 -04:00
(admin) hopefully decrease peak memory usage of /admin/stats/user-charts/:locale by streaming rows
This commit is contained in:
parent
1a73ddf760
commit
7c25397a9f
@ -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>;
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user