Skip to content

Commit

Permalink
Stats API: do not convert arrays to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Jul 20, 2018
1 parent 0dbaf65 commit 18b224e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
36 changes: 36 additions & 0 deletions src/server/usage/classes/__tests__/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
});
});
});
32 changes: 18 additions & 14 deletions src/server/usage/classes/collector_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 18b224e

Please sign in to comment.