Commit f69ad389 authored by Mike Greiling's avatar Mike Greiling

refactor monitoring_store.js

parent a4d3ac40
import _ from 'underscore';
const sortMetrics = metrics => _.chain(metrics).sortBy('weight').sortBy('title').value();
function sortMetrics(metrics) {
return _.chain(metrics).sortBy('weight').sortBy('title').value();
}
function normalizeMetrics(metrics) {
return metrics.map(metric => ({
...metric,
queries: metric.queries.map(query => ({
...query,
result: query.result.map(result => ({
...result,
values: result.values.map(([timestamp, value]) => ({
time: new Date(timestamp * 1000),
value,
})),
})),
})),
}));
}
function collate(array, rows = 2) {
const collatedArray = [];
let row = [];
array.forEach((value, index) => {
row.push(value);
if (index % rows === 0) {
collatedArray.push(row);
row = [];
}
});
if (row.length > 0) {
collatedArray.push(row);
}
return collatedArray;
}
class MonitoringStore {
export default class MonitoringStore {
constructor() {
this.groups = [];
this.deploymentData = [];
}
// eslint-disable-next-line class-methods-use-this
createArrayRows(metrics = []) {
const currentMetrics = metrics;
const availableMetrics = [];
let metricsRow = [];
let index = 1;
Object.keys(currentMetrics).forEach((key) => {
const metricValues = currentMetrics[key].queries[0].result;
if (metricValues != null) {
currentMetrics[key].queries[0].result = metricValues.map((series) => {
let convertedValues = [];
if (series != null) {
convertedValues = series.values.map(metric => ({
time: new Date(metric[0] * 1000),
value: metric[1],
}));
}
return {
metric: series.metric,
values: convertedValues,
};
});
metricsRow.push(currentMetrics[key]);
if (index % 2 === 0) {
availableMetrics.push(metricsRow);
metricsRow = [];
}
index = index += 1;
}
});
if (metricsRow.length > 0) {
availableMetrics.push(metricsRow);
}
return availableMetrics;
}
storeMetrics(groups = []) {
this.groups = groups.map(group => ({
...group,
metrics: this.createArrayRows(sortMetrics(group.metrics)),
metrics: collate(normalizeMetrics(sortMetrics(group.metrics))),
}));
}
......@@ -65,5 +63,3 @@ class MonitoringStore {
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