1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00

fix: Gracefully handle variant metrics

This commit is contained in:
sveisvei 2019-01-25 13:05:25 +01:00 committed by Ivar Conradi Østhus
parent b4e1a3138e
commit 7044522670
3 changed files with 45 additions and 6 deletions

View File

@ -92,8 +92,19 @@ module.exports = class UnleashClientMetrics {
} }
createCountObject(entry) { createCountObject(entry) {
const yes = typeof entry.yes == 'number' ? entry.yes : 0; let yes = typeof entry.yes == 'number' ? entry.yes : 0;
const no = typeof entry.no == 'number' ? entry.no : 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 }; return { yes, no };
} }

View File

@ -8,14 +8,16 @@ const countSchema = joi
.keys({ .keys({
yes: joi yes: joi
.number() .number()
.required()
.min(0) .min(0)
.default(0), .default(0),
no: joi no: joi
.number() .number()
.required()
.min(0) .min(0)
.default(0), .default(0),
variants: joi.object({
arg: joi.string(),
value: joi.number().min(0),
}),
}); });
const clientMetricsSchema = joi const clientMetricsSchema = joi

View File

@ -70,7 +70,33 @@ test('should accept client metrics with yes/no', t => {
.expect(202); .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); t.plan(0);
const { request } = getSetup(); const { request } = getSetup();
return request return request
@ -89,5 +115,5 @@ test('should not accept client metrics without yes/no', t => {
}, },
}, },
}) })
.expect(400); .expect(202);
}); });