Skip to content

Commit

Permalink
SLA over time: make intervals based on panel intervalMs, #728
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Jul 30, 2019
1 parent 2fac45c commit 8f139d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/datasource-zabbix/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ export const TRIGGER_SEVERITY = [
{val: 4, text: 'High'},
{val: 5, text: 'Disaster'}
];

/** Minimum interval for SLA over time (1 hour) */
export const MIN_SLA_INTERVAL = 3600;
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import _ from 'lodash';
import kbn from 'grafana/app/core/utils/kbn';
import * as utils from '../../../utils';
import { ZabbixAPICore } from './zabbixAPICore';
import { ZBX_ACK_ACTION_NONE, ZBX_ACK_ACTION_ACK, ZBX_ACK_ACTION_ADD_MESSAGE } from '../../../constants';
import { ZBX_ACK_ACTION_NONE, ZBX_ACK_ACTION_ACK, ZBX_ACK_ACTION_ADD_MESSAGE, MIN_SLA_INTERVAL } from '../../../constants';

/**
* Zabbix API Wrapper.
Expand Down Expand Up @@ -316,25 +317,11 @@ export class ZabbixAPIConnector {
return this.request('service.get', params);
}

getSLA(serviceids, timeRange) {
let defaultRange = 86400;
let i;
let getIntervals = [];
let [timeFrom, timeTo] = timeRange;

for (i = timeFrom; i <= timeTo; i = i + defaultRange) {
if (timeTo < (i + defaultRange)) {
if (timeTo !== i) {
getIntervals.push({from : i, to : timeTo});
}
} else {
getIntervals.push({from : i, to : (i + defaultRange)});
}
}

var params = {
serviceids: serviceids,
intervals: getIntervals
getSLA(serviceids, timeRange, options) {
const intervals = buildSLAIntervals(timeRange, options.intervalMs);
const params = {
serviceids,
intervals
};
return this.request('service.getsla', params);
}
Expand Down Expand Up @@ -536,3 +523,30 @@ function isNotAuthorized(message) {
message === "Not authorized."
);
}

function getSLAInterval(intervalMs) {
// Too many intervals may cause significant load on the database, so decrease number of resulting points
const resolutionRatio = 100;
const interval = kbn.round_interval(intervalMs * resolutionRatio) / 1000;
return Math.max(interval, MIN_SLA_INTERVAL);
}

function buildSLAIntervals(timeRange, intervalMs) {
let [timeFrom, timeTo] = timeRange;
const slaInterval = getSLAInterval(intervalMs);
const intervals = [];

// Align time range with calculated interval
timeFrom = Math.floor(timeFrom / slaInterval) * slaInterval;
timeTo = Math.ceil(timeTo / slaInterval) * slaInterval;

for (let i = timeFrom; i <= timeTo - slaInterval; i += slaInterval) {
intervals.push({
from : i,
to : (i + slaInterval)
});

}

return intervals;
}
2 changes: 1 addition & 1 deletion src/datasource-zabbix/zabbix/zabbix.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class Zabbix {
itServices = _.filter(itServices, {'serviceid': target.itservice.serviceid});
}
let itServiceIds = _.map(itServices, 'serviceid');
return this.zabbixAPI.getSLA(itServiceIds, timeRange)
return this.zabbixAPI.getSLA(itServiceIds, timeRange, options)
.then(slaResponse => {
return _.map(itServiceIds, serviceid => {
let itservice = _.find(itServices, {'serviceid': serviceid});
Expand Down

0 comments on commit 8f139d0

Please sign in to comment.