2021-09-16 22:28:03 +00:00
|
|
|
import Component from '@glimmer/component';
|
2021-09-22 19:50:59 +00:00
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
2021-10-28 22:05:55 +00:00
|
|
|
import { format } from 'date-fns';
|
2021-09-16 22:28:03 +00:00
|
|
|
|
|
|
|
export default class HistoryComponent extends Component {
|
|
|
|
max_namespaces = 10;
|
|
|
|
|
2021-09-22 19:50:59 +00:00
|
|
|
@tracked selectedNamespace = null;
|
|
|
|
|
|
|
|
// Determine if we have client count data based on the current tab,
|
|
|
|
// since model is slightly different for current month vs history api
|
2021-09-16 22:28:03 +00:00
|
|
|
get hasClientData() {
|
|
|
|
if (this.args.tab === 'current') {
|
|
|
|
return this.args.model.activity && this.args.model.activity.clients;
|
|
|
|
}
|
|
|
|
return this.args.model.activity && this.args.model.activity.total;
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:50:59 +00:00
|
|
|
// Show namespace graph only if we have more than 1
|
|
|
|
get showGraphs() {
|
|
|
|
return (
|
|
|
|
this.args.model.activity &&
|
|
|
|
this.args.model.activity.byNamespace &&
|
|
|
|
this.args.model.activity.byNamespace.length > 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the namespace model for the search select component
|
|
|
|
get searchDataset() {
|
2021-09-16 22:28:03 +00:00
|
|
|
if (!this.args.model.activity || !this.args.model.activity.byNamespace) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-22 19:50:59 +00:00
|
|
|
let dataList = this.args.model.activity.byNamespace;
|
|
|
|
return dataList.map(d => {
|
|
|
|
return {
|
|
|
|
name: d['namespace_id'],
|
|
|
|
id: d['namespace_path'] === '' ? 'root' : d['namespace_path'],
|
|
|
|
};
|
2021-09-16 22:28:03 +00:00
|
|
|
});
|
2021-09-22 19:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the namespace model for the bar chart component
|
|
|
|
get barChartDataset() {
|
|
|
|
if (!this.args.model.activity || !this.args.model.activity.byNamespace) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let dataset = this.args.model.activity.byNamespace.slice(0, this.max_namespaces);
|
2021-09-16 22:28:03 +00:00
|
|
|
return dataset.map(d => {
|
|
|
|
return {
|
2021-09-22 19:50:59 +00:00
|
|
|
label: d['namespace_path'] === '' ? 'root' : d['namespace_path'],
|
2021-09-16 22:28:03 +00:00
|
|
|
non_entity_tokens: d['counts']['non_entity_tokens'],
|
|
|
|
distinct_entities: d['counts']['distinct_entities'],
|
|
|
|
total: d['counts']['clients'],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:50:59 +00:00
|
|
|
// Create namespaces data for csv format
|
|
|
|
get getCsvData() {
|
2021-09-16 22:28:03 +00:00
|
|
|
if (!this.args.model.activity || !this.args.model.activity.byNamespace) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-22 19:50:59 +00:00
|
|
|
let results = '',
|
|
|
|
namespaces = this.args.model.activity.byNamespace,
|
|
|
|
fields = ['Namespace path', 'Active clients', 'Unique entities', 'Non-entity tokens'];
|
|
|
|
|
|
|
|
results = fields.join(',') + '\n';
|
|
|
|
|
|
|
|
namespaces.forEach(function(item) {
|
|
|
|
let path = item.namespace_path !== '' ? item.namespace_path : 'root',
|
|
|
|
total = item.counts.clients,
|
|
|
|
unique = item.counts.distinct_entities,
|
|
|
|
non_entity = item.counts.non_entity_tokens;
|
|
|
|
|
|
|
|
results += path + ',' + total + ',' + unique + ',' + non_entity + '\n';
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:05:55 +00:00
|
|
|
// Return csv filename with start and end dates
|
|
|
|
get getCsvFileName() {
|
|
|
|
let defaultFileName = `clients-by-namespace`,
|
|
|
|
startDate =
|
|
|
|
this.args.model.queryStart || `${format(new Date(this.args.model.activity.startTime), 'MM-yyyy')}`,
|
|
|
|
endDate =
|
|
|
|
this.args.model.queryEnd || `${format(new Date(this.args.model.activity.endTime), 'MM-yyyy')}`;
|
|
|
|
if (startDate && endDate) {
|
|
|
|
defaultFileName += `-${startDate}-${endDate}`;
|
|
|
|
}
|
|
|
|
return defaultFileName;
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:50:59 +00:00
|
|
|
// Get the namespace by matching the path from the namespace list
|
|
|
|
getNamespace(path) {
|
|
|
|
return this.args.model.activity.byNamespace.find(ns => {
|
|
|
|
if (path === 'root') {
|
|
|
|
return ns.namespace_path === '';
|
|
|
|
}
|
|
|
|
return ns.namespace_path === path;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
selectNamespace(value) {
|
|
|
|
// In case of search select component, value returned is an array
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
this.selectedNamespace = this.getNamespace(value[0]);
|
|
|
|
} else if (typeof value === 'object') {
|
|
|
|
// While D3 bar selection returns an object
|
|
|
|
this.selectedNamespace = this.getNamespace(value.label);
|
|
|
|
} else {
|
|
|
|
this.selectedNamespace = null;
|
|
|
|
}
|
2021-09-16 22:28:03 +00:00
|
|
|
}
|
|
|
|
}
|