debian-mirror-gitlab/app/assets/javascripts/helpers/monitor_helper.js

33 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-12-26 22:10:19 +05:30
/**
* @param {Array} queryResults - Array of Result objects
* @param {Object} defaultConfig - Default chart config values (e.g. lineStyle, name)
* @returns {Array} The formatted values
*/
// eslint-disable-next-line import/prefer-default-export
2019-07-07 11:18:12 +05:30
export const makeDataSeries = (queryResults, defaultConfig) =>
2019-12-26 22:10:19 +05:30
queryResults
.map(result => {
2020-01-01 13:55:28 +05:30
// NaN values may disrupt avg., max. & min. calculations in the legend, filter them out
2019-12-26 22:10:19 +05:30
const data = result.values.filter(([, value]) => !Number.isNaN(value));
if (!data.length) {
return null;
}
const relevantMetric = defaultConfig.name.toLowerCase().replace(' ', '_');
const name = result.metric[relevantMetric];
const series = { data };
if (name) {
series.name = `${defaultConfig.name}: ${name}`;
} else {
series.name = defaultConfig.name;
Object.keys(result.metric).forEach(templateVar => {
const value = result.metric[templateVar];
const regex = new RegExp(`{{\\s*${templateVar}\\s*}}`, 'g');
series.name = series.name.replace(regex, value);
});
}
2019-07-07 11:18:12 +05:30
2019-12-26 22:10:19 +05:30
return { ...defaultConfig, ...series };
})
.filter(series => series !== null);