From e62ad931f0a1749f0707e3f2b927175869882ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 7 Oct 2021 11:30:40 +0200 Subject: [PATCH] fix: add getSeenAppsForFeatureToggle to client-store-v2 --- src/lib/db/client-metrics-store-v2.ts | 12 +++++++ .../types/stores/client-metrics-store-v2.ts | 4 +++ .../client-metrics-store-v2.e2e.test.ts | 34 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/lib/db/client-metrics-store-v2.ts b/src/lib/db/client-metrics-store-v2.ts index 6318448760..bea2fdfd04 100644 --- a/src/lib/db/client-metrics-store-v2.ts +++ b/src/lib/db/client-metrics-store-v2.ts @@ -114,4 +114,16 @@ export class ClientMetricsStoreV2 implements IClientMetricsStoreV2 { .andWhereRaw(`timestamp >= NOW() - INTERVAL '${hoursBack} hours'`); return rows.map(fromRow); } + + async getSeenAppsForFeatureToggle( + featureName: string, + hoursBack: number = 24, + ): Promise { + return this.db(TABLE) + .distinct() + .where({ feature_name: featureName }) + .andWhereRaw(`timestamp >= NOW() - INTERVAL '${hoursBack} hours'`) + .pluck('app_name') + .orderBy('app_name'); + } } diff --git a/src/lib/types/stores/client-metrics-store-v2.ts b/src/lib/types/stores/client-metrics-store-v2.ts index 18ecba95f7..c64cca4731 100644 --- a/src/lib/types/stores/client-metrics-store-v2.ts +++ b/src/lib/types/stores/client-metrics-store-v2.ts @@ -19,4 +19,8 @@ export interface IClientMetricsStoreV2 featureName: string, hoursBack?: number, ): Promise; + getSeenAppsForFeatureToggle( + featureName: string, + hoursBack?: number, + ): Promise; } diff --git a/src/test/e2e/stores/client-metrics-store-v2.e2e.test.ts b/src/test/e2e/stores/client-metrics-store-v2.e2e.test.ts index 746ebbed02..b1c618aa4a 100644 --- a/src/test/e2e/stores/client-metrics-store-v2.e2e.test.ts +++ b/src/test/e2e/stores/client-metrics-store-v2.e2e.test.ts @@ -214,3 +214,37 @@ test('Should insert 1500 feature toggle metrics', async () => { expect(savedMetrics).toHaveLength(1500); }); + +test('Should return seen applications using a feature toggle', async () => { + const metrics: IClientMetricsEnv[] = [ + { + featureName: 'demo', + appName: 'web', + environment: 'dev', + timestamp: new Date(), + yes: 2, + no: 2, + }, + { + featureName: 'demo', + appName: 'backend-api', + environment: 'dev', + timestamp: new Date(), + yes: 1, + no: 3, + }, + { + featureName: 'demo', + appName: 'backend-api', + environment: 'dev', + timestamp: new Date(), + yes: 1, + no: 3, + }, + ]; + await clientMetricsStore.batchInsertMetrics(metrics); + const apps = await clientMetricsStore.getSeenAppsForFeatureToggle('demo'); + + expect(apps).toHaveLength(2); + expect(apps).toStrictEqual(['backend-api', 'web']); +});