mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-31 13:47:02 +02:00
chore(1-3293): add new method for monthly aggregate of traffic data
This commit is contained in:
parent
6fe1d9f1a7
commit
6079114217
@ -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 { Db } from '../../db/db';
|
||||||
import type { Logger, LogProvider } from '../../logger';
|
import type { Logger, LogProvider } from '../../logger';
|
||||||
import type {
|
import type {
|
||||||
@ -112,8 +112,9 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTrafficDataForMonthRange(
|
async getMonthlyTrafficDataUsageForPeriod(
|
||||||
monthsBack: number,
|
from: Date,
|
||||||
|
to: Date,
|
||||||
): Promise<IStatMonthlyTrafficUsage[]> {
|
): Promise<IStatMonthlyTrafficUsage[]> {
|
||||||
const rows = await this.db(TABLE)
|
const rows = await this.db(TABLE)
|
||||||
.select(
|
.select(
|
||||||
@ -122,10 +123,8 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
|
|||||||
this.db.raw(`to_char(day, 'YYYY-MM') AS month`),
|
this.db.raw(`to_char(day, 'YYYY-MM') AS month`),
|
||||||
this.db.raw(`SUM(count) AS count`),
|
this.db.raw(`SUM(count) AS count`),
|
||||||
)
|
)
|
||||||
.whereRaw(
|
.where('day', '>=', from)
|
||||||
`day >= date_trunc('month', CURRENT_DATE) - make_interval(months := ?)`,
|
.andWhere('day', '<=', to)
|
||||||
[monthsBack],
|
|
||||||
)
|
|
||||||
.groupBy([
|
.groupBy([
|
||||||
'traffic_group',
|
'traffic_group',
|
||||||
this.db.raw(`to_char(day, 'YYYY-MM')`),
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user