Commit 0257dbda authored by Jose Ivan Vargas Lopez's avatar Jose Ivan Vargas Lopez

Merge branch 'refactor-monitoring-store' into 'additional-time-series-charts'

Refactor MonitoringStore class

See merge request !13999
parents ce06927b bfc9b3b1
import _ from 'underscore'; import _ from 'underscore';
class MonitoringStore { function sortMetrics(metrics) {
constructor() { return _.chain(metrics).sortBy('weight').sortBy('title').value();
this.groups = []; }
this.deploymentData = [];
}
// eslint-disable-next-line class-methods-use-this function normalizeMetrics(metrics) {
createArrayRows(metrics = []) { return metrics.map(metric => ({
const currentMetrics = metrics; ...metric,
const availableMetrics = []; queries: metric.queries.map(query => ({
let metricsRow = []; ...query,
let index = 1; result: query.result.map(result => ({
Object.keys(currentMetrics).forEach((key) => { ...result,
const metricValues = currentMetrics[key].queries[0].result; values: result.values.map(([timestamp, value]) => ({
if (metricValues != null) { time: new Date(timestamp * 1000),
currentMetrics[key].queries[0].result = metricValues.map((series) => { value,
let convertedValues = []; })),
if (series != null) { })),
convertedValues = series.values.map(metric => ({ })),
time: new Date(metric[0] * 1000),
value: metric[1],
})); }));
} }
return {
metric: series.metric, function collate(array, rows = 2) {
values: convertedValues, const collatedArray = [];
}; let row = [];
}); array.forEach((value, index) => {
metricsRow.push(currentMetrics[key]); row.push(value);
if (index % 2 === 0) { if ((index + 1) % rows === 0) {
availableMetrics.push(metricsRow); collatedArray.push(row);
metricsRow = []; row = [];
}
index = index += 1;
} }
}); });
if (metricsRow.length > 0) { if (row.length > 0) {
availableMetrics.push(metricsRow); collatedArray.push(row);
} }
return availableMetrics; return collatedArray;
}
export default class MonitoringStore {
constructor() {
this.groups = [];
this.deploymentData = [];
} }
storeMetrics(groups = []) { storeMetrics(groups = []) {
this.groups = groups.map((group) => { this.groups = groups.map(group => ({
const currentGroup = group; ...group,
currentGroup.metrics = _.chain(group.metrics).sortBy('weight').sortBy('title').value(); metrics: collate(normalizeMetrics(sortMetrics(group.metrics))),
currentGroup.metrics = this.createArrayRows(currentGroup.metrics); }));
return currentGroup;
});
} }
storeDeploymentData(deploymentData = []) { storeDeploymentData(deploymentData = []) {
...@@ -65,5 +63,3 @@ class MonitoringStore { ...@@ -65,5 +63,3 @@ class MonitoringStore {
return metricsCount; return metricsCount;
} }
} }
export default MonitoringStore;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment