-
Notifications
You must be signed in to change notification settings - Fork 575
/
index.js
231 lines (205 loc) · 7.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* @author Alejandro Galue <agalue@opennms.org>
* @copyright 2016 The OpenNMS Group, Inc.
*/
'use strict';
const angular = require('vendor/angular-js');
const _ = require('underscore');
require('lib/onms-http');
require('apps/onms-default-apps');
angular.module('onms-resources', [
'onms.http',
'onms.default.apps',
'ui.bootstrap',
'angular-growl'
])
.config(['growlProvider', function(growlProvider) {
growlProvider.globalTimeToLive(3000);
growlProvider.globalPosition('bottom-center');
}])
.filter('startFrom', function() {
return function(input, _start) {
const start = Number(_start);
if (input) {
return input.length < start ? input : input.slice(start);
}
return [];
};
})
.controller('NodeListCtrl', ['$scope', '$filter', '$http', '$window', 'growl', function($scope, $filter, $http, $window, growl) {
$scope.endUrl = 'graph/results.htm';
$scope.resources = [];
$scope.filteredResources = [];
$scope.pageSize = 10;
$scope.maxSize = 5;
$scope.totalItems = 0;
$scope.hasResources = false;
$scope.loaded = false;
$scope.goTo = function(id) {
$window.location.href = getBaseHref() + 'graph/chooseresource.jsp?reports=all&parentResourceId=' + id + '&endUrl=' + $scope.endUrl;
};
$scope.update = function() {
$scope.currentPage = 1;
$scope.totalItems = $scope.filteredResources.length;
$scope.numPages = Math.ceil($scope.totalItems / $scope.pageSize);
};
$http.get('rest/resources?depth=0').then(function succeeded(response) {
var data = response.data;
$scope.loaded = true;
$scope.hasResources = data.resource.length > 0;
$scope.resources = data.resource;
$scope.filteredResources = $scope.resources;
$scope.update();
}, function errorCallback(response) {
$scope.loaded = true;
growl.error('There was a problem in retrieving resources through ReST', {ttl: 10000});
});
$scope.$watch('resourceFilter', function() {
$scope.filteredResources = $filter('filter')($scope.resources, $scope.resourceFilter);
$scope.update();
});
}])
.controller('NodeResourcesCtrl', ['$scope', '$filter', '$http', '$window', 'growl', function($scope, $filter, $http, $window, growl) {
$scope.searchQuery = undefined;
$scope.resources = {};
$scope.hasResources = false;
$scope.filteredResources = {};
$scope.isCollapsed = {};
$scope.nodeLink = undefined;
$scope.nodeLabel = undefined;
$scope.nodeCriteria = undefined;
$scope.url = 'graph/results.htm';
$scope.reports = 'all';
$scope.loaded = false;
$scope.generatedId = '';
$scope.init = function(nodeCriteria, reports, endUrl) {
if (!nodeCriteria) {
return;
}
// Update node criteria in scope.
$scope.nodeCriteria = nodeCriteria;
if (reports) {
$scope.reports = reports;
}
if (endUrl) {
$scope.url = endUrl;
}
$http.get('rest/resources/fornode/'+nodeCriteria).then(function succeeded(response) {
var data = response.data;
$scope.nodeLink = data.link;
$scope.nodeLabel = data.label;
$scope.loaded = true;
$scope.hasResources = data.children.resource.length > 0;
var reduced = _.map(data.children.resource, function (obj) {
var resource = {
id: obj.id,
label: obj.label,
typeLabel: obj.typeLabel,
checked: false,
ifIndex: parseInt(obj.externalValueAttributes.ifIndex, 10), // will return NaN if not set
hasFlows: typeof obj.externalValueAttributes.hasFlows === 'undefined' ? false : JSON.parse(obj.externalValueAttributes.hasFlows),
hasIngressFlows: typeof obj.externalValueAttributes.hasIngressFlows === 'undefined' ? false : JSON.parse(obj.externalValueAttributes.hasIngressFlows),
hasEgressFlows: typeof obj.externalValueAttributes.hasEgressFlows === 'undefined' ? false : JSON.parse(obj.externalValueAttributes.hasEgressFlows)
};
$scope.updateFlowUrlForResource(nodeCriteria, resource);
return resource;
});
$scope.resources = _.groupBy(_.sortBy(reduced, function(r) {
var type = r['typeLabel'];
return (type === 'SNMP Node Data' || type === 'SNMP Interface Data') ? Infinity : type;
}), 'typeLabel');
// Perform a shallow copy of the resource map - the resources may be updated asynchronously
// with additional attributes
$scope.filteredResources = {};
for (var k in $scope.resources) {
if (Object.prototype.hasOwnProperty.call($scope.resources, k)) {
$scope.filteredResources[k] = $scope.resources[k];
}
}
}, function errorCallback(response) {
$scope.loaded = true;
growl.error('There was a problem in retrieving resources through ReST', {ttl: 10000});
});
};
$scope.updateFlowUrlForResource = function(nodeCriteria, resource) {
if ((!resource.hasIngressFlows && !resource.hasEgressFlows) || isNaN(resource.ifIndex)) {
// No flows, or not an interface, nothing to do
return;
}
$http({
url: 'rest/flows/flowGraphUrl',
method: 'GET',
params: {
exporterNode: nodeCriteria,
ifIndex: resource.ifIndex
}
}).then(function succeeded(response) {
// Update the flowGraphUrl on the associated resource
resource.flowGraphUrl = response.data.flowGraphUrl;
}, function errorCallback(response) {
// pass
});
};
$scope.checkAll = function(check) {
for (var key in $scope.resources) {
if ($scope.resources.hasOwnProperty(key)) {
_.each($scope.filteredResources[key], function(r) {
r.selected = check;
});
}
}
};
$scope.graphSelected = function() {
var selected = [];
for (var key in $scope.resources) {
if ($scope.resources.hasOwnProperty(key)) {
_.each($scope.filteredResources[key], function(r) {
if (r.selected) {
selected.push(r.id);
}
});
}
}
$scope.doGraph(selected);
};
$scope.graphAll = function() {
// Graph All will render all graphs for specific node. Controller will fetch specific resources.
if ($scope.nodeCriteria) {
$window.location.href = getBaseHref() + $scope.url + '?nodeCriteria=' + $scope.nodeCriteria + ($scope.reports ? '&reports=' + $scope.reports : '');
} else {
growl.error('Invalid node.');
}
};
$scope.doGraph = function (selected) {
// Save resources with an ID and form url with generatedId.
if (selected.length > 0) {
$http.post('rest/resources/generateId', selected)
.success(function (response) {
$scope.generatedId = response;
if ($scope.generatedId) {
$window.location.href = getBaseHref() + $scope.url + '?generatedId=' + $scope.generatedId + ($scope.reports ? '&reports=' + $scope.reports : '');
} else {
$scope.setResourceIds(selected);
}
}).error(function (error, status) {
$scope.setResourceIds(selected);
});
} else {
growl.error('Please select at least one resource.');
}
};
$scope.setResourceIds = function (selected) {
for (var i = 0; i < selected.length; i++) {
selected[i] = 'resourceId=' + selected[i];
}
$window.location.href = getBaseHref() + $scope.url + '?' + selected.join('&') + ($scope.reports ? '&reports=' + $scope.reports : '');
};
$scope.$watch('searchQuery', function() {
$scope.filteredResources = {};
for (var key in $scope.resources) {
if ($scope.resources.hasOwnProperty(key)) {
$scope.filteredResources[key] = $filter('filter')($scope.resources[key], $scope.searchQuery);
}
}
});
}]);