2020-04-09 11:09:29 -04:00
|
|
|
/**
|
|
|
|
* @param {String} queryLabel - Default query label for chart
|
|
|
|
* @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
|
|
|
|
* @returns {String} The formatted query label
|
|
|
|
* @example
|
|
|
|
* singleAttributeLabel('app', {__name__: "up", app: "prometheus"}) -> "app: prometheus"
|
|
|
|
*/
|
|
|
|
const singleAttributeLabel = (queryLabel, metricAttributes) => {
|
|
|
|
if (!queryLabel) return '';
|
|
|
|
const relevantAttribute = queryLabel.toLowerCase().replace(' ', '_');
|
|
|
|
const value = metricAttributes[relevantAttribute];
|
|
|
|
if (!value) return '';
|
|
|
|
return `${queryLabel}: ${value}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} queryLabel - Default query label for chart
|
|
|
|
* @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
|
|
|
|
* @returns {String} The formatted query label
|
|
|
|
* @example
|
|
|
|
* templatedLabel('__name__', {__name__: "up", app: "prometheus"}) -> "__name__"
|
|
|
|
*/
|
|
|
|
const templatedLabel = (queryLabel, metricAttributes) => {
|
|
|
|
if (!queryLabel) return '';
|
|
|
|
// eslint-disable-next-line array-callback-return
|
|
|
|
Object.entries(metricAttributes).map(([templateVar, label]) => {
|
|
|
|
const regex = new RegExp(`{{\\s*${templateVar}\\s*}}`, 'g');
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
queryLabel = queryLabel.replace(regex, label);
|
|
|
|
});
|
|
|
|
|
|
|
|
return queryLabel;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
|
|
|
|
* @returns {String} The formatted query label
|
|
|
|
* @example
|
|
|
|
* multiMetricLabel('', {__name__: "up", app: "prometheus"}) -> "__name__: up, app: prometheus"
|
|
|
|
*/
|
|
|
|
const multiMetricLabel = metricAttributes => {
|
|
|
|
return Object.entries(metricAttributes)
|
|
|
|
.map(([templateVar, label]) => `${templateVar}: ${label}`)
|
|
|
|
.join(', ');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} queryLabel - Default query label for chart
|
|
|
|
* @param {Object} metricAttributes - Default metric attribute values (e.g. method, instance)
|
|
|
|
* @returns {String} The formatted query label
|
|
|
|
*/
|
2020-07-22 14:09:27 -04:00
|
|
|
export const getSeriesLabel = (queryLabel, metricAttributes) => {
|
2020-04-09 11:09:29 -04:00
|
|
|
return (
|
|
|
|
singleAttributeLabel(queryLabel, metricAttributes) ||
|
|
|
|
templatedLabel(queryLabel, metricAttributes) ||
|
|
|
|
multiMetricLabel(metricAttributes) ||
|
|
|
|
queryLabel
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-11-04 07:06:19 -05:00
|
|
|
/**
|
|
|
|
* @param {Array} queryResults - Array of Result objects
|
|
|
|
* @param {Object} defaultConfig - Default chart config values (e.g. lineStyle, name)
|
|
|
|
* @returns {Array} The formatted values
|
|
|
|
*/
|
2019-04-05 05:14:13 -04:00
|
|
|
export const makeDataSeries = (queryResults, defaultConfig) =>
|
2020-06-29 08:09:20 -04:00
|
|
|
queryResults.map(result => {
|
|
|
|
return {
|
|
|
|
...defaultConfig,
|
|
|
|
data: result.values,
|
|
|
|
name: getSeriesLabel(defaultConfig.name, result.metric),
|
|
|
|
};
|
|
|
|
});
|