From 7044522670187bff1b1df148ec2c28344c216106 Mon Sep 17 00:00:00 2001 From: sveisvei Date: Fri, 25 Jan 2019 13:05:25 +0100 Subject: [PATCH] fix: Gracefully handle variant metrics --- lib/client-metrics/index.js | 15 +++++++++++-- lib/routes/client-api/metrics-schema.js | 6 +++-- lib/routes/client-api/metrics.test.js | 30 +++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/lib/client-metrics/index.js b/lib/client-metrics/index.js index 7707731e7f..903f0a5b67 100644 --- a/lib/client-metrics/index.js +++ b/lib/client-metrics/index.js @@ -92,8 +92,19 @@ module.exports = class UnleashClientMetrics { } createCountObject(entry) { - const yes = typeof entry.yes == 'number' ? entry.yes : 0; - const no = typeof entry.no == 'number' ? entry.no : 0; + let yes = typeof entry.yes == 'number' ? entry.yes : 0; + let no = typeof entry.no == 'number' ? entry.no : 0; + + if (entry.variants) { + Object.entries(entry.variants).forEach(([key, value]) => { + if (key === 'disabled') { + no += value; + } else { + yes += value; + } + }); + } + return { yes, no }; } diff --git a/lib/routes/client-api/metrics-schema.js b/lib/routes/client-api/metrics-schema.js index 3b7fe26bea..37b5811714 100644 --- a/lib/routes/client-api/metrics-schema.js +++ b/lib/routes/client-api/metrics-schema.js @@ -8,14 +8,16 @@ const countSchema = joi .keys({ yes: joi .number() - .required() .min(0) .default(0), no: joi .number() - .required() .min(0) .default(0), + variants: joi.object({ + arg: joi.string(), + value: joi.number().min(0), + }), }); const clientMetricsSchema = joi diff --git a/lib/routes/client-api/metrics.test.js b/lib/routes/client-api/metrics.test.js index 3328639ee5..bbb092aeb2 100644 --- a/lib/routes/client-api/metrics.test.js +++ b/lib/routes/client-api/metrics.test.js @@ -70,7 +70,33 @@ test('should accept client metrics with yes/no', t => { .expect(202); }); -test('should not accept client metrics without yes/no', t => { +test('should accept client metrics with variants', t => { + t.plan(0); + const { request } = getSetup(); + return request + .post('/api/client/metrics') + .send({ + appName: 'demo', + instanceId: '1', + bucket: { + start: Date.now(), + stop: Date.now(), + toggles: { + toggleA: { + yes: 200, + no: 0, + variants: { + variant1: 1, + variant2: 2, + }, + }, + }, + }, + }) + .expect(202); +}); + +test('should accept client metrics without yes/no', t => { t.plan(0); const { request } = getSetup(); return request @@ -89,5 +115,5 @@ test('should not accept client metrics without yes/no', t => { }, }, }) - .expect(400); + .expect(202); });