minor refactor
This commit is contained in:
@@ -3,10 +3,12 @@ import _ from 'lodash';
|
||||
import $ from 'jquery';
|
||||
import * as metricFunctions from './metricFunctions';
|
||||
|
||||
/** @ngInject */
|
||||
angular
|
||||
.module('grafana.directives')
|
||||
.directive('addMetricFunction', function($compile) {
|
||||
.directive('addMetricFunction',
|
||||
|
||||
/** @ngInject */
|
||||
function($compile) {
|
||||
var inputTemplate = '<input type="text"'+
|
||||
' class="gf-form-input"' +
|
||||
' spellcheck="false" style="display:none"></input>';
|
||||
|
||||
@@ -9,6 +9,7 @@ const defaultConfig = {
|
||||
};
|
||||
|
||||
export class ZabbixDSConfigController {
|
||||
|
||||
/** @ngInject */
|
||||
constructor($scope, $injector, 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 dataProcessor from './dataProcessor';
|
||||
import responseHandler from './responseHandler';
|
||||
import './zabbixAlerting.service.js';
|
||||
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 */
|
||||
constructor(instanceSettings, templateSrv, alertSrv, dashboardSrv, backendSrv, datasourceSrv, zabbixAlertingSrv) {
|
||||
@@ -96,7 +95,7 @@ class ZabbixAPIDatasource {
|
||||
|
||||
// Create request for each target
|
||||
let promises = _.map(options.targets, t => {
|
||||
// Don't request undefined and hidden targets
|
||||
// Don't request for hidden targets
|
||||
if (t.hide) {
|
||||
return [];
|
||||
}
|
||||
@@ -111,7 +110,7 @@ class ZabbixAPIDatasource {
|
||||
// Apply Time-related functions (timeShift(), etc)
|
||||
let timeFunctions = bindFunctionDefs(target.functions, 'Time');
|
||||
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;
|
||||
timeTo = time_to;
|
||||
}
|
||||
@@ -124,8 +123,8 @@ class ZabbixAPIDatasource {
|
||||
// Migrate old targets
|
||||
target = migrations.migrate(target);
|
||||
|
||||
// Don't request undefined and hidden targets
|
||||
if (target.hide || !target.group || !target.host || !target.item) {
|
||||
// Don't request undefined targets
|
||||
if (!target.group || !target.host || !target.item) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -167,9 +166,7 @@ class ZabbixAPIDatasource {
|
||||
itemtype: 'num'
|
||||
};
|
||||
return this.zabbix.getItemsFromTarget(target, getItemOptions)
|
||||
.then(items => {
|
||||
return this.queryNumericDataForItems(items, target, timeRange, useTrends, options);
|
||||
});
|
||||
.then(items => this.queryNumericDataForItems(items, target, timeRange, useTrends, options));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,19 +205,19 @@ class ZabbixAPIDatasource {
|
||||
|
||||
// Apply transformation functions
|
||||
timeseries_data = _.cloneDeep(_.map(timeseries_data, timeseries => {
|
||||
timeseries.datapoints = sequence(transformFunctions)(timeseries.datapoints);
|
||||
timeseries.datapoints = utils.sequence(transformFunctions)(timeseries.datapoints);
|
||||
return timeseries;
|
||||
}));
|
||||
|
||||
// Apply filter functions
|
||||
if (filterFunctions.length) {
|
||||
timeseries_data = sequence(filterFunctions)(timeseries_data);
|
||||
timeseries_data = utils.sequence(filterFunctions)(timeseries_data);
|
||||
}
|
||||
|
||||
// Apply aggregations
|
||||
if (aggregationFunctions.length) {
|
||||
let dp = _.map(timeseries_data, 'datapoints');
|
||||
dp = sequence(aggregationFunctions)(dp);
|
||||
dp = utils.sequence(aggregationFunctions)(dp);
|
||||
|
||||
let aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
|
||||
let lastAgg = _.findLast(target.functions, func => {
|
||||
@@ -234,7 +231,7 @@ class ZabbixAPIDatasource {
|
||||
}
|
||||
|
||||
// Apply alias functions
|
||||
_.forEach(timeseries_data, sequence(aliasFunctions));
|
||||
_.forEach(timeseries_data, utils.sequence(aliasFunctions));
|
||||
|
||||
// Apply Time-related functions (timeShift(), etc)
|
||||
// Find timeShift() function and get specified trend value
|
||||
@@ -648,7 +645,7 @@ function formatMetric(metricObj) {
|
||||
* template variables, for example
|
||||
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
|
||||
*/
|
||||
function zabbixTemplateFormat(value) {
|
||||
export function zabbixTemplateFormat(value) {
|
||||
if (typeof value === 'string') {
|
||||
return utils.escapeRegex(value);
|
||||
}
|
||||
@@ -680,17 +677,6 @@ function replaceTemplateVars(templateSrv, target, scopedVars) {
|
||||
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) {
|
||||
return _.filter(targets, target => {
|
||||
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
|
||||
if (!_.includes) {_.includes = _.contains;}
|
||||
if (!_.keyBy) {_.keyBy = _.indexBy;}
|
||||
|
||||
@@ -2,10 +2,14 @@ import angular from 'angular';
|
||||
import _ from 'lodash';
|
||||
import $ from 'jquery';
|
||||
|
||||
/** @ngInject */
|
||||
const DOCS_FUNC_REF_URL = 'http://docs.grafana-zabbix.org/reference/functions/';
|
||||
|
||||
angular
|
||||
.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 paramTemplate = '<input type="text" style="display:none"' +
|
||||
@@ -221,7 +225,7 @@ angular
|
||||
}
|
||||
|
||||
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');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import {loadPluginCss} from 'app/plugins/sdk';
|
||||
import {ZabbixAPIDatasource} from './datasource';
|
||||
import {ZabbixQueryController} from './query.controller';
|
||||
import {ZabbixDSConfigController} from './config.controller';
|
||||
import { loadPluginCss } from 'app/plugins/sdk';
|
||||
import { ZabbixDatasource } from './datasource';
|
||||
import { ZabbixQueryController } from './query.controller';
|
||||
import { ZabbixDSConfigController } from './config.controller';
|
||||
import './zabbixAlerting.service.js';
|
||||
import './add-metric-function.directive';
|
||||
import './metric-function-editor.directive';
|
||||
|
||||
loadPluginCss({
|
||||
dark: 'plugins/alexanderzobnin-zabbix-app/css/grafana-zabbix.dark.css',
|
||||
@@ -14,8 +17,11 @@ ZabbixQueryOptionsController.templateUrl = 'datasource-zabbix/partials/query.opt
|
||||
class ZabbixAnnotationsQueryController {}
|
||||
ZabbixAnnotationsQueryController.templateUrl = 'datasource-zabbix/partials/annotations.editor.html';
|
||||
|
||||
ZabbixQueryController.templateUrl = 'datasource-zabbix/partials/query.editor.html';
|
||||
ZabbixDSConfigController.templateUrl = 'datasource-zabbix/partials/config.html';
|
||||
|
||||
export {
|
||||
ZabbixAPIDatasource as Datasource,
|
||||
ZabbixDatasource as Datasource,
|
||||
ZabbixDSConfigController as ConfigCtrl,
|
||||
ZabbixQueryController as QueryCtrl,
|
||||
ZabbixQueryOptionsController as QueryOptionsCtrl,
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import {QueryCtrl} from 'app/plugins/sdk';
|
||||
import { QueryCtrl } from 'app/plugins/sdk';
|
||||
import _ from 'lodash';
|
||||
import * as c from './constants';
|
||||
import * as utils from './utils';
|
||||
import * as metricFunctions from './metricFunctions';
|
||||
import * as migrations from './migrations';
|
||||
|
||||
import './add-metric-function.directive';
|
||||
import './metric-function-editor.directive';
|
||||
|
||||
export class ZabbixQueryController extends QueryCtrl {
|
||||
|
||||
// ZabbixQueryCtrl constructor
|
||||
@@ -333,6 +330,3 @@ export class ZabbixQueryController extends QueryCtrl {
|
||||
this.targetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
// Set templateUrl as static property
|
||||
ZabbixQueryController.templateUrl = 'datasource-zabbix/partials/query.editor.html';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
import Q, { Promise } from "q";
|
||||
import {Datasource} from "../module";
|
||||
import {zabbixTemplateFormat} from "../datasource";
|
||||
import { Datasource } from "../module";
|
||||
import { zabbixTemplateFormat } from "../datasource";
|
||||
|
||||
describe('ZabbixDatasource', () => {
|
||||
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
|
||||
if (!_.includes) {
|
||||
_.includes = _.contains;
|
||||
|
||||
@@ -2,6 +2,10 @@ import _ from 'lodash';
|
||||
|
||||
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 {
|
||||
constructor(options, backendSrv, datasourceSrv) {
|
||||
this.backendSrv = backendSrv;
|
||||
@@ -24,14 +28,23 @@ export default class DBConnector {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send test request to datasource in order to ensure it's working.
|
||||
*/
|
||||
testDataSource() {
|
||||
throw NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get history data from external sources.
|
||||
*/
|
||||
getHistory() {
|
||||
throw NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get trends data from external sources.
|
||||
*/
|
||||
getTrends() {
|
||||
throw NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user