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';
class MonitoringStore {
constructor() {
this.groups = [];
this.deploymentData = [];
}
function sortMetrics(metrics) {
return _.chain(metrics).sortBy('weight').sortBy('title').value();
}
// 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],
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,
})),
})),
})),
}));
}
return {
metric: series.metric,
values: convertedValues,
};
});
metricsRow.push(currentMetrics[key]);
if (index % 2 === 0) {
availableMetrics.push(metricsRow);
metricsRow = [];
}
index = index += 1;
}
function collate(array, rows = 2) {
const collatedArray = [];
let row = [];
array.forEach((value, index) => {
row.push(value);
if ((index + 1) % rows === 0) {
collatedArray.push(row);
row = [];
}
});
if (metricsRow.length > 0) {
availableMetrics.push(metricsRow);
if (row.length > 0) {
collatedArray.push(row);
}
return availableMetrics;
return collatedArray;
}
export default class MonitoringStore {
constructor() {
this.groups = [];
this.deploymentData = [];
}
storeMetrics(groups = []) {
this.groups = groups.map((group) => {
const currentGroup = group;
currentGroup.metrics = _.chain(group.metrics).sortBy('weight').sortBy('title').value();
currentGroup.metrics = this.createArrayRows(currentGroup.metrics);
return currentGroup;
});
this.groups = groups.map(group => ({
...group,
metrics: collate(normalizeMetrics(sortMetrics(group.metrics))),
}));
}
storeDeploymentData(deploymentData = []) {
......@@ -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