2020-10-01 19:37:33 +00:00
|
|
|
import ApplicationSerializer from '../application';
|
2022-01-27 18:59:08 +00:00
|
|
|
import { formatISO } from 'date-fns';
|
2022-02-02 19:46:59 +00:00
|
|
|
export default class ActivitySerializer extends ApplicationSerializer {
|
|
|
|
flattenDataset(byNamespaceArray) {
|
2022-02-02 21:24:24 +00:00
|
|
|
return byNamespaceArray.map((ns) => {
|
2022-02-02 19:46:59 +00:00
|
|
|
// 'namespace_path' is an empty string for root
|
|
|
|
if (ns['namespace_id'] === 'root') ns['namespace_path'] = 'root';
|
|
|
|
let label = ns['namespace_path'];
|
|
|
|
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]));
|
|
|
|
flattenedNs = this.homogenizeClientNaming(flattenedNs);
|
|
|
|
|
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 = {};
|
2022-02-22 21:24:33 +00:00
|
|
|
flattenedMount.label = mount['mount_path'];
|
2022-02-02 21:24:24 +00:00
|
|
|
Object.keys(mount['counts']).forEach((key) => (flattenedMount[key] = mount['counts'][key]));
|
|
|
|
return flattenedMount;
|
|
|
|
})
|
|
|
|
: [];
|
|
|
|
|
2022-02-02 19:46:59 +00:00
|
|
|
return {
|
|
|
|
label,
|
|
|
|
...flattenedNs,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-02-08 21:07:04 +00:00
|
|
|
parseRFC3339(timestamp) {
|
|
|
|
// convert '2021-03-21T00:00:00Z' --> ['2021', 2] (e.g. 2021 March, month is zero indexed)
|
2022-02-02 19:46:59 +00:00
|
|
|
return timestamp
|
|
|
|
? [timestamp.split('-')[0], Number(timestamp.split('-')[1].replace(/^0+/, '')) - 1]
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
|
|
|
if (payload.id === 'no-data') {
|
|
|
|
return super.normalizeResponse(store, primaryModelClass, payload, id, requestType);
|
|
|
|
}
|
|
|
|
let response_timestamp = formatISO(new Date());
|
|
|
|
let transformedPayload = {
|
|
|
|
...payload,
|
|
|
|
response_timestamp,
|
|
|
|
by_namespace: this.flattenDataset(payload.data.by_namespace),
|
|
|
|
total: this.homogenizeClientNaming(payload.data.total),
|
2022-02-08 21:07:04 +00:00
|
|
|
formatted_end_time: this.parseRFC3339(payload.data.end_time),
|
|
|
|
formatted_start_time: this.parseRFC3339(payload.data.start_time),
|
2022-02-02 19:46:59 +00:00
|
|
|
};
|
|
|
|
delete payload.data.by_namespace;
|
|
|
|
delete payload.data.total;
|
|
|
|
return super.normalizeResponse(store, primaryModelClass, transformedPayload, id, requestType);
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 19:37:33 +00:00
|
|
|
|
2022-01-25 22:06:56 +00:00
|
|
|
/*
|
|
|
|
SAMPLE PAYLOAD BEFORE/AFTER:
|
|
|
|
|
|
|
|
payload.data.by_namespace = [
|
|
|
|
{
|
|
|
|
namespace_id: '5SWT8',
|
|
|
|
namespace_path: 'namespacelonglonglong4/',
|
|
|
|
counts: {
|
|
|
|
entity_clients: 171,
|
|
|
|
non_entity_clients: 20,
|
|
|
|
clients: 191,
|
|
|
|
},
|
|
|
|
mounts: [
|
|
|
|
{
|
2022-02-22 21:24:33 +00:00
|
|
|
mount_path: 'auth/method/uMGBU',
|
|
|
|
"counts":{
|
|
|
|
"distinct_entities":0,
|
|
|
|
"entity_clients":0,
|
|
|
|
"non_entity_tokens":0,
|
|
|
|
"non_entity_clients":10,
|
|
|
|
"clients":10
|
|
|
|
}
|
2022-01-25 22:06:56 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
transformedPayload.by_namespace = [
|
|
|
|
{
|
|
|
|
label: 'namespacelonglonglong4/',
|
|
|
|
entity_clients: 171,
|
|
|
|
non_entity_clients: 20,
|
|
|
|
clients: 191,
|
|
|
|
mounts: [
|
|
|
|
{
|
|
|
|
label: 'auth/method/uMGBU',
|
|
|
|
entity_clients: 20,
|
|
|
|
non_entity_clients: 15,
|
|
|
|
clients: 35,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
*/
|