2022-01-25 22:06:56 +00:00
|
|
|
import ApplicationSerializer from '../application';
|
2022-01-27 18:59:08 +00:00
|
|
|
import { formatISO } from 'date-fns';
|
2022-02-01 20:45:01 +00:00
|
|
|
export default class MonthlySerializer extends ApplicationSerializer {
|
2022-02-02 19:46:59 +00:00
|
|
|
flattenDataset(byNamespaceArray) {
|
2022-02-02 21:24:24 +00:00
|
|
|
return byNamespaceArray.map((ns) => {
|
2022-01-25 22:06:56 +00:00
|
|
|
// 'namespace_path' is an empty string for root
|
|
|
|
if (ns['namespace_id'] === 'root') ns['namespace_path'] = 'root';
|
2022-01-27 18:59:08 +00:00
|
|
|
let label = ns['namespace_path'];
|
2022-01-25 22:06:56 +00:00
|
|
|
let flattenedNs = {};
|
|
|
|
// we don't want client counts nested within the 'counts' object for stacked charts
|
|
|
|
Object.keys(ns['counts']).forEach((key) => (flattenedNs[key] = ns['counts'][key]));
|
2022-02-02 19:46:59 +00:00
|
|
|
flattenedNs = this.homogenizeClientNaming(flattenedNs);
|
2022-01-25 22:06:56 +00:00
|
|
|
|
2022-02-02 21:24:24 +00:00
|
|
|
// TODO CMB check how this works with actual API endpoint
|
|
|
|
// if no mounts, mounts will be an empty array
|
|
|
|
flattenedNs.mounts = ns.mounts
|
|
|
|
? ns.mounts.map((mount) => {
|
|
|
|
let flattenedMount = {};
|
|
|
|
flattenedMount.label = mount['path'];
|
|
|
|
Object.keys(mount['counts']).forEach((key) => (flattenedMount[key] = mount['counts'][key]));
|
|
|
|
return flattenedMount;
|
|
|
|
})
|
|
|
|
: [];
|
2022-01-25 22:06:56 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
label,
|
|
|
|
...flattenedNs,
|
|
|
|
};
|
|
|
|
});
|
2022-02-01 20:45:01 +00:00
|
|
|
}
|
2022-01-25 22:06:56 +00:00
|
|
|
|
2022-02-02 19:46:59 +00:00
|
|
|
// For 1.10 release naming changed from 'distinct_entities' to 'entity_clients' and
|
|
|
|
// 'non_entity_tokens' to 'non_entity_clients'
|
|
|
|
// accounting for deprecated API keys here and updating to latest nomenclature
|
|
|
|
homogenizeClientNaming(object) {
|
|
|
|
// TODO CMB check with API payload, latest draft includes both new and old key names
|
2022-02-02 21:24:24 +00:00
|
|
|
// TODO CMB Delete old key names IF correct ones exist?
|
2022-02-02 19:46:59 +00:00
|
|
|
if (Object.keys(object).includes('distinct_entities', 'non_entity_tokens')) {
|
|
|
|
let entity_clients = object.distinct_entities;
|
|
|
|
let non_entity_clients = object.non_entity_tokens;
|
|
|
|
let { clients } = object;
|
|
|
|
return {
|
|
|
|
clients,
|
|
|
|
entity_clients,
|
|
|
|
non_entity_clients,
|
|
|
|
};
|
|
|
|
}
|
2022-02-02 21:24:24 +00:00
|
|
|
return object;
|
2022-02-02 19:46:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 22:06:56 +00:00
|
|
|
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
2022-02-08 21:07:04 +00:00
|
|
|
if (payload.id === 'no-data') {
|
|
|
|
return super.normalizeResponse(store, primaryModelClass, payload, id, requestType);
|
|
|
|
}
|
2022-01-27 18:59:08 +00:00
|
|
|
let response_timestamp = formatISO(new Date());
|
2022-01-25 22:06:56 +00:00
|
|
|
let transformedPayload = {
|
|
|
|
...payload,
|
2022-01-27 18:59:08 +00:00
|
|
|
response_timestamp,
|
2022-02-02 19:46:59 +00:00
|
|
|
by_namespace: this.flattenDataset(payload.data.by_namespace),
|
2022-01-27 18:59:08 +00:00
|
|
|
// nest within 'total' object to mimic /activity response shape
|
2022-02-02 19:46:59 +00:00
|
|
|
total: this.homogenizeClientNaming(payload.data),
|
2022-01-25 22:06:56 +00:00
|
|
|
};
|
|
|
|
delete payload.data.by_namespace;
|
2022-02-01 20:45:01 +00:00
|
|
|
return super.normalizeResponse(store, primaryModelClass, transformedPayload, id, requestType);
|
|
|
|
}
|
|
|
|
}
|