mirror of
https://github.com/Unleash/unleash.git
synced 2025-05-17 01:17:29 +02:00
chore(1-3293): add new query for daily data that uses date ranges (#9150)
Add new methods to the store behind data usage metrics that accept date ranges instead of a single month. The old data collection methods re-route to the new ones instead, so the new methods are tested implicitly. Also deprecates the new endpoint that's not in use anywhere except in an unused service method in Enterprise yet. ## Discussion point: Accepts from and to params as dates for type safety. You can send unparseable strings, but if you send a date object, you know it'll work. Leaves the use of the old method in `src/lib/features/instance-stats/instance-stats-service.ts` to keep changes small.
This commit is contained in:
parent
fb95415d27
commit
85ae6f3b95
@ -6,9 +6,11 @@ import type {
|
||||
import type { ITrafficDataUsageStore } from '../../types';
|
||||
import {
|
||||
differenceInCalendarMonths,
|
||||
endOfDay,
|
||||
format,
|
||||
isSameMonth,
|
||||
parse,
|
||||
startOfDay,
|
||||
} from 'date-fns';
|
||||
|
||||
export class FakeTrafficDataUsageStore implements ITrafficDataUsageStore {
|
||||
@ -89,4 +91,45 @@ export class FakeTrafficDataUsageStore implements ITrafficDataUsageStore {
|
||||
|
||||
return Object.values(data);
|
||||
}
|
||||
|
||||
async getDailyTrafficUsageDataForPeriod(
|
||||
from: Date,
|
||||
to: Date,
|
||||
): Promise<IStatTrafficUsage[]> {
|
||||
return this.trafficData.filter(
|
||||
(data) => data.day >= startOfDay(from) && data.day <= endOfDay(to),
|
||||
);
|
||||
}
|
||||
|
||||
async getMonthlyTrafficUsageDataForPeriod(
|
||||
from: Date,
|
||||
to: Date,
|
||||
): Promise<IStatMonthlyTrafficUsage[]> {
|
||||
const data: { [key: string]: IStatMonthlyTrafficUsage } =
|
||||
this.trafficData
|
||||
.filter(
|
||||
(data) =>
|
||||
data.day >= startOfDay(from) &&
|
||||
data.day <= endOfDay(to),
|
||||
)
|
||||
.reduce((acc, entry) => {
|
||||
const month = format(entry.day, 'yyyy-MM');
|
||||
const key = `${month}-${entry.trafficGroup}-${entry.statusCodeSeries}`;
|
||||
|
||||
if (acc[key]) {
|
||||
acc[key].count += entry.count;
|
||||
} else {
|
||||
acc[key] = {
|
||||
month,
|
||||
trafficGroup: entry.trafficGroup,
|
||||
statusCodeSeries: entry.statusCodeSeries,
|
||||
count: entry.count,
|
||||
};
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return Object.values(data);
|
||||
}
|
||||
}
|
||||
|
@ -27,4 +27,12 @@ export interface ITrafficDataUsageStore
|
||||
getTrafficDataForMonthRange(
|
||||
monthsBack: number,
|
||||
): Promise<IStatMonthlyTrafficUsage[]>;
|
||||
getDailyTrafficUsageDataForPeriod(
|
||||
from: Date,
|
||||
to: Date,
|
||||
): Promise<IStatTrafficUsage[]>;
|
||||
getMonthlyTrafficUsageDataForPeriod(
|
||||
from: Date,
|
||||
to: Date,
|
||||
): Promise<IStatMonthlyTrafficUsage[]>;
|
||||
}
|
||||
|
@ -1,3 +1,10 @@
|
||||
import {
|
||||
endOfDay,
|
||||
endOfMonth,
|
||||
startOfDay,
|
||||
startOfMonth,
|
||||
subMonths,
|
||||
} from 'date-fns';
|
||||
import type { Db } from '../../db/db';
|
||||
import type { Logger, LogProvider } from '../../logger';
|
||||
import type {
|
||||
@ -89,18 +96,20 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
|
||||
});
|
||||
}
|
||||
|
||||
async getTrafficDataUsageForPeriod(
|
||||
period: string,
|
||||
async getDailyTrafficUsageDataForPeriod(
|
||||
from: Date,
|
||||
to: Date,
|
||||
): Promise<IStatTrafficUsage[]> {
|
||||
const rows = await this.db<IStatTrafficUsage>(TABLE).whereRaw(
|
||||
`to_char(day, 'YYYY-MM') = ?`,
|
||||
[period],
|
||||
);
|
||||
const rows = await this.db<IStatTrafficUsage>(TABLE)
|
||||
.where('day', '>=', startOfDay(from))
|
||||
.andWhere('day', '<=', endOfDay(to));
|
||||
|
||||
return rows.map(mapRow);
|
||||
}
|
||||
|
||||
async getTrafficDataForMonthRange(
|
||||
monthsBack: number,
|
||||
async getMonthlyTrafficUsageDataForPeriod(
|
||||
from: Date,
|
||||
to: Date,
|
||||
): Promise<IStatMonthlyTrafficUsage[]> {
|
||||
const rows = await this.db(TABLE)
|
||||
.select(
|
||||
@ -109,10 +118,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', '>=', startOfDay(from))
|
||||
.andWhere('day', '<=', endOfDay(to))
|
||||
.groupBy([
|
||||
'traffic_group',
|
||||
this.db.raw(`to_char(day, 'YYYY-MM')`),
|
||||
@ -131,4 +138,23 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async getTrafficDataUsageForPeriod(
|
||||
period: string,
|
||||
): Promise<IStatTrafficUsage[]> {
|
||||
const month = new Date(period);
|
||||
return this.getDailyTrafficUsageDataForPeriod(
|
||||
startOfMonth(month),
|
||||
endOfMonth(month),
|
||||
);
|
||||
}
|
||||
|
||||
// @deprecated: remove with flag `dataUsageMultiMonthView`
|
||||
async getTrafficDataForMonthRange(
|
||||
monthsBack: number,
|
||||
): Promise<IStatMonthlyTrafficUsage[]> {
|
||||
const to = endOfMonth(new Date());
|
||||
const from = startOfMonth(subMonths(to, monthsBack));
|
||||
return this.getMonthlyTrafficUsageDataForPeriod(from, to);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user