From 18b224e89a1b6b13d514e04f2e83f474b86d9407 Mon Sep 17 00:00:00 2001 From: Timothy Sullivan Date: Fri, 20 Jul 2018 14:55:49 -0700 Subject: [PATCH] Stats API: do not convert arrays to objects --- .../usage/classes/__tests__/collector_set.js | 36 +++++++++++++++++++ src/server/usage/classes/collector_set.js | 32 +++++++++-------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/server/usage/classes/__tests__/collector_set.js b/src/server/usage/classes/__tests__/collector_set.js index b99ec51c42acf..de50a4fcccba8 100644 --- a/src/server/usage/classes/__tests__/collector_set.js +++ b/src/server/usage/classes/__tests__/collector_set.js @@ -70,4 +70,40 @@ describe('CollectorSet', () => { }]); }); }); + + describe('toApiFieldNames', () => { + it('should snake_case and convert field names to api standards', () => { + const apiData = { + os: { + load: { + '15m': 2.3525390625, + '1m': 2.22412109375, + '5m': 2.4462890625 + }, + memory: { + free_in_bytes: 458280960, + total_in_bytes: 17179869184, + used_in_bytes: 16721588224 + }, + uptime_in_millis: 137844000 + }, + daysOfTheWeek: [ + 'monday', + 'tuesday', + 'wednesday', + ] + }; + + const collectors = new CollectorSet(); + const result = collectors.toApiFieldNames(apiData); + expect(result).to.eql({ + os: { + load: { '15m': 2.3525390625, '1m': 2.22412109375, '5m': 2.4462890625 }, + memory: { free_bytes: 458280960, total_bytes: 17179869184, used_bytes: 16721588224 }, + uptime_ms: 137844000, + }, + days_of_the_week: ['monday', 'tuesday', 'wednesday'], + }); + }); + }); }); diff --git a/src/server/usage/classes/collector_set.js b/src/server/usage/classes/collector_set.js index 91834565bfab3..4d19b32fc2523 100644 --- a/src/server/usage/classes/collector_set.js +++ b/src/server/usage/classes/collector_set.js @@ -117,19 +117,23 @@ export class CollectorSet { } }; - return Object.keys(apiData).reduce((accum, currName) => { - const value = apiData[currName]; - - let newName = currName; - newName = snakeCase(newName); - newName = newName.replace(/^(1|5|15)_m/, '$1m'); // os.load.15m, os.load.5m, os.load.1m - newName = newName.replace('_in_bytes', '_bytes'); - newName = newName.replace('_in_millis', '_ms'); - - return { - ...accum, - [newName]: getValueOrRecurse(value), - }; - }, {}); + if (!Array.isArray(apiData)) { + return Object.keys(apiData).reduce((accum, field) => { + const value = apiData[field]; + let newName = field; + + newName = snakeCase(newName); + newName = newName.replace(/^(1|5|15)_m/, '$1m'); // os.load.15m, os.load.5m, os.load.1m + newName = newName.replace('_in_bytes', '_bytes'); + newName = newName.replace('_in_millis', '_ms'); + + return { + ...accum, + [newName]: getValueOrRecurse(value), + }; + }, {}); + } else { + return apiData; + } } }