1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

fix: use modulo instead for Math.floor for rounding down to full hour

This commit is contained in:
Ivar Conradi Østhus 2021-10-08 09:53:33 +02:00
parent 497552097c
commit 290a4c18bf
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
2 changed files with 14 additions and 4 deletions

View File

@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import util from 'util';
import { Knex } from 'knex';
import { Logger, LogProvider } from '../logger';
import {
@ -20,10 +19,8 @@ interface ClientMetricsEnvTable {
const TABLE = 'client_metrics_env';
// Unsure if this would be better be done by the service?
export function roundDownToHour(date: Date): Date {
let p = 60 * 60 * 1000; // milliseconds in an hour
return new Date(Math.floor(date.getTime() / p) * p);
return new Date(date.getTime() - (date.getTime() % 3600000));
}
const fromRow = (row: ClientMetricsEnvTable) => ({

View File

@ -5,6 +5,7 @@ import {
IClientMetricsEnv,
IClientMetricsStoreV2,
} from '../../../lib/types/stores/client-metrics-store-v2';
import { roundDownToHour } from '../../../lib/db/client-metrics-store-v2';
let db;
let stores: IUnleashStores;
@ -382,3 +383,15 @@ test('Should not exists after delete', async () => {
const existAfter = await clientMetricsStore.exists(metric);
expect(existAfter).toBe(false);
});
test('should floor hours as expected', () => {
expect(
roundDownToHour(new Date('2019-11-12T08:44:32.499Z')).toISOString(),
).toBe('2019-11-12T08:00:00.000Z');
expect(
roundDownToHour(new Date('2019-11-12T08:59:59.999Z')).toISOString(),
).toBe('2019-11-12T08:00:00.000Z');
expect(
roundDownToHour(new Date('2019-11-12T09:01:00.999Z')).toISOString(),
).toBe('2019-11-12T09:00:00.000Z');
});