Skip to content

Commit

Permalink
chore(1-3293): verify that data is end of day/start of day
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Jan 24, 2025
1 parent 6079114 commit 3ad10be
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/lib/features/traffic-data-usage/traffic-data-usage-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { endOfMonth, startOfMonth, subMonths } from 'date-fns';
import {
endOfDay,
endOfMonth,
startOfDay,
startOfMonth,
subMonths,
} from 'date-fns';
import type { Db } from '../../db/db';
import type { Logger, LogProvider } from '../../logger';
import type {
Expand Down Expand Up @@ -95,23 +101,12 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
to: Date,
): Promise<IStatTrafficUsage[]> {
const rows = await this.db<IStatTrafficUsage>(TABLE)
.where('day', '>=', from)
.andWhere('day', '<=', to);
.where('day', '>=', startOfDay(from))
.andWhere('day', '<=', endOfDay(to));

return rows.map(mapRow);
}

// @deprecated: remove with flag `dataUsageMultiMonthView`
async getTrafficDataUsageForPeriod(
period: string,
): Promise<IStatTrafficUsage[]> {
const month = new Date(period);
return this.getDailyTrafficDataUsageForPeriod(
startOfMonth(month),
endOfMonth(month),
);
}

async getMonthlyTrafficDataUsageForPeriod(
from: Date,
to: Date,
Expand All @@ -123,8 +118,8 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
this.db.raw(`to_char(day, 'YYYY-MM') AS month`),
this.db.raw(`SUM(count) AS count`),
)
.where('day', '>=', from)
.andWhere('day', '<=', to)
.where('day', '>=', startOfDay(from))
.andWhere('day', '<=', endOfDay(to))
.groupBy([
'traffic_group',
this.db.raw(`to_char(day, 'YYYY-MM')`),
Expand All @@ -144,6 +139,17 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
);
}

async getTrafficDataUsageForPeriod(
period: string,
): Promise<IStatTrafficUsage[]> {
const month = new Date(period);
return this.getDailyTrafficDataUsageForPeriod(
startOfMonth(month),
endOfMonth(month),
);
}

// @deprecated: remove with flag `dataUsageMultiMonthView`
async getTrafficDataForMonthRange(
monthsBack: number,
): Promise<IStatMonthlyTrafficUsage[]> {
Expand Down

0 comments on commit 3ad10be

Please sign in to comment.