diff --git a/src/lib/db/client-metrics-store-v2.test.ts b/src/lib/db/client-metrics-store-v2.test.ts index 120f64ad17e2..234055c489fa 100644 --- a/src/lib/db/client-metrics-store-v2.test.ts +++ b/src/lib/db/client-metrics-store-v2.test.ts @@ -17,6 +17,11 @@ afterAll(async () => { await db.destroy(); }); +beforeEach(async () => { + await clientMetricsStore.clearMetrics(0); + await clientMetricsStore.clearDailyMetrics(0); +}); + test('aggregate daily metrics from previous day', async () => { const yesterday = subDays(new Date(), 1); await clientMetricsStore.batchInsertMetrics([ @@ -84,3 +89,46 @@ test('aggregate daily metrics from previous day', async () => { }, ]); }); + +test('clear daily metrics', async () => { + const yesterday = subDays(new Date(), 1); + const twoDaysAgo = subDays(new Date(), 2); + await clientMetricsStore.batchInsertMetrics([ + { + appName: 'irrelevant', + featureName: 'irrelevant', + environment: 'irrelevant', + timestamp: yesterday, + no: 0, + yes: 1, + variants: { + a: 0, + b: 1, + }, + }, + { + appName: 'irrelevant', + featureName: 'irrelevant', + environment: 'irrelevant', + timestamp: twoDaysAgo, + no: 0, + yes: 2, + variants: { + a: 1, + b: 1, + }, + }, + ]); + await clientMetricsStore.aggregateDailyMetrics(); + + await clientMetricsStore.clearDailyMetrics(2); + + const results = await db.rawDatabase + .table('client_metrics_env_daily') + .select('*'); + expect(results.length).toBe(1); + const variantResults = await db.rawDatabase + .table('client_metrics_env_variants_daily') + .select('*'); + expect(variantResults.length).toBe(2); +}); diff --git a/src/lib/db/client-metrics-store-v2.ts b/src/lib/db/client-metrics-store-v2.ts index 9607a10129b6..81035de947d0 100644 --- a/src/lib/db/client-metrics-store-v2.ts +++ b/src/lib/db/client-metrics-store-v2.ts @@ -256,6 +256,12 @@ export class ClientMetricsStoreV2 implements IClientMetricsStoreV2 { .del(); } + async clearDailyMetrics(daysAgo: number): Promise { + return this.db(DAILY_TABLE) + .whereRaw(`date <= CURRENT_DATE - INTERVAL '${daysAgo} days'`) + .del(); + } + // aggregates all hourly metrics from a previous day into daily metrics async aggregateDailyMetrics(): Promise { const rawQuery: string = ` diff --git a/src/lib/features/scheduler/schedule-services.ts b/src/lib/features/scheduler/schedule-services.ts index 4546c019405d..7e6fad7dcb45 100644 --- a/src/lib/features/scheduler/schedule-services.ts +++ b/src/lib/features/scheduler/schedule-services.ts @@ -145,6 +145,14 @@ export const scheduleServices = async ( 'clearMetrics', ); + schedulerService.schedule( + () => { + clientMetricsServiceV2.clearDailyMetrics(92).catch(console.error); + }, + hoursToMilliseconds(24), + 'clearDailyMetrics', + ); + schedulerService.schedule( () => { clientMetricsServiceV2.aggregateDailyMetrics().catch(console.error); diff --git a/src/lib/services/client-metrics/metrics-service-v2.ts b/src/lib/services/client-metrics/metrics-service-v2.ts index a1ac2b0e636e..f4a7e7203401 100644 --- a/src/lib/services/client-metrics/metrics-service-v2.ts +++ b/src/lib/services/client-metrics/metrics-service-v2.ts @@ -49,6 +49,12 @@ export default class ClientMetricsServiceV2 { return this.clientMetricsStoreV2.clearMetrics(hoursAgo); } + async clearDailyMetrics(daysAgo: number) { + if (this.flagResolver.isEnabled('extendedUsageMetrics')) { + return this.clientMetricsStoreV2.clearDailyMetrics(daysAgo); + } + } + async aggregateDailyMetrics() { if (this.flagResolver.isEnabled('extendedUsageMetrics')) { await this.clientMetricsStoreV2.aggregateDailyMetrics(); diff --git a/src/lib/types/stores/client-metrics-store-v2.ts b/src/lib/types/stores/client-metrics-store-v2.ts index 6bda292bb59f..ebe75d54b6cc 100644 --- a/src/lib/types/stores/client-metrics-store-v2.ts +++ b/src/lib/types/stores/client-metrics-store-v2.ts @@ -34,5 +34,6 @@ export interface IClientMetricsStoreV2 hoursBack?: number, ): Promise; clearMetrics(hoursAgo: number): Promise; + clearDailyMetrics(daysAgo: number): Promise; aggregateDailyMetrics(): Promise; } diff --git a/src/test/fixtures/fake-client-metrics-store-v2.ts b/src/test/fixtures/fake-client-metrics-store-v2.ts index fd2c080a77df..3484dd10d642 100644 --- a/src/test/fixtures/fake-client-metrics-store-v2.ts +++ b/src/test/fixtures/fake-client-metrics-store-v2.ts @@ -26,6 +26,9 @@ export default class FakeClientMetricsStoreV2 clearMetrics(hoursBack: number): Promise { return Promise.resolve(); } + clearDailyMetrics(daysBack: number): Promise { + return Promise.resolve(); + } aggregateDailyMetrics(): Promise { return Promise.resolve(); }