Commit f69ad389 authored by Mike Greiling's avatar Mike Greiling

refactor monitoring_store.js

parent a4d3ac40
import _ from 'underscore'; import _ from 'underscore';
const sortMetrics = metrics => _.chain(metrics).sortBy('weight').sortBy('title').value(); function sortMetrics(metrics) {
return _.chain(metrics).sortBy('weight').sortBy('title').value();
class MonitoringStore { }
constructor() {
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 % 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 => ({
...group, ...group,
metrics: this.createArrayRows(sortMetrics(group.metrics)), metrics: collate(normalizeMetrics(sortMetrics(group.metrics))),
})); }));
} }
...@@ -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