minor refactor
This commit is contained in:
@@ -3,10 +3,12 @@ import _ from 'lodash';
|
|||||||
import $ from 'jquery';
|
import $ from 'jquery';
|
||||||
import * as metricFunctions from './metricFunctions';
|
import * as metricFunctions from './metricFunctions';
|
||||||
|
|
||||||
/** @ngInject */
|
|
||||||
angular
|
angular
|
||||||
.module('grafana.directives')
|
.module('grafana.directives')
|
||||||
.directive('addMetricFunction', function($compile) {
|
.directive('addMetricFunction',
|
||||||
|
|
||||||
|
/** @ngInject */
|
||||||
|
function($compile) {
|
||||||
var inputTemplate = '<input type="text"'+
|
var inputTemplate = '<input type="text"'+
|
||||||
' class="gf-form-input"' +
|
' class="gf-form-input"' +
|
||||||
' spellcheck="false" style="display:none"></input>';
|
' spellcheck="false" style="display:none"></input>';
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ const defaultConfig = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export class ZabbixDSConfigController {
|
export class ZabbixDSConfigController {
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor($scope, $injector, datasourceSrv) {
|
constructor($scope, $injector, datasourceSrv) {
|
||||||
this.datasourceSrv = datasourceSrv;
|
this.datasourceSrv = datasourceSrv;
|
||||||
@@ -24,5 +25,3 @@ export class ZabbixDSConfigController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ZabbixDSConfigController.templateUrl = 'datasource-zabbix/partials/config.html';
|
|
||||||
|
|||||||
@@ -6,11 +6,10 @@ import * as metricFunctions from './metricFunctions';
|
|||||||
import * as c from './constants';
|
import * as c from './constants';
|
||||||
import dataProcessor from './dataProcessor';
|
import dataProcessor from './dataProcessor';
|
||||||
import responseHandler from './responseHandler';
|
import responseHandler from './responseHandler';
|
||||||
import './zabbixAlerting.service.js';
|
|
||||||
import { Zabbix } from './zabbix/zabbix';
|
import { Zabbix } from './zabbix/zabbix';
|
||||||
import {ZabbixAPIError} from './zabbix/connectors/zabbix_api/zabbixAPICore';
|
import { ZabbixAPIError } from './zabbix/connectors/zabbix_api/zabbixAPICore';
|
||||||
|
|
||||||
class ZabbixAPIDatasource {
|
export class ZabbixDatasource {
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor(instanceSettings, templateSrv, alertSrv, dashboardSrv, backendSrv, datasourceSrv, zabbixAlertingSrv) {
|
constructor(instanceSettings, templateSrv, alertSrv, dashboardSrv, backendSrv, datasourceSrv, zabbixAlertingSrv) {
|
||||||
@@ -96,7 +95,7 @@ class ZabbixAPIDatasource {
|
|||||||
|
|
||||||
// Create request for each target
|
// Create request for each target
|
||||||
let promises = _.map(options.targets, t => {
|
let promises = _.map(options.targets, t => {
|
||||||
// Don't request undefined and hidden targets
|
// Don't request for hidden targets
|
||||||
if (t.hide) {
|
if (t.hide) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -111,7 +110,7 @@ class ZabbixAPIDatasource {
|
|||||||
// Apply Time-related functions (timeShift(), etc)
|
// Apply Time-related functions (timeShift(), etc)
|
||||||
let timeFunctions = bindFunctionDefs(target.functions, 'Time');
|
let timeFunctions = bindFunctionDefs(target.functions, 'Time');
|
||||||
if (timeFunctions.length) {
|
if (timeFunctions.length) {
|
||||||
const [time_from, time_to] = sequence(timeFunctions)([timeFrom, timeTo]);
|
const [time_from, time_to] = utils.sequence(timeFunctions)([timeFrom, timeTo]);
|
||||||
timeFrom = time_from;
|
timeFrom = time_from;
|
||||||
timeTo = time_to;
|
timeTo = time_to;
|
||||||
}
|
}
|
||||||
@@ -124,8 +123,8 @@ class ZabbixAPIDatasource {
|
|||||||
// Migrate old targets
|
// Migrate old targets
|
||||||
target = migrations.migrate(target);
|
target = migrations.migrate(target);
|
||||||
|
|
||||||
// Don't request undefined and hidden targets
|
// Don't request undefined targets
|
||||||
if (target.hide || !target.group || !target.host || !target.item) {
|
if (!target.group || !target.host || !target.item) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,9 +166,7 @@ class ZabbixAPIDatasource {
|
|||||||
itemtype: 'num'
|
itemtype: 'num'
|
||||||
};
|
};
|
||||||
return this.zabbix.getItemsFromTarget(target, getItemOptions)
|
return this.zabbix.getItemsFromTarget(target, getItemOptions)
|
||||||
.then(items => {
|
.then(items => this.queryNumericDataForItems(items, target, timeRange, useTrends, options));
|
||||||
return this.queryNumericDataForItems(items, target, timeRange, useTrends, options);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -208,19 +205,19 @@ class ZabbixAPIDatasource {
|
|||||||
|
|
||||||
// Apply transformation functions
|
// Apply transformation functions
|
||||||
timeseries_data = _.cloneDeep(_.map(timeseries_data, timeseries => {
|
timeseries_data = _.cloneDeep(_.map(timeseries_data, timeseries => {
|
||||||
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
|
timeseries.datapoints = utils.sequence(transformFunctions)(timeseries.datapoints);
|
||||||
return timeseries;
|
return timeseries;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Apply filter functions
|
// Apply filter functions
|
||||||
if (filterFunctions.length) {
|
if (filterFunctions.length) {
|
||||||
timeseries_data = sequence(filterFunctions)(timeseries_data);
|
timeseries_data = utils.sequence(filterFunctions)(timeseries_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply aggregations
|
// Apply aggregations
|
||||||
if (aggregationFunctions.length) {
|
if (aggregationFunctions.length) {
|
||||||
let dp = _.map(timeseries_data, 'datapoints');
|
let dp = _.map(timeseries_data, 'datapoints');
|
||||||
dp = sequence(aggregationFunctions)(dp);
|
dp = utils.sequence(aggregationFunctions)(dp);
|
||||||
|
|
||||||
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
|
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
|
||||||
let lastAgg = _.findLast(target.functions, func => {
|
let lastAgg = _.findLast(target.functions, func => {
|
||||||
@@ -234,7 +231,7 @@ class ZabbixAPIDatasource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply alias functions
|
// Apply alias functions
|
||||||
_.forEach(timeseries_data, sequence(aliasFunctions));
|
_.forEach(timeseries_data, utils.sequence(aliasFunctions));
|
||||||
|
|
||||||
// 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
|
||||||
@@ -648,7 +645,7 @@ function formatMetric(metricObj) {
|
|||||||
* template variables, for example
|
* template variables, for example
|
||||||
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
|
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
|
||||||
*/
|
*/
|
||||||
function zabbixTemplateFormat(value) {
|
export function zabbixTemplateFormat(value) {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return utils.escapeRegex(value);
|
return utils.escapeRegex(value);
|
||||||
}
|
}
|
||||||
@@ -680,17 +677,6 @@ function replaceTemplateVars(templateSrv, target, scopedVars) {
|
|||||||
return replacedTarget;
|
return replacedTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply function one by one:
|
|
||||||
// sequence([a(), b(), c()]) = c(b(a()));
|
|
||||||
function sequence(funcsArray) {
|
|
||||||
return function(result) {
|
|
||||||
for (var i = 0; i < funcsArray.length; i++) {
|
|
||||||
result = funcsArray[i].call(this, result);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function filterEnabledTargets(targets) {
|
function filterEnabledTargets(targets) {
|
||||||
return _.filter(targets, target => {
|
return _.filter(targets, target => {
|
||||||
return !(target.hide || !target.group || !target.host || !target.item);
|
return !(target.hide || !target.group || !target.host || !target.item);
|
||||||
@@ -709,8 +695,6 @@ function getTriggerThreshold(expression) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {ZabbixAPIDatasource, zabbixTemplateFormat};
|
|
||||||
|
|
||||||
// Fix for backward compatibility with lodash 2.4
|
// Fix for backward compatibility with lodash 2.4
|
||||||
if (!_.includes) {_.includes = _.contains;}
|
if (!_.includes) {_.includes = _.contains;}
|
||||||
if (!_.keyBy) {_.keyBy = _.indexBy;}
|
if (!_.keyBy) {_.keyBy = _.indexBy;}
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ import angular from 'angular';
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import $ from 'jquery';
|
import $ from 'jquery';
|
||||||
|
|
||||||
/** @ngInject */
|
const DOCS_FUNC_REF_URL = 'http://docs.grafana-zabbix.org/reference/functions/';
|
||||||
|
|
||||||
angular
|
angular
|
||||||
.module('grafana.directives')
|
.module('grafana.directives')
|
||||||
.directive('metricFunctionEditor', function($compile, templateSrv) {
|
.directive('metricFunctionEditor',
|
||||||
|
|
||||||
|
/** @ngInject */
|
||||||
|
function($compile, templateSrv) {
|
||||||
|
|
||||||
var funcSpanTemplate = '<a ng-click="">{{func.def.name}}</a><span>(</span>';
|
var funcSpanTemplate = '<a ng-click="">{{func.def.name}}</a><span>(</span>';
|
||||||
var paramTemplate = '<input type="text" style="display:none"' +
|
var paramTemplate = '<input type="text" style="display:none"' +
|
||||||
@@ -221,7 +225,7 @@ angular
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($target.hasClass('fa-question-circle')) {
|
if ($target.hasClass('fa-question-circle')) {
|
||||||
var docSite = "http://docs.grafana-zabbix.org/reference/functions/";
|
var docSite = DOCS_FUNC_REF_URL;
|
||||||
window.open(docSite + '#' + funcDef.name.toLowerCase(),'_blank');
|
window.open(docSite + '#' + funcDef.name.toLowerCase(),'_blank');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import {loadPluginCss} from 'app/plugins/sdk';
|
import { loadPluginCss } from 'app/plugins/sdk';
|
||||||
import {ZabbixAPIDatasource} from './datasource';
|
import { ZabbixDatasource } from './datasource';
|
||||||
import {ZabbixQueryController} from './query.controller';
|
import { ZabbixQueryController } from './query.controller';
|
||||||
import {ZabbixDSConfigController} from './config.controller';
|
import { ZabbixDSConfigController } from './config.controller';
|
||||||
|
import './zabbixAlerting.service.js';
|
||||||
|
import './add-metric-function.directive';
|
||||||
|
import './metric-function-editor.directive';
|
||||||
|
|
||||||
loadPluginCss({
|
loadPluginCss({
|
||||||
dark: 'plugins/alexanderzobnin-zabbix-app/css/grafana-zabbix.dark.css',
|
dark: 'plugins/alexanderzobnin-zabbix-app/css/grafana-zabbix.dark.css',
|
||||||
@@ -14,8 +17,11 @@ ZabbixQueryOptionsController.templateUrl = 'datasource-zabbix/partials/query.opt
|
|||||||
class ZabbixAnnotationsQueryController {}
|
class ZabbixAnnotationsQueryController {}
|
||||||
ZabbixAnnotationsQueryController.templateUrl = 'datasource-zabbix/partials/annotations.editor.html';
|
ZabbixAnnotationsQueryController.templateUrl = 'datasource-zabbix/partials/annotations.editor.html';
|
||||||
|
|
||||||
|
ZabbixQueryController.templateUrl = 'datasource-zabbix/partials/query.editor.html';
|
||||||
|
ZabbixDSConfigController.templateUrl = 'datasource-zabbix/partials/config.html';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
ZabbixAPIDatasource as Datasource,
|
ZabbixDatasource as Datasource,
|
||||||
ZabbixDSConfigController as ConfigCtrl,
|
ZabbixDSConfigController as ConfigCtrl,
|
||||||
ZabbixQueryController as QueryCtrl,
|
ZabbixQueryController as QueryCtrl,
|
||||||
ZabbixQueryOptionsController as QueryOptionsCtrl,
|
ZabbixQueryOptionsController as QueryOptionsCtrl,
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
import {QueryCtrl} from 'app/plugins/sdk';
|
import { QueryCtrl } from 'app/plugins/sdk';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import * as c from './constants';
|
import * as c from './constants';
|
||||||
import * as utils from './utils';
|
import * as utils from './utils';
|
||||||
import * as metricFunctions from './metricFunctions';
|
import * as metricFunctions from './metricFunctions';
|
||||||
import * as migrations from './migrations';
|
import * as migrations from './migrations';
|
||||||
|
|
||||||
import './add-metric-function.directive';
|
|
||||||
import './metric-function-editor.directive';
|
|
||||||
|
|
||||||
export class ZabbixQueryController extends QueryCtrl {
|
export class ZabbixQueryController extends QueryCtrl {
|
||||||
|
|
||||||
// ZabbixQueryCtrl constructor
|
// ZabbixQueryCtrl constructor
|
||||||
@@ -333,6 +330,3 @@ export class ZabbixQueryController extends QueryCtrl {
|
|||||||
this.targetChanged();
|
this.targetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set templateUrl as static property
|
|
||||||
ZabbixQueryController.templateUrl = 'datasource-zabbix/partials/query.editor.html';
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import Q, { Promise } from "q";
|
import Q, { Promise } from "q";
|
||||||
import {Datasource} from "../module";
|
import { Datasource } from "../module";
|
||||||
import {zabbixTemplateFormat} from "../datasource";
|
import { zabbixTemplateFormat } from "../datasource";
|
||||||
|
|
||||||
describe('ZabbixDatasource', () => {
|
describe('ZabbixDatasource', () => {
|
||||||
let ctx = {};
|
let ctx = {};
|
||||||
|
|||||||
@@ -227,6 +227,19 @@ export function callOnce(func, promiseKeeper) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply function one by one: `sequence([a(), b(), c()]) = c(b(a()))`
|
||||||
|
* @param {*} funcsArray functions to apply
|
||||||
|
*/
|
||||||
|
export function sequence(funcsArray) {
|
||||||
|
return function(result) {
|
||||||
|
for (var i = 0; i < funcsArray.length; i++) {
|
||||||
|
result = funcsArray[i].call(this, result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Fix for backward compatibility with lodash 2.4
|
// Fix for backward compatibility with lodash 2.4
|
||||||
if (!_.includes) {
|
if (!_.includes) {
|
||||||
_.includes = _.contains;
|
_.includes = _.contains;
|
||||||
|
|||||||
@@ -2,6 +2,10 @@ import _ from 'lodash';
|
|||||||
|
|
||||||
const NOT_IMPLEMENTED = 'Method should be implemented in subclass of DBConnector';
|
const NOT_IMPLEMENTED = 'Method should be implemented in subclass of DBConnector';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for external history database connectors. Subclasses should implement `getHistory()`, `getTrends()` and
|
||||||
|
* `testDataSource()` methods, which describe how to fetch data from source other than Zabbix API.
|
||||||
|
*/
|
||||||
export default class DBConnector {
|
export default class DBConnector {
|
||||||
constructor(options, backendSrv, datasourceSrv) {
|
constructor(options, backendSrv, datasourceSrv) {
|
||||||
this.backendSrv = backendSrv;
|
this.backendSrv = backendSrv;
|
||||||
@@ -24,14 +28,23 @@ export default class DBConnector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send test request to datasource in order to ensure it's working.
|
||||||
|
*/
|
||||||
testDataSource() {
|
testDataSource() {
|
||||||
throw NOT_IMPLEMENTED;
|
throw NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get history data from external sources.
|
||||||
|
*/
|
||||||
getHistory() {
|
getHistory() {
|
||||||
throw NOT_IMPLEMENTED;
|
throw NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get trends data from external sources.
|
||||||
|
*/
|
||||||
getTrends() {
|
getTrends() {
|
||||||
throw NOT_IMPLEMENTED;
|
throw NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user