1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00

feat(1-3260): impl function

This commit is contained in:
Thomas Heartman 2025-01-22 08:55:34 +01:00
parent 44ee534b9c
commit ef5209fe57
No known key found for this signature in database
GPG Key ID: BD1F880DAED1EE78
2 changed files with 33 additions and 8 deletions

View File

@ -204,7 +204,7 @@ test('can query for monthly aggregation of data for a specified range', async ()
// fill in with data for the last 13 months // fill in with data for the last 13 months
for (let i = 0; i <= 12; i++) { for (let i = 0; i <= 12; i++) {
const month = subMonths(now, i).getMonth(); const then = subMonths(now, i);
let monthAggregateA = 0; let monthAggregateA = 0;
let monthAggregateB = 0; let monthAggregateB = 0;
for (let day = 1; day <= 5; day++) { for (let day = 1; day <= 5; day++) {
@ -213,14 +213,14 @@ test('can query for monthly aggregation of data for a specified range', async ()
monthAggregateA += dayValue; monthAggregateA += dayValue;
monthAggregateB += dayValueB; monthAggregateB += dayValueB;
const dataA = { const dataA = {
day: new Date(2024, month, day), day: new Date(then.getFullYear(), then.getMonth(), day),
trafficGroup: 'groupA', trafficGroup: 'groupA',
statusCodeSeries: 200, statusCodeSeries: 200,
count: dayValue, count: dayValue,
}; };
await trafficDataUsageStore.upsert(dataA); await trafficDataUsageStore.upsert(dataA);
const dataB = { const dataB = {
day: new Date(2024, month, day), day: new Date(then.getFullYear(), then.getMonth(), day),
trafficGroup: 'groupB', trafficGroup: 'groupB',
statusCodeSeries: 200, statusCodeSeries: 200,
count: dayValueB, count: dayValueB,
@ -233,14 +233,12 @@ test('can query for monthly aggregation of data for a specified range', async ()
}); });
} }
console.log(expectedValues);
for (const monthsBack of [3, 6, 12]) { for (const monthsBack of [3, 6, 12]) {
const result = const result =
await trafficDataUsageStore.getTrafficDataForMonthRange(monthsBack); await trafficDataUsageStore.getTrafficDataForMonthRange(monthsBack);
// should have the current month and the preceding n months // should have the current month and the preceding n months
expect(result.length).toBe(monthsBack + 1); expect(result.length).toBe((monthsBack + 1) * 2);
for (const entry of result) { for (const entry of result) {
const index = differenceInCalendarMonths( const index = differenceInCalendarMonths(

View File

@ -102,6 +102,33 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
async getTrafficDataForMonthRange( async getTrafficDataForMonthRange(
monthsBack: number, monthsBack: number,
): Promise<IStatMonthlyTrafficUsage[]> { ): Promise<IStatMonthlyTrafficUsage[]> {
return []; const rows = await this.db(TABLE)
.select(
'traffic_group',
'status_code_series',
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],
)
.groupBy([
'traffic_group',
this.db.raw(`to_char(day, 'YYYY-MM')`),
'status_code_series',
])
.orderBy([
{ column: 'month', order: 'desc' },
{ column: 'traffic_group', order: 'asc' },
]);
return rows.map(
({ traffic_group, status_code_series, month, count }) => ({
trafficGroup: traffic_group,
statusCodeSeries: status_code_series,
month,
count: Number.parseInt(count),
}),
);
} }
} }