Add series-specific variables for using in alias functions, #619

This commit is contained in:
Alexander Zobnin
2020-04-13 15:13:54 +03:00
parent 9430993b26
commit 6729de0e48
4 changed files with 64 additions and 40 deletions

View File

@@ -1,35 +1,37 @@
import _ from 'lodash'; import _ from 'lodash';
// Available in 7.0
// import { getTemplateSrv } from '@grafana/runtime';
import * as utils from './utils'; import * as utils from './utils';
import ts, { groupBy_perf as groupBy } from './timeseries'; import ts, { groupBy_perf as groupBy } from './timeseries';
let SUM = ts.SUM; const SUM = ts.SUM;
let COUNT = ts.COUNT; const COUNT = ts.COUNT;
let AVERAGE = ts.AVERAGE; const AVERAGE = ts.AVERAGE;
let MIN = ts.MIN; const MIN = ts.MIN;
let MAX = ts.MAX; const MAX = ts.MAX;
let MEDIAN = ts.MEDIAN; const MEDIAN = ts.MEDIAN;
let PERCENTILE = ts.PERCENTILE; const PERCENTILE = ts.PERCENTILE;
let downsampleSeries = ts.downsample; const downsampleSeries = ts.downsample;
let groupBy_exported = (interval, groupFunc, datapoints) => groupBy(datapoints, interval, groupFunc); const groupBy_exported = (interval, groupFunc, datapoints) => groupBy(datapoints, interval, groupFunc);
let sumSeries = ts.sumSeries; const sumSeries = ts.sumSeries;
let delta = ts.delta; const delta = ts.delta;
let rate = ts.rate; const rate = ts.rate;
let scale = (factor, datapoints) => ts.scale_perf(datapoints, factor); const scale = (factor, datapoints) => ts.scale_perf(datapoints, factor);
let offset = (delta, datapoints) => ts.offset(datapoints, delta); const offset = (delta, datapoints) => ts.offset(datapoints, delta);
let simpleMovingAverage = (n, datapoints) => ts.simpleMovingAverage(datapoints, n); const simpleMovingAverage = (n, datapoints) => ts.simpleMovingAverage(datapoints, n);
let expMovingAverage = (a, datapoints) => ts.expMovingAverage(datapoints, a); const expMovingAverage = (a, datapoints) => ts.expMovingAverage(datapoints, a);
let percentile = (interval, n, datapoints) => groupBy(datapoints, interval, _.partial(PERCENTILE, n)); const percentile = (interval, n, datapoints) => groupBy(datapoints, interval, _.partial(PERCENTILE, n));
function limit(order, n, orderByFunc, timeseries) { function limit(order, n, orderByFunc, timeseries) {
let orderByCallback = aggregationFunctions[orderByFunc]; const orderByCallback = aggregationFunctions[orderByFunc];
let sortByIteratee = (ts) => { const sortByIteratee = (ts) => {
let values = _.map(ts.datapoints, (point) => { const values = _.map(ts.datapoints, (point) => {
return point[0]; return point[0];
}); });
return orderByCallback(values); return orderByCallback(values);
}; };
let sortedTimeseries = _.sortBy(timeseries, sortByIteratee); const sortedTimeseries = _.sortBy(timeseries, sortByIteratee);
if (order === 'bottom') { if (order === 'bottom') {
return sortedTimeseries.slice(0, n); return sortedTimeseries.slice(0, n);
} else { } else {
@@ -64,13 +66,17 @@ function transformNull(n, datapoints) {
}); });
} }
function sortSeries(direction, timeseries) { function sortSeries(direction, timeseries: any[]) {
return _.orderBy(timeseries, [function (ts) { return _.orderBy(timeseries, [ts => {
return ts.target.toLowerCase(); return ts.target.toLowerCase();
}], direction); }], direction);
} }
function setAlias(alias, timeseries) { function setAlias(alias, timeseries) {
// TODO: use getTemplateSrv() when available (since 7.0)
if (this.templateSrv && timeseries && timeseries.scopedVars) {
alias = this.templateSrv.replace(alias, timeseries.scopedVars);
}
timeseries.target = alias; timeseries.target = alias;
return timeseries; return timeseries;
} }
@@ -84,6 +90,10 @@ function replaceAlias(regexp, newAlias, timeseries) {
} }
let alias = timeseries.target.replace(pattern, newAlias); let alias = timeseries.target.replace(pattern, newAlias);
// TODO: use getTemplateSrv() when available (since 7.0)
if (this.templateSrv && timeseries && timeseries.scopedVars) {
alias = this.templateSrv.replace(alias, timeseries.scopedVars);
}
timeseries.target = alias; timeseries.target = alias;
return timeseries; return timeseries;
} }
@@ -94,14 +104,13 @@ function setAliasByRegex(alias, timeseries) {
} }
function extractText(str, pattern) { function extractText(str, pattern) {
var extractPattern = new RegExp(pattern); const extractPattern = new RegExp(pattern);
var extractedValue = extractPattern.exec(str); const extractedValue = extractPattern.exec(str);
extractedValue = extractedValue[0]; return extractedValue[0];
return extractedValue;
} }
function groupByWrapper(interval, groupFunc, datapoints) { function groupByWrapper(interval, groupFunc, datapoints) {
var groupByCallback = aggregationFunctions[groupFunc]; const groupByCallback = aggregationFunctions[groupFunc];
return groupBy(datapoints, interval, groupByCallback); return groupBy(datapoints, interval, groupByCallback);
} }
@@ -110,12 +119,12 @@ function aggregateByWrapper(interval, aggregateFunc, datapoints) {
const flattenedPoints = ts.flattenDatapoints(datapoints); const flattenedPoints = ts.flattenDatapoints(datapoints);
// groupBy_perf works with sorted series only // groupBy_perf works with sorted series only
const sortedPoints = ts.sortByTime(flattenedPoints); const sortedPoints = ts.sortByTime(flattenedPoints);
let groupByCallback = aggregationFunctions[aggregateFunc]; const groupByCallback = aggregationFunctions[aggregateFunc];
return groupBy(sortedPoints, interval, groupByCallback); return groupBy(sortedPoints, interval, groupByCallback);
} }
function aggregateWrapper(groupByCallback, interval, datapoints) { function aggregateWrapper(groupByCallback, interval, datapoints) {
var flattenedPoints = ts.flattenDatapoints(datapoints); const flattenedPoints = ts.flattenDatapoints(datapoints);
// groupBy_perf works with sorted series only // groupBy_perf works with sorted series only
const sortedPoints = ts.sortByTime(flattenedPoints); const sortedPoints = ts.sortByTime(flattenedPoints);
return groupBy(sortedPoints, interval, groupByCallback); return groupBy(sortedPoints, interval, groupByCallback);
@@ -125,19 +134,19 @@ function percentileAgg(interval, n, datapoints) {
const flattenedPoints = ts.flattenDatapoints(datapoints); const flattenedPoints = ts.flattenDatapoints(datapoints);
// groupBy_perf works with sorted series only // groupBy_perf works with sorted series only
const sortedPoints = ts.sortByTime(flattenedPoints); const sortedPoints = ts.sortByTime(flattenedPoints);
let groupByCallback = _.partial(PERCENTILE, n); const groupByCallback = _.partial(PERCENTILE, n);
return groupBy(sortedPoints, interval, groupByCallback); return groupBy(sortedPoints, interval, groupByCallback);
} }
function timeShift(interval, range) { function timeShift(interval, range) {
let shift = utils.parseTimeShiftInterval(interval) / 1000; const shift = utils.parseTimeShiftInterval(interval) / 1000;
return _.map(range, time => { return _.map(range, time => {
return time - shift; return time - shift;
}); });
} }
function unShiftTimeSeries(interval, datapoints) { function unShiftTimeSeries(interval, datapoints) {
let unshift = utils.parseTimeShiftInterval(interval); const unshift = utils.parseTimeShiftInterval(interval);
return _.map(datapoints, dp => { return _.map(datapoints, dp => {
return [ return [
dp[0], dp[0],
@@ -146,7 +155,7 @@ function unShiftTimeSeries(interval, datapoints) {
}); });
} }
let metricFunctions = { const metricFunctions = {
groupBy: groupByWrapper, groupBy: groupByWrapper,
scale: scale, scale: scale,
offset: offset, offset: offset,
@@ -177,7 +186,7 @@ let metricFunctions = {
replaceAlias: replaceAlias replaceAlias: replaceAlias
}; };
let aggregationFunctions = { const aggregationFunctions = {
avg: AVERAGE, avg: AVERAGE,
min: MIN, min: MIN,
max: MAX, max: MAX,

View File

@@ -253,7 +253,7 @@ export class ZabbixDatasource {
} }
// Apply alias functions // Apply alias functions
_.forEach(timeseries_data, utils.sequence(aliasFunctions)); _.forEach(timeseries_data, utils.sequence(aliasFunctions).bind(this));
// Apply Time-related functions (timeShift(), etc) // Apply Time-related functions (timeShift(), etc)
// Find timeShift() function and get specified trend value // Find timeShift() function and get specified trend value

View File

@@ -29,14 +29,29 @@ function convertHistory(history, items, addHostName, convertPointCallback) {
return _.map(grouped_history, (hist, itemid) => { return _.map(grouped_history, (hist, itemid) => {
const item = _.find(items, {'itemid': itemid}) as any; const item = _.find(items, {'itemid': itemid}) as any;
let alias = item.name; let alias = item.name;
if (_.keys(hosts).length > 1 && addHostName) { //only when actual multi hosts selected
// Add scopedVars for using in alias functions
const scopedVars: any = {
'__zbx_item': { value: item.name },
'__zbx_item_name': { value: item.name },
'__zbx_item_key': { value: item.key_ },
};
if (_.keys(hosts).length > 0) {
const host = _.find(hosts, {'hostid': item.hostid}); const host = _.find(hosts, {'hostid': item.hostid});
scopedVars['__zbx_host'] = { value: host.host };
scopedVars['__zbx_host_name'] = { value: host.name };
// Only add host when multiple hosts selected
if (_.keys(hosts).length > 1 && addHostName) {
alias = host.name + ": " + alias; alias = host.name + ": " + alias;
} }
}
return { return {
target: alias, target: alias,
datapoints: _.map(hist, convertPointCallback) datapoints: _.map(hist, convertPointCallback),
scopedVars,
}; };
}); });
} }

View File

@@ -153,7 +153,7 @@ export class ZabbixAPIConnector {
sortfield: 'name', sortfield: 'name',
webitems: true, webitems: true,
filter: {}, filter: {},
selectHosts: ['hostid', 'name'] selectHosts: ['hostid', 'name', 'host']
}; };
if (hostids) { if (hostids) {
params.hostids = hostids; params.hostids = hostids;