Skip to content

Commit

Permalink
UI/Add months to activity serializer (#14942)
Browse files Browse the repository at this point in the history
* add mock monthly data to mirage handler

* add months to serializer for activity response

* change selectors

Co-authored-by: Chelsea Shaw <[email protected]>

* clean up serializer

* please stop being flakey <3

Co-authored-by: Chelsea Shaw <[email protected]>
  • Loading branch information
hellobontempo and hashishaw authored Apr 7, 2022
1 parent 311910b commit 48b43e9
Show file tree
Hide file tree
Showing 6 changed files with 720 additions and 33 deletions.
6 changes: 4 additions & 2 deletions ui/app/models/clients/activity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Model, { attr } from '@ember-data/model';
export default class Activity extends Model {
@attr('string') responseTimestamp;
@attr('array') byMonthTotalClients;
@attr('array') byMonthNewClients;
@attr('array') byNamespace;
@attr('object') total;
@attr('array') formattedEndTime;
@attr('array') formattedStartTime;
@attr('string') startTime;
@attr('string') endTime;
@attr('object') total;
@attr('string') responseTimestamp;
}
65 changes: 54 additions & 11 deletions ui/app/serializers/clients/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ export default class ActivitySerializer extends ApplicationSerializer {
Object.keys(ns['counts']).forEach((key) => (flattenedNs[key] = ns['counts'][key]));
flattenedNs = this.homogenizeClientNaming(flattenedNs);

// 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['mount_path'];
let label = mount['mount_path'];
Object.keys(mount['counts']).forEach((key) => (flattenedMount[key] = mount['counts'][key]));
return flattenedMount;
flattenedMount = this.homogenizeClientNaming(flattenedMount);
return {
label,
...flattenedMount,
};
})
: [];

Expand All @@ -29,22 +32,59 @@ export default class ActivitySerializer extends ApplicationSerializer {
});
}

// For 1.10 release naming changed from 'distinct_entities' to 'entity_clients' and
// for vault usage - vertical bar chart
flattenByMonths(payload, isNewClients = false) {
if (isNewClients) {
return payload.map((m) => {
return {
month: m.timestamp,
entity_clients: m.new_clients.counts.entity_clients,
non_entity_clients: m.new_clients.counts.non_entity_clients,
total: m.new_clients.counts.clients,
namespaces: this.flattenDataset(m.new_clients.namespaces),
};
});
} else {
return payload.map((m) => {
return {
month: m.timestamp,
entity_clients: m.counts.entity_clients,
non_entity_clients: m.counts.non_entity_clients,
total: m.counts.clients,
namespaces: this.flattenDataset(m.namespaces),
new_clients: {
entity_clients: m.new_clients.counts.entity_clients,
non_entity_clients: m.new_clients.counts.non_entity_clients,
total: m.new_clients.counts.clients,
namespaces: this.flattenDataset(m.new_clients.namespaces),
},
};
});
}
}

// In 1.10 'distinct_entities' changed 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
// TODO CMB Delete old key names IF correct ones exist?
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;
// 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;
return {
clients,
entity_clients,
non_entity_clients,
};
}
// 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,
};
}
// TODO CMB: test what to return if neither key exists
return object;
}

Expand All @@ -64,11 +104,14 @@ export default class ActivitySerializer extends ApplicationSerializer {
...payload,
response_timestamp,
by_namespace: this.flattenDataset(payload.data.by_namespace),
by_month_total_clients: this.flattenByMonths(payload.data.months),
by_month_new_clients: this.flattenByMonths(payload.data.months, { isNewClients: true }),
total: this.homogenizeClientNaming(payload.data.total),
formatted_end_time: this.parseRFC3339(payload.data.end_time),
formatted_start_time: this.parseRFC3339(payload.data.start_time),
};
delete payload.data.by_namespace;
delete payload.data.months;
delete payload.data.total;
return super.normalizeResponse(store, primaryModelClass, transformedPayload, id, requestType);
}
Expand Down
Loading

0 comments on commit 48b43e9

Please sign in to comment.