From 8f139d0322c86fd146217447e53c85b8b2921831 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 30 Jul 2019 18:52:32 +0300 Subject: [PATCH] SLA over time: make intervals based on panel intervalMs, #728 --- src/datasource-zabbix/constants.js | 3 ++ .../zabbix_api/zabbixAPIConnector.js | 54 ++++++++++++------- src/datasource-zabbix/zabbix/zabbix.js | 2 +- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/datasource-zabbix/constants.js b/src/datasource-zabbix/constants.js index 3c8de51..af3c8d1 100644 --- a/src/datasource-zabbix/constants.js +++ b/src/datasource-zabbix/constants.js @@ -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; diff --git a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js index 6fd7931..f3a9a9b 100644 --- a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js +++ b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js @@ -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. @@ -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); } @@ -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; +} diff --git a/src/datasource-zabbix/zabbix/zabbix.js b/src/datasource-zabbix/zabbix/zabbix.js index 35bb96a..74139a4 100644 --- a/src/datasource-zabbix/zabbix/zabbix.js +++ b/src/datasource-zabbix/zabbix/zabbix.js @@ -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});