-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
activity.js
157 lines (150 loc) · 5.06 KB
/
activity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import ApplicationSerializer from '../application';
import { formatISO } from 'date-fns';
import { parseAPITimestamp, parseRFC3339 } from 'core/utils/date-formatters';
export default class ActivitySerializer extends ApplicationSerializer {
flattenDataset(namespaceArray) {
return namespaceArray.map((ns) => {
// '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);
// if no mounts, mounts will be an empty array
flattenedNs.mounts = ns.mounts
? ns.mounts.map((mount) => {
let flattenedMount = {};
let label = mount['mount_path'];
Object.keys(mount['counts']).forEach((key) => (flattenedMount[key] = mount['counts'][key]));
flattenedMount = this.homogenizeClientNaming(flattenedMount);
return {
label,
...flattenedMount,
};
})
: [];
return {
label,
...flattenedNs,
};
});
}
flattenByMonths(payload, isNewClients = false) {
const sortedPayload = [...payload];
sortedPayload.reverse();
if (isNewClients) {
return sortedPayload?.map((m) => {
return {
month: parseAPITimestamp(m.timestamp, 'M/yy'),
entity_clients: m.new_clients.counts.entity_clients,
non_entity_clients: m.new_clients.counts.non_entity_clients,
clients: m.new_clients.counts.clients,
namespaces: this.flattenDataset(m.new_clients.namespaces),
};
});
} else {
return sortedPayload?.map((m) => {
return {
month: parseAPITimestamp(m.timestamp, 'M/yy'),
entity_clients: m.counts.entity_clients,
non_entity_clients: m.counts.non_entity_clients,
clients: 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,
clients: 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'
homogenizeClientNaming(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;
}
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),
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: parseRFC3339(payload.data.end_time),
formatted_start_time: 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);
}
}
/*
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: [
{
mount_path: 'auth/method/uMGBU',
"counts":{
"distinct_entities":0,
"entity_clients":0,
"non_entity_tokens":0,
"non_entity_clients":10,
"clients":10
}
},
],
},
];
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,
},
],
},
]
*/