From fc35789982e872da33649d1941ded117bfedf1f2 Mon Sep 17 00:00:00 2001 From: Tim Sullivan Date: Mon, 23 Jul 2018 15:36:17 -0700 Subject: [PATCH] Stats API: do not convert arrays to objects (#21053) * Stats API: do not convert arrays to objects * handle nested arrays in toApiFieldNames * add early return --- .../usage/classes/__tests__/collector_set.js | 72 +++++++++++++++++++ src/server/usage/classes/collector_set.js | 11 ++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/server/usage/classes/__tests__/collector_set.js b/src/server/usage/classes/__tests__/collector_set.js index b99ec51c42acf..3d1a23b8dc5d6 100644 --- a/src/server/usage/classes/__tests__/collector_set.js +++ b/src/server/usage/classes/__tests__/collector_set.js @@ -28,6 +28,7 @@ describe('CollectorSet', () => { let server; let init; let fetch; + beforeEach(() => { server = { log: sinon.spy() }; init = noop; @@ -70,4 +71,75 @@ describe('CollectorSet', () => { }]); }); }); + + describe('toApiFieldNames', () => { + let collectorSet; + + beforeEach(() => { + collectorSet = new CollectorSet(); + }); + + 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 result = collectorSet.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'], + }); + }); + + it('should correct object key fields nested in arrays', () => { + const apiData = { + daysOfTheWeek: [ + { + dayName: 'monday', + dayIndex: 1 + }, + { + dayName: 'tuesday', + dayIndex: 2 + }, + { + dayName: 'wednesday', + dayIndex: 3 + } + ] + }; + + const result = collectorSet.toApiFieldNames(apiData); + expect(result).to.eql({ + days_of_the_week: [ + { day_index: 1, day_name: 'monday' }, + { day_index: 2, day_name: 'tuesday' }, + { day_index: 3, day_name: 'wednesday' }, + ], + }); + }); + }); }); + + diff --git a/src/server/usage/classes/collector_set.js b/src/server/usage/classes/collector_set.js index 91834565bfab3..2b6d3717cfdb9 100644 --- a/src/server/usage/classes/collector_set.js +++ b/src/server/usage/classes/collector_set.js @@ -117,10 +117,15 @@ export class CollectorSet { } }; - return Object.keys(apiData).reduce((accum, currName) => { - const value = apiData[currName]; + // handle array and return early, or return a reduced object - let newName = currName; + if (Array.isArray(apiData)) { + return apiData.map(getValueOrRecurse); + } + + 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');