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-01-25 22:06:56 +00:00
|
|
|
|
2022-02-01 20:45:01 +00:00
|
|
|
export default class MonthlySerializer extends ApplicationSerializer {
|
2022-01-25 22:06:56 +00:00
|
|
|
flattenDataset(payload) {
|
2022-01-27 18:59:08 +00:00
|
|
|
let topTen = payload ? payload.slice(0, 10) : [];
|
2022-01-25 22:06:56 +00:00
|
|
|
|
|
|
|
return topTen.map((ns) => {
|
|
|
|
// '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]));
|
|
|
|
|
|
|
|
// homogenize client naming for all namespaces
|
|
|
|
if (Object.keys(flattenedNs).includes('distinct_entities', 'non_entity_tokens')) {
|
|
|
|
flattenedNs.entity_clients = flattenedNs.distinct_entities;
|
|
|
|
flattenedNs.non_entity_clients = flattenedNs.non_entity_tokens;
|
|
|
|
delete flattenedNs.distinct_entities;
|
|
|
|
delete flattenedNs.non_entity_tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if mounts attribution unavailable, mounts will be undefined
|
|
|
|
flattenedNs.mounts = ns.mounts?.map((mount) => {
|
|
|
|
let flattenedMount = {};
|
|
|
|
flattenedMount.label = mount['path'];
|
|
|
|
Object.keys(mount['counts']).forEach((key) => (flattenedMount[key] = mount['counts'][key]));
|
|
|
|
return flattenedMount;
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
label,
|
|
|
|
...flattenedNs,
|
|
|
|
};
|
|
|
|
});
|
2022-02-01 20:45:01 +00:00
|
|
|
}
|
2022-01-25 22:06:56 +00:00
|
|
|
|
|
|
|
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
|
|
|
let { data } = payload;
|
|
|
|
let { clients, distinct_entities, non_entity_tokens } = data;
|
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-01-25 22:06:56 +00:00
|
|
|
by_namespace: this.flattenDataset(data.by_namespace),
|
2022-01-27 18:59:08 +00:00
|
|
|
// nest within 'total' object to mimic /activity response shape
|
2022-01-25 22:06:56 +00:00
|
|
|
total: {
|
|
|
|
clients,
|
2022-01-27 18:59:08 +00:00
|
|
|
entityClients: distinct_entities,
|
|
|
|
nonEntityClients: non_entity_tokens,
|
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);
|
|
|
|
}
|
|
|
|
}
|