debian-mirror-gitlab/app/assets/javascripts/monitoring/stores/utils.js

86 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import _ from 'underscore';
2019-02-15 15:39:39 +05:30
function checkQueryEmptyData(query) {
return {
...query,
result: query.result.filter(timeSeries => {
const newTimeSeries = timeSeries;
const hasValue = series =>
2019-03-02 22:35:43 +05:30
!Number.isNaN(series[1]) && (series[1] !== null || series[1] !== undefined);
2019-02-15 15:39:39 +05:30
const hasNonNullValue = timeSeries.values.find(hasValue);
newTimeSeries.values = hasNonNullValue ? newTimeSeries.values : [];
return newTimeSeries.values.length > 0;
}),
};
}
function removeTimeSeriesNoData(queries) {
return queries.reduce((series, query) => series.concat(checkQueryEmptyData(query)), []);
}
2019-07-07 11:18:12 +05:30
// Metrics and queries are currently stored 1:1, so `queries` is an array of length one.
// We want to group queries onto a single chart by title & y-axis label.
// This function will no longer be required when metrics:queries are 1:many,
// though there is no consequence if the function stays in use.
// @param metrics [Array<Object>]
// Ex) [
// { id: 1, title: 'title', y_label: 'MB', queries: [{ ...query1Attrs }] },
// { id: 2, title: 'title', y_label: 'MB', queries: [{ ...query2Attrs }] },
// { id: 3, title: 'new title', y_label: 'MB', queries: [{ ...query3Attrs }] }
// ]
// @return [Array<Object>]
// Ex) [
// { title: 'title', y_label: 'MB', queries: [{ metricId: 1, ...query1Attrs },
// { metricId: 2, ...query2Attrs }] },
// { title: 'new title', y_label: 'MB', queries: [{ metricId: 3, ...query3Attrs }]}
// ]
function groupQueriesByChartInfo(metrics) {
const metricsByChart = metrics.reduce((accumulator, metric) => {
const { queries, ...chart } = metric;
const metricId = chart.id ? chart.id.toString() : null;
const chartKey = `${chart.title}|${chart.y_label}`;
accumulator[chartKey] = accumulator[chartKey] || { ...chart, queries: [] };
queries.forEach(queryAttrs => accumulator[chartKey].queries.push({ metricId, ...queryAttrs }));
return accumulator;
}, {});
return Object.values(metricsByChart);
}
2019-09-04 21:01:54 +05:30
export const sortMetrics = metrics =>
_.chain(metrics)
.sortBy('title')
.sortBy('weight')
.value();
export const normalizeQueryResult = timeSeries => ({
...timeSeries,
values: timeSeries.values.map(([timestamp, value]) => [
new Date(timestamp * 1000).toISOString(),
Number(value),
]),
});
export const normalizeMetrics = metrics => {
2019-07-07 11:18:12 +05:30
const groupedMetrics = groupQueriesByChartInfo(metrics);
return groupedMetrics.map(metric => {
2019-02-15 15:39:39 +05:30
const queries = metric.queries.map(query => ({
2018-03-17 18:26:18 +05:30
...query,
2019-07-07 11:18:12 +05:30
// custom metrics do not require a label, so we should ensure this attribute is defined
label: query.label || metric.y_label,
2019-09-04 21:01:54 +05:30
result: (query.result || []).map(normalizeQueryResult),
2019-02-15 15:39:39 +05:30
}));
return {
...metric,
queries: removeTimeSeriesNoData(queries),
};
});
2019-09-04 21:01:54 +05:30
};