Skip to content

Commit

Permalink
refactor: add an hoursBack query param to the raw metrics endpoint (#…
Browse files Browse the repository at this point in the history
…1373)

* refactor: add an hoursBack query param to the raw metrics endpoint

* refactor: explicitly return undefined

* refactor: make parseHoursBackQueryParam non-static

* refactor: add test for hoursBack query param

* refactor: improve arg name

* refactor: add a 1 hour test case
  • Loading branch information
olav committed Feb 17, 2022
1 parent 6d8f401 commit eb08ed0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/lib/routes/admin-api/client-metrics.ts
@@ -1,7 +1,7 @@
import { Request, Response } from 'express';
import Controller from '../controller';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import { IUnleashServices } from '../../types';
import { Logger } from '../../logger';
import ClientMetricsServiceV2 from '../../services/client-metrics/metrics-service-v2';

Expand All @@ -10,6 +10,10 @@ class ClientMetricsController extends Controller {

private metrics: ClientMetricsServiceV2;

private static HOURS_BACK_MIN = 1;

private static HOURS_BACK_MAX = 48;

constructor(
config: IUnleashConfig,
{
Expand All @@ -27,7 +31,11 @@ class ClientMetricsController extends Controller {

async getRawToggleMetrics(req: Request, res: Response): Promise<void> {
const { name } = req.params;
const data = await this.metrics.getClientMetricsForToggle(name);
const { hoursBack } = req.query;
const data = await this.metrics.getClientMetricsForToggle(
name,
this.parseHoursBackQueryParam(hoursBack),
);
res.json({
version: 1,
maturity: 'stable',
Expand All @@ -44,5 +52,21 @@ class ClientMetricsController extends Controller {
...data,
});
}

private parseHoursBackQueryParam(param: unknown): number | undefined {
if (typeof param !== 'string') {
return undefined;
}

const parsed = Number(param);

if (
parsed >= ClientMetricsController.HOURS_BACK_MIN &&
parsed <= ClientMetricsController.HOURS_BACK_MAX
) {
return parsed;
}
}
}

export default ClientMetricsController;
6 changes: 5 additions & 1 deletion src/lib/services/client-metrics/metrics-service-v2.ts
Expand Up @@ -114,8 +114,12 @@ export default class ClientMetricsServiceV2 {

async getClientMetricsForToggle(
toggleName: string,
hoursBack?: number,
): Promise<IClientMetricsEnv[]> {
return this.clientMetricsStoreV2.getMetricsForFeatureToggle(toggleName);
return this.clientMetricsStoreV2.getMetricsForFeatureToggle(
toggleName,
hoursBack,
);
}

destroy(): void {
Expand Down
54 changes: 54 additions & 0 deletions src/test/e2e/api/admin/client-metrics.e2e.test.ts
Expand Up @@ -95,6 +95,60 @@ test('should return raw metrics, aggregated on key', async () => {
expect(t2.data[0].no).toBe(104);
});

test('should support the hoursBack query param for raw metrics', async () => {
const date = new Date();
const metrics: IClientMetricsEnv[] = [
{
featureName: 'demo',
appName: 'web',
environment: 'default',
timestamp: date,
yes: 1,
no: 1,
},
{
featureName: 'demo',
appName: 'web',
environment: 'default',
timestamp: subHours(date, 12),
yes: 2,
no: 2,
},
{
featureName: 'demo',
appName: 'web',
environment: 'default',
timestamp: subHours(date, 32),
yes: 3,
no: 3,
},
];

await db.stores.clientMetricsStoreV2.batchInsertMetrics(metrics);

const fetchHoursBack = (hoursBack: number) => {
return app.request
.get(
`/api/admin/client-metrics/features/demo/raw?hoursBack=${hoursBack}`,
)
.expect('Content-Type', /json/)
.expect(200)
.then((res) => res.body);
};

const hours1 = await fetchHoursBack(1);
const hours24 = await fetchHoursBack(24);
const hours48 = await fetchHoursBack(48);
const hoursTooFew = await fetchHoursBack(-999);
const hoursTooMany = await fetchHoursBack(999);

expect(hours1.data).toHaveLength(1);
expect(hours24.data).toHaveLength(2);
expect(hours48.data).toHaveLength(3);
expect(hoursTooFew.data).toHaveLength(2);
expect(hoursTooMany.data).toHaveLength(2);
});

test('should return toggle summary', async () => {
const date = new Date();
const metrics: IClientMetricsEnv[] = [
Expand Down

0 comments on commit eb08ed0

Please sign in to comment.