From 3ad10be1b2892b19e9be790fe7df6ac920c85761 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Fri, 24 Jan 2025 14:48:14 +0100 Subject: [PATCH] chore(1-3293): verify that data is end of day/start of day --- .../traffic-data-usage-store.ts | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts b/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts index 7b1bb8ac10..23cdb488b4 100644 --- a/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts +++ b/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts @@ -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 { @@ -95,23 +101,12 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore { to: Date, ): Promise { const rows = await this.db(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 { - const month = new Date(period); - return this.getDailyTrafficDataUsageForPeriod( - startOfMonth(month), - endOfMonth(month), - ); - } - async getMonthlyTrafficDataUsageForPeriod( from: Date, to: Date, @@ -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')`), @@ -144,6 +139,17 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore { ); } + async getTrafficDataUsageForPeriod( + period: string, + ): Promise { + const month = new Date(period); + return this.getDailyTrafficDataUsageForPeriod( + startOfMonth(month), + endOfMonth(month), + ); + } + + // @deprecated: remove with flag `dataUsageMultiMonthView` async getTrafficDataForMonthRange( monthsBack: number, ): Promise {