Range-related variables #814
This commit is contained in:
@@ -37,3 +37,5 @@ export const TRIGGER_SEVERITY = [
|
||||
|
||||
/** Minimum interval for SLA over time (1 hour) */
|
||||
export const MIN_SLA_INTERVAL = 3600;
|
||||
|
||||
export const RANGE_VARIABLE_VALUE = 'range_series';
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import _ from 'lodash';
|
||||
import * as utils from './utils';
|
||||
import ts from './timeseries';
|
||||
import ts, { groupBy_perf as groupBy } from './timeseries';
|
||||
|
||||
let downsampleSeries = ts.downsample;
|
||||
let groupBy = ts.groupBy_perf;
|
||||
let groupBy_exported = (interval, groupFunc, datapoints) => groupBy(datapoints, interval, groupFunc);
|
||||
let sumSeries = ts.sumSeries;
|
||||
let delta = ts.delta;
|
||||
|
||||
@@ -111,6 +111,9 @@ export class ZabbixDatasource {
|
||||
let timeFrom = Math.ceil(dateMath.parse(options.range.from) / 1000);
|
||||
let timeTo = Math.ceil(dateMath.parse(options.range.to) / 1000);
|
||||
|
||||
// Add range variables
|
||||
options.scopedVars = Object.assign({}, options.scopedVars, utils.getRangeScopedVars(options.range));
|
||||
|
||||
// Prevent changes of original object
|
||||
let target = _.cloneDeep(t);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import _ from 'lodash';
|
||||
import mocks from '../../test-setup/mocks';
|
||||
import { Datasource } from "../module";
|
||||
import { zabbixTemplateFormat } from "../datasource";
|
||||
import { dateMath } from '@grafana/data';
|
||||
|
||||
describe('ZabbixDatasource', () => {
|
||||
let ctx = {};
|
||||
@@ -41,7 +42,10 @@ describe('ZabbixDatasource', () => {
|
||||
item: {filter: ""}
|
||||
}
|
||||
],
|
||||
range: {from: 'now-7d', to: 'now'}
|
||||
range: {
|
||||
from: dateMath.parse('now-1h'),
|
||||
to: dateMath.parse('now')
|
||||
}
|
||||
};
|
||||
|
||||
it('should return an empty array when no targets are set', (done) => {
|
||||
@@ -59,7 +63,7 @@ describe('ZabbixDatasource', () => {
|
||||
let ranges = ['now-8d', 'now-169h', 'now-1M', 'now-1y'];
|
||||
|
||||
_.forEach(ranges, range => {
|
||||
ctx.options.range.from = range;
|
||||
ctx.options.range.from = dateMath.parse(range);
|
||||
ctx.ds.queryNumericData = jest.fn();
|
||||
ctx.ds.query(ctx.options);
|
||||
|
||||
@@ -76,7 +80,7 @@ describe('ZabbixDatasource', () => {
|
||||
let ranges = ['now-7d', 'now-168h', 'now-1h', 'now-30m', 'now-30s'];
|
||||
|
||||
_.forEach(ranges, range => {
|
||||
ctx.options.range.from = range;
|
||||
ctx.options.range.from = dateMath.parse(range);
|
||||
ctx.ds.queryNumericData = jest.fn();
|
||||
ctx.ds.query(ctx.options);
|
||||
|
||||
@@ -108,24 +112,19 @@ describe('ZabbixDatasource', () => {
|
||||
}
|
||||
]));
|
||||
|
||||
ctx.options = {
|
||||
range: {from: 'now-1h', to: 'now'},
|
||||
targets: [
|
||||
{
|
||||
group: {filter: ""},
|
||||
host: {filter: "Zabbix server"},
|
||||
application: {filter: ""},
|
||||
item: {filter: "System information"},
|
||||
textFilter: "",
|
||||
useCaptureGroups: true,
|
||||
mode: 2,
|
||||
resultFormat: "table",
|
||||
options: {
|
||||
skipEmptyValues: false
|
||||
}
|
||||
}
|
||||
],
|
||||
};
|
||||
ctx.options.targets = [{
|
||||
group: {filter: ""},
|
||||
host: {filter: "Zabbix server"},
|
||||
application: {filter: ""},
|
||||
item: {filter: "System information"},
|
||||
textFilter: "",
|
||||
useCaptureGroups: true,
|
||||
mode: 2,
|
||||
resultFormat: "table",
|
||||
options: {
|
||||
skipEmptyValues: false
|
||||
}
|
||||
}];
|
||||
});
|
||||
|
||||
it('should return data in table format', (done) => {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
import _ from 'lodash';
|
||||
import * as utils from './utils';
|
||||
import * as c from './constants';
|
||||
|
||||
const POINT_VALUE = 0;
|
||||
const POINT_TIMESTAMP = 1;
|
||||
@@ -94,11 +95,15 @@ function groupBy(datapoints, interval, groupByCallback) {
|
||||
}));
|
||||
}
|
||||
|
||||
function groupBy_perf(datapoints, interval, groupByCallback) {
|
||||
export function groupBy_perf(datapoints, interval, groupByCallback) {
|
||||
if (datapoints.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (interval === c.RANGE_VARIABLE_VALUE) {
|
||||
return groupByRange(datapoints, groupByCallback);
|
||||
}
|
||||
|
||||
let ms_interval = utils.parseInterval(interval);
|
||||
let grouped_series = [];
|
||||
let frame_values = [];
|
||||
@@ -132,6 +137,19 @@ function groupBy_perf(datapoints, interval, groupByCallback) {
|
||||
return grouped_series;
|
||||
}
|
||||
|
||||
export function groupByRange(datapoints, groupByCallback) {
|
||||
const frame_values = [];
|
||||
const frame_start = datapoints[0][POINT_TIMESTAMP];
|
||||
const frame_end = datapoints[datapoints.length - 1][POINT_TIMESTAMP];
|
||||
let point;
|
||||
for (let i=0; i < datapoints.length; i++) {
|
||||
point = datapoints[i];
|
||||
frame_values.push(point[POINT_VALUE]);
|
||||
}
|
||||
const frame_value = groupByCallback(frame_values);
|
||||
return [[frame_value, frame_start], [frame_value, frame_end]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize set of time series into one.
|
||||
* @param {datapoints[]} timeseries array of time series
|
||||
@@ -495,6 +513,7 @@ const exportedFunctions = {
|
||||
downsample,
|
||||
groupBy,
|
||||
groupBy_perf,
|
||||
groupByRange,
|
||||
sumSeries,
|
||||
scale,
|
||||
offset,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
import moment from 'moment';
|
||||
import kbn from 'grafana/app/core/utils/kbn';
|
||||
import * as c from './constants';
|
||||
|
||||
/**
|
||||
* Expand Zabbix item name
|
||||
@@ -141,6 +143,18 @@ export function isTemplateVariable(str, templateVariables) {
|
||||
}
|
||||
}
|
||||
|
||||
export function getRangeScopedVars(range) {
|
||||
const msRange = range.to.diff(range.from);
|
||||
const sRange = Math.round(msRange / 1000);
|
||||
const regularRange = kbn.secondsToHms(msRange / 1000);
|
||||
return {
|
||||
__range_ms: { text: msRange, value: msRange },
|
||||
__range_s: { text: sRange, value: sRange },
|
||||
__range: { text: regularRange, value: regularRange },
|
||||
__range_series: {text: c.RANGE_VARIABLE_VALUE, value: c.RANGE_VARIABLE_VALUE},
|
||||
};
|
||||
}
|
||||
|
||||
export function buildRegex(str) {
|
||||
var matches = str.match(regexPattern);
|
||||
var pattern = matches[1];
|
||||
|
||||
Reference in New Issue
Block a user