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-04-15 19:06:10 +00:00
|
|
|
|
2022-02-01 20:45:01 +00:00
|
|
|
export default class MonthlySerializer extends ApplicationSerializer {
|
2022-04-15 19:06:10 +00:00
|
|
|
flattenDataset(namespaceArray) {
|
|
|
|
return namespaceArray?.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
|
|
|
// if no mounts, mounts will be an empty array
|
|
|
|
flattenedNs.mounts = ns.mounts
|
|
|
|
? ns.mounts.map((mount) => {
|
|
|
|
let flattenedMount = {};
|
2022-04-15 19:06:10 +00:00
|
|
|
let label = mount['mount_path'];
|
2022-02-02 21:24:24 +00:00
|
|
|
Object.keys(mount['counts']).forEach((key) => (flattenedMount[key] = mount['counts'][key]));
|
2022-04-15 19:06:10 +00:00
|
|
|
flattenedMount = this.homogenizeClientNaming(flattenedMount);
|
|
|
|
return {
|
|
|
|
label,
|
|
|
|
...flattenedMount,
|
|
|
|
};
|
2022-02-02 21:24:24 +00:00
|
|
|
})
|
|
|
|
: [];
|
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-04-15 19:06:10 +00:00
|
|
|
// In 1.10 'distinct_entities' changed to 'entity_clients' and
|
2022-02-02 19:46:59 +00:00
|
|
|
// 'non_entity_tokens' to 'non_entity_clients'
|
|
|
|
homogenizeClientNaming(object) {
|
2022-04-15 19:06:10 +00:00
|
|
|
// if new key names exist, only return those key/value pairs
|
|
|
|
if (Object.keys(object).includes('entity_clients')) {
|
|
|
|
let { clients, entity_clients, non_entity_clients } = object;
|
2022-02-02 19:46:59 +00:00
|
|
|
return {
|
|
|
|
clients,
|
|
|
|
entity_clients,
|
|
|
|
non_entity_clients,
|
|
|
|
};
|
|
|
|
}
|
2022-04-15 19:06:10 +00:00
|
|
|
// if object only has outdated key names, update naming
|
|
|
|
if (Object.keys(object).includes('distinct_entities')) {
|
|
|
|
let { clients, distinct_entities, non_entity_tokens } = object;
|
|
|
|
return {
|
|
|
|
clients,
|
|
|
|
entity_clients: distinct_entities,
|
|
|
|
non_entity_clients: non_entity_tokens,
|
|
|
|
};
|
|
|
|
}
|
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-04-15 19:06:10 +00:00
|
|
|
// TODO CMB: the following is assumed, need to confirm
|
|
|
|
// the months array will always include a single object: a timestamp of the current month and new/total count data, if available
|
|
|
|
let newClientsData = payload.data.months[0]?.new_clients || null;
|
|
|
|
let by_namespace_new_clients, new_clients;
|
|
|
|
if (newClientsData) {
|
|
|
|
by_namespace_new_clients = this.flattenDataset(newClientsData.namespaces);
|
|
|
|
new_clients = this.homogenizeClientNaming(newClientsData.counts);
|
|
|
|
} else {
|
|
|
|
by_namespace_new_clients = [];
|
|
|
|
new_clients = [];
|
|
|
|
}
|
2022-01-25 22:06:56 +00:00
|
|
|
let transformedPayload = {
|
|
|
|
...payload,
|
2022-01-27 18:59:08 +00:00
|
|
|
response_timestamp,
|
2022-04-15 19:06:10 +00:00
|
|
|
by_namespace_total_clients: this.flattenDataset(payload.data.by_namespace),
|
|
|
|
by_namespace_new_clients,
|
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-04-15 19:06:10 +00:00
|
|
|
new: new_clients,
|
2022-01-25 22:06:56 +00:00
|
|
|
};
|
|
|
|
delete payload.data.by_namespace;
|
2022-04-15 19:06:10 +00:00
|
|
|
delete payload.data.months;
|
|
|
|
delete payload.data.total;
|
2022-02-01 20:45:01 +00:00
|
|
|
return super.normalizeResponse(store, primaryModelClass, transformedPayload, id, requestType);
|
|
|
|
}
|
|
|
|
}
|