Format api response.

This commit is contained in:
Alexander Zobnin
2016-01-16 19:34:45 +03:00
parent 7b9ddade2e
commit 8d3a00d783
2 changed files with 35 additions and 23 deletions

View File

@@ -120,8 +120,7 @@ function (angular, _) {
p.getGroups = function() { p.getGroups = function() {
var params = { var params = {
output: ['name'], output: ['name'],
sortfield: 'name', sortfield: 'name'
selectHosts: []
}; };
return this.performZabbixAPIRequest('hostgroup.get', params); return this.performZabbixAPIRequest('hostgroup.get', params);
@@ -130,7 +129,8 @@ function (angular, _) {
p.getHosts = function() { p.getHosts = function() {
var params = { var params = {
output: ['name', 'host'], output: ['name', 'host'],
sortfield: 'name' sortfield: 'name',
selectGroups: []
}; };
return this.performZabbixAPIRequest('host.get', params); return this.performZabbixAPIRequest('host.get', params);
@@ -139,7 +139,8 @@ function (angular, _) {
p.getApplications = function() { p.getApplications = function() {
var params = { var params = {
output: ['name'], output: ['name'],
sortfield: 'name' sortfield: 'name',
selectHosts: []
}; };
return this.performZabbixAPIRequest('application.get', params); return this.performZabbixAPIRequest('application.get', params);
@@ -147,16 +148,9 @@ function (angular, _) {
p.getItems = function() { p.getItems = function() {
var params = { var params = {
output: ['name', 'key_', 'value_type'], output: ['name', 'key_', 'value_type', 'hostid', 'status', 'state'],
sortfield: 'name', sortfield: 'name',
//Include web items in the result selectApplications: []
webitems: true,
// Return only numeric items
filter: {
value_type: [0, 3]
},
// Return only enabled items
monitored: true
}; };
return this.performZabbixAPIRequest('item.get', params); return this.performZabbixAPIRequest('item.get', params);

View File

@@ -1,7 +1,7 @@
define([ define([
'angular', 'angular',
'lodash' 'lodash'
], ],
function (angular, _) { function (angular, _) {
'use strict'; 'use strict';
@@ -11,9 +11,22 @@ function (angular, _) {
function ZabbixCache(zabbixAPI, lifetime) { function ZabbixCache(zabbixAPI, lifetime) {
var self = this; var self = this;
this.zabbixAPI = zabbixAPI; this.zabbixAPI = zabbixAPI;
this.lifetime = lifetime; this.lifetime = lifetime;
this._groups = [];
this._hosts = [];
this._applications = [];
this._items = [];
this.refresh();
}
var p = ZabbixCache.prototype;
p.refresh = function () {
var self = this;
var promises = [ var promises = [
this.zabbixAPI.getGroups(), this.zabbixAPI.getGroups(),
this.zabbixAPI.getHosts(), this.zabbixAPI.getHosts(),
@@ -22,17 +35,23 @@ function (angular, _) {
]; ];
$q.all(promises).then(function (results) { $q.all(promises).then(function (results) {
console.log(results);
if (results.length) { if (results.length) {
self._groups = results[0]; self._groups = results[0];
self._hosts = results[1];
self._hosts = _.forEach(results[1], function(host) {
host.groups = _.map(host.groups, 'groupid');
return host;
});
self._applications = groupApplications(results[2]); self._applications = groupApplications(results[2]);
self._items = results[3];
self._items = _.forEach(results[3], function(item) {
item.applications = _.map(item.applications, 'applicationid');
return item;
});
} }
}); });
} };
var p = ZabbixCache.prototype;
p.getGroups = function() { p.getGroups = function() {
return this._groups; return this._groups;
@@ -52,14 +71,13 @@ function (angular, _) {
/** /**
* Group Zabbix applications by name * Group Zabbix applications by name
* @param {[type]} applications [description]
* @return {[type]} [description]
*/ */
function groupApplications(applications) { function groupApplications(applications) {
return _.map(_.groupBy(applications, 'name'), function (value, key) { return _.map(_.groupBy(applications, 'name'), function (value, key) {
return { return {
name: key, name: key,
ids: _.map(value, 'applicationid') applicationids: _.map(value, 'applicationid'),
hostids: _.uniq(_.map(_.flatten(value, 'hosts'), 'hostid'))
}; };
}); });
} }