1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

chore(1-3293): add new method for monthly aggregate of traffic data

This commit is contained in:
Thomas Heartman 2025-01-24 14:42:27 +01:00
parent 6fe1d9f1a7
commit 6079114217
No known key found for this signature in database
GPG Key ID: BD1F880DAED1EE78

View File

@ -1,4 +1,4 @@
import { endOfMonth, startOfMonth } from 'date-fns';
import { endOfMonth, startOfMonth, subMonths } from 'date-fns';
import type { Db } from '../../db/db';
import type { Logger, LogProvider } from '../../logger';
import type {
@ -112,8 +112,9 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
);
}
async getTrafficDataForMonthRange(
monthsBack: number,
async getMonthlyTrafficDataUsageForPeriod(
from: Date,
to: Date,
): Promise<IStatMonthlyTrafficUsage[]> {
const rows = await this.db(TABLE)
.select(
@ -122,10 +123,8 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
this.db.raw(`to_char(day, 'YYYY-MM') AS month`),
this.db.raw(`SUM(count) AS count`),
)
.whereRaw(
`day >= date_trunc('month', CURRENT_DATE) - make_interval(months := ?)`,
[monthsBack],
)
.where('day', '>=', from)
.andWhere('day', '<=', to)
.groupBy([
'traffic_group',
this.db.raw(`to_char(day, 'YYYY-MM')`),
@ -144,4 +143,12 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
}),
);
}
async getTrafficDataForMonthRange(
monthsBack: number,
): Promise<IStatMonthlyTrafficUsage[]> {
const to = endOfMonth(new Date());
const from = startOfMonth(subMonths(to, monthsBack));
return this.getMonthlyTrafficDataUsageForPeriod(from, to);
}
}