Skip to content

Commit

Permalink
Stats API: do not convert arrays to objects (elastic#21053)
Browse files Browse the repository at this point in the history
* Stats API: do not convert arrays to objects

* handle nested arrays in toApiFieldNames

* add early return
  • Loading branch information
tsullivan committed Jul 23, 2018
1 parent cb39132 commit fc35789
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
72 changes: 72 additions & 0 deletions src/server/usage/classes/__tests__/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('CollectorSet', () => {
let server;
let init;
let fetch;

beforeEach(() => {
server = { log: sinon.spy() };
init = noop;
Expand Down Expand Up @@ -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' },
],
});
});
});
});


11 changes: 8 additions & 3 deletions src/server/usage/classes/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit fc35789

Please sign in to comment.