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

fix: add getSeenAppsForFeatureToggle to client-store-v2

This commit is contained in:
Ivar Conradi Østhus 2021-10-07 11:30:40 +02:00
parent 944e74af52
commit e62ad931f0
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
3 changed files with 50 additions and 0 deletions

View File

@ -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<string[]> {
return this.db<ClientMetricsEnvTable>(TABLE)
.distinct()
.where({ feature_name: featureName })
.andWhereRaw(`timestamp >= NOW() - INTERVAL '${hoursBack} hours'`)
.pluck('app_name')
.orderBy('app_name');
}
}

View File

@ -19,4 +19,8 @@ export interface IClientMetricsStoreV2
featureName: string,
hoursBack?: number,
): Promise<IClientMetricsEnv[]>;
getSeenAppsForFeatureToggle(
featureName: string,
hoursBack?: number,
): Promise<string[]>;
}

View File

@ -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']);
});