Refactoring - reformat code.

This commit is contained in:
Alexander Zobnin
2015-07-25 18:48:08 +03:00
parent f53ebaa9fc
commit 9855c05f64

View File

@@ -1,250 +1,249 @@
define([ define([
'angular', 'angular',
'lodash', 'lodash',
'./helperFunctions' './helperFunctions'
], ],
function (angular, _) { function (angular, _) {
'use strict'; 'use strict';
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
module.controller('ZabbixAPIQueryCtrl', function($scope, $sce, templateSrv, zabbixHelperSrv) { module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, zabbixHelperSrv) {
$scope.init = function() { $scope.init = function () {
$scope.targetLetters = targetLetters; $scope.targetLetters = targetLetters;
if ($scope.target.ITService) { if ($scope.target.ITService) {
$scope.slaPropertyList = [ $scope.slaPropertyList = [
{name: "SLA", property: "sla"}, {name: "SLA", property: "sla"},
{name: "OK time", property: "okTime"}, {name: "OK time", property: "okTime"},
{name: "Problem time", property: "problemTime"}, {name: "Problem time", property: "problemTime"},
{name: "Down time", property: "downtimeTime"} {name: "Down time", property: "downtimeTime"}
]; ];
$scope.itserviceList = [{name: "test"}]; $scope.itserviceList = [{name: "test"}];
$scope.updateITServiceList(); $scope.updateITServiceList();
} else { } else {
$scope.metric = { $scope.metric = {
hostGroupList: [], hostGroupList: [],
hostList: [{name: '*', visible_name: 'All'}], hostList: [{name: '*', visible_name: 'All'}],
applicationList: [{name: '*', visible_name: 'All'}], applicationList: [{name: '*', visible_name: 'All'}],
itemList: [{name: 'All'}] itemList: [{name: 'All'}]
}; };
// Update host group, host, application and item lists // Update host group, host, application and item lists
$scope.updateGroupList(); $scope.updateGroupList();
$scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList();
setItemAlias();
}
$scope.target.errors = validateTarget($scope.target);
};
$scope.switchEditorMode = function () {
$scope.target.ITService = !$scope.target.ITService;
$scope.init();
};
/**
* Take alias from item name by default
*/
function setItemAlias() {
if (!$scope.target.alias && $scope.target.item) {
$scope.target.alias = $scope.target.item.name;
}
}
$scope.targetBlur = function () {
setItemAlias();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when IT service is selected.
*/
$scope.selectITService = function () {
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host group selected
*/
$scope.selectHostGroup = function () {
$scope.updateHostList(); $scope.updateHostList();
$scope.updateAppList(); $scope.updateAppList();
$scope.updateItemList(); $scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host selected
*/
$scope.selectHost = function () {
$scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when application selected
*/
$scope.selectApplication = function () {
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when item selected
*/
$scope.selectItem = function () {
setItemAlias(); setItemAlias();
} $scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
$scope.target.errors = validateTarget($scope.target); $scope.duplicate = function () {
}; var clone = angular.copy($scope.target);
$scope.panel.targets.push(clone);
};
$scope.switchEditorMode = function() { $scope.moveMetricQuery = function (fromIndex, toIndex) {
$scope.target.ITService = !$scope.target.ITService; _.move($scope.panel.targets, fromIndex, toIndex);
$scope.init(); };
};
/** //////////////////////////////
* Take alias from item name by default // SUGGESTION QUERIES
*/ //////////////////////////////
function setItemAlias() {
if (!$scope.target.alias && $scope.target.item) {
$scope.target.alias = $scope.target.item.name;
}
}
$scope.targetBlur = function() { /**
setItemAlias(); * Update list of IT services
$scope.target.errors = validateTarget($scope.target); */
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) { $scope.updateITServiceList = function () {
$scope.oldTarget = angular.copy($scope.target); $scope.datasource.zabbixAPI.getITService().then(function (iteservices) {
$scope.get_data(); $scope.itserviceList = [];
} $scope.itserviceList = $scope.itserviceList.concat(iteservices);
};
/**
* Call when IT service is selected.
*/
$scope.selectITService = function() {
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host group selected
*/
$scope.selectHostGroup = function() {
$scope.updateHostList();
$scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when host selected
*/
$scope.selectHost = function() {
$scope.updateAppList();
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when application selected
*/
$scope.selectApplication = function() {
$scope.updateItemList();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
/**
* Call when item selected
*/
$scope.selectItem = function() {
setItemAlias();
$scope.target.errors = validateTarget($scope.target);
if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.get_data();
}
};
$scope.duplicate = function() {
var clone = angular.copy($scope.target);
$scope.panel.targets.push(clone);
};
$scope.moveMetricQuery = function(fromIndex, toIndex) {
_.move($scope.panel.targets, fromIndex, toIndex);
};
//////////////////////////////
// SUGGESTION QUERIES
//////////////////////////////
/**
* Update list of IT services
*/
$scope.updateITServiceList = function() {
$scope.datasource.zabbixAPI.getITService().then(function (iteservices) {
$scope.itserviceList = [];
$scope.itserviceList = $scope.itserviceList.concat(iteservices);
});
};
/**
* Update list of host groups
*/
$scope.updateGroupList = function() {
$scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) {
$scope.metric.groupList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.groupList);
$scope.metric.groupList = $scope.metric.groupList.concat(groups);
});
};
/**
* Update list of hosts
*/
$scope.updateHostList = function() {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
if (groups) {
$scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) {
$scope.metric.hostList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.hostList);
$scope.metric.hostList = $scope.metric.hostList.concat(hosts);
}); });
} };
};
/** /**
* Update list of host applications * Update list of host groups
*/ */
$scope.updateAppList = function() { $scope.updateGroupList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; $scope.datasource.zabbixAPI.performHostGroupSuggestQuery().then(function (groups) {
var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; $scope.metric.groupList = [{name: '*', visible_name: 'All'}];
if (groups && hosts) { addTemplatedVariables($scope.metric.groupList);
$scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) { $scope.metric.groupList = $scope.metric.groupList.concat(groups);
apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) { });
return {name: appname}; };
/**
* Update list of hosts
*/
$scope.updateHostList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
if (groups) {
$scope.datasource.zabbixAPI.hostFindQuery(groups).then(function (hosts) {
$scope.metric.hostList = [{name: '*', visible_name: 'All'}];
addTemplatedVariables($scope.metric.hostList);
$scope.metric.hostList = $scope.metric.hostList.concat(hosts);
}); });
$scope.metric.applicationList = [{name: '*', visible_name: 'All'}]; }
addTemplatedVariables($scope.metric.applicationList); };
$scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
});
}
};
/** /**
* Update list of items * Update list of host applications
*/ */
$scope.updateItemList = function() { $scope.updateAppList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined; var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined; var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
var apps = $scope.target.application ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined; if (groups && hosts) {
if (groups && hosts && apps) { $scope.datasource.zabbixAPI.appFindQuery(hosts, groups).then(function (apps) {
$scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps).then(function (items) { apps = _.map(_.uniq(_.map(apps, 'name')), function (appname) {
// Show only unique item names return {name: appname};
var uniq_items = _.map(_.uniq(items, function (item) { });
return zabbixHelperSrv.expandItemName(item); $scope.metric.applicationList = [{name: '*', visible_name: 'All'}];
}), function (item) { addTemplatedVariables($scope.metric.applicationList);
return {name: zabbixHelperSrv.expandItemName(item)}; $scope.metric.applicationList = $scope.metric.applicationList.concat(apps);
});
}
};
/**
* Update list of items
*/
$scope.updateItemList = function () {
var groups = $scope.target.group ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.group.name)) : undefined;
var hosts = $scope.target.host ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.host.name)) : undefined;
var apps = $scope.target.application ? zabbixHelperSrv.splitMetrics(templateSrv.replace($scope.target.application.name)) : undefined;
if (groups && hosts && apps) {
$scope.datasource.zabbixAPI.itemFindQuery(groups, hosts, apps).then(function (items) {
// Show only unique item names
var uniq_items = _.map(_.uniq(items, function (item) {
return zabbixHelperSrv.expandItemName(item);
}), function (item) {
return {name: zabbixHelperSrv.expandItemName(item)};
});
$scope.metric.itemList = [{name: 'All'}];
addTemplatedVariables($scope.metric.itemList);
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
});
}
};
/**
* Add templated variables to list of available metrics
*
* @param {Array} metricList List of metrics which variables add to
*/
function addTemplatedVariables(metricList) {
_.each(templateSrv.variables, function (variable) {
metricList.push({
name: '$' + variable.name,
templated: true
}); });
$scope.metric.itemList = [{name: 'All'}];
addTemplatedVariables($scope.metric.itemList);
$scope.metric.itemList = $scope.metric.itemList.concat(uniq_items);
}); });
} }
};
/** //////////////////////////////
* Add templated variables to list of available metrics // VALIDATION
* //////////////////////////////
* @param {Array} metricList List of metrics which variables add to
*/
function addTemplatedVariables(metricList) {
_.each(templateSrv.variables, function(variable) {
metricList.push({
name: '$' + variable.name,
templated: true
});
});
}
////////////////////////////// function validateTarget(target) {
// VALIDATION var errs = {};
////////////////////////////// if (!target) {
errs = 'Not defined';
function validateTarget(target) { }
var errs = {}; return errs;
if (!target) {
errs = 'Not defined';
} }
return errs;
} });
}); });
});