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:
parent
497552097c
commit
290a4c18bf
@ -1,5 +1,4 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||||
import util from 'util';
|
|
||||||
import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
import { Logger, LogProvider } from '../logger';
|
import { Logger, LogProvider } from '../logger';
|
||||||
import {
|
import {
|
||||||
@ -20,10 +19,8 @@ interface ClientMetricsEnvTable {
|
|||||||
|
|
||||||
const TABLE = 'client_metrics_env';
|
const TABLE = 'client_metrics_env';
|
||||||
|
|
||||||
// Unsure if this would be better be done by the service?
|
|
||||||
export function roundDownToHour(date: Date): Date {
|
export function roundDownToHour(date: Date): Date {
|
||||||
let p = 60 * 60 * 1000; // milliseconds in an hour
|
return new Date(date.getTime() - (date.getTime() % 3600000));
|
||||||
return new Date(Math.floor(date.getTime() / p) * p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const fromRow = (row: ClientMetricsEnvTable) => ({
|
const fromRow = (row: ClientMetricsEnvTable) => ({
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
IClientMetricsEnv,
|
IClientMetricsEnv,
|
||||||
IClientMetricsStoreV2,
|
IClientMetricsStoreV2,
|
||||||
} from '../../../lib/types/stores/client-metrics-store-v2';
|
} from '../../../lib/types/stores/client-metrics-store-v2';
|
||||||
|
import { roundDownToHour } from '../../../lib/db/client-metrics-store-v2';
|
||||||
|
|
||||||
let db;
|
let db;
|
||||||
let stores: IUnleashStores;
|
let stores: IUnleashStores;
|
||||||
@ -382,3 +383,15 @@ test('Should not exists after delete', async () => {
|
|||||||
const existAfter = await clientMetricsStore.exists(metric);
|
const existAfter = await clientMetricsStore.exists(metric);
|
||||||
expect(existAfter).toBe(false);
|
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');
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user