/
Requisitions.js
532 lines (494 loc) · 17.9 KB
/
Requisitions.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* Licensed to The OpenNMS Group, Inc (TOG) under one or more
* contributor license agreements. See the LICENSE.md file
* distributed with this work for additional information
* regarding copyright ownership.
*
* TOG licenses this file to You under the GNU Affero General
* Public License Version 3 (the "License") or (at your option)
* any later version. You may not use this file except in
* compliance with the License. You may obtain a copy of the
* License at:
*
* https://www.gnu.org/licenses/agpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
const bootbox = require('bootbox');
require('../services/Requisitions');
require('../services/Synchronize');
/**
* @author Alejandro Galue <agalue@opennms.org>
* @copyright 2014-2022 The OpenNMS Group, Inc.
*/
(function() {
'use strict';
const quickAddNodeView = require('../../views/quick-add-node.html');
const cloneForeignsourceView = require('../../views/clone-foreignsource.html');
angular.module('onms-requisitions')
.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}])
/**
* @ngdoc controller
* @name RequisitionsController
* @module onms-requisitions
*
* @description The controller for manage all the requisitions (list/add/remove/synchronize)
*
* @requires $scope Angular local scope
* @requires $filter Angular filter
* @requires $cookies Angular cookies
* @requires $window Document window
* @requires $uibModal Angular UI modal
* @required Configuration The configuration object
* @requires RequisitionsService The requisitions service
* @requires SynchronizeService The synchronize service
* @requires growl The growl plugin for instant notifications
*/
.controller('RequisitionsController', ['$scope', '$filter', '$cookies', '$window', '$uibModal', 'Configuration', 'DateFormatterService', 'RequisitionsService', 'SynchronizeService', 'growl', function($scope, $filter, $cookies, $window, $uibModal, Configuration, DateFormatterService, RequisitionsService, SynchronizeService, growl) {
/**
* @description The timing status.
*
* @ngdoc property
* @name RequisitionsController#timingStatus
* @propertyOf RequisitionsController
* @returns {object} The timing status object
*/
$scope.timingStatus = RequisitionsService.getTiming();
/**
* @description The load flag.
*
* @ngdoc property
* @name RequisitionsController#loaded
* @propertyOf RequisitionsController
* @returns {boolean} True, if the requisitions have been loaded.
*/
$scope.loaded = false;
/**
* @description The requisitions data
*
* @ngdoc property
* @name RequisitionsController#requisitionsData
* @propertyOf RequisitionsController
* @returns {object} The requisitions data
*/
$scope.requisitionsData = { requisitions: [] };
/**
* @description The filtered version of the requisitions list
*
* @ngdoc property
* @name RequisitionsController#filteredRequisitions
* @propertyOf RequisitionsController
* @returns {array} The filtered array
*/
$scope.filteredRequisitions = [];
/**
* @description The amount of items per page for pagination (defaults to 10)
*
* @ngdoc property
* @name RequisitionsController#pageSize
* @propertyOf RequisitionsController
* @returns {integer} The page size
*/
$scope.pageSize = 10;
/**
* @description The maximum size of pages for pagination (defaults to 5)
*
* @ngdoc property
* @name RequisitionControllers#maxSize
* @propertyOf RequisitionsController
* @returns {integer} The maximum size
*/
$scope.maxSize = 5;
/**
* @description The total amount of items for pagination (defaults to 0)
*
* @ngdoc property
* @name RequisitionControllers#maxSize
* @propertyOf RequisitionsController
* @returns {integer} The total items
*/
$scope.totalItems = 0;
/**
* @description Shows an error to the user
*
* @name RequisitionsController:errorHandler
* @ngdoc method
* @methodOf RequisitionsController
* @param {string} message The error message
*/
$scope.errorHandler = function(message) {
growl.error(message, {ttl: 10000});
};
/**
* @description Quick add a new node
*
* @name RequisitionsController:quickAddNode
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.quickAddNode = function() {
var availableForeignSources = [];
angular.forEach($scope.requisitionsData.requisitions, function(r) {
availableForeignSources.push(r.foreignSource);
});
var modalInstance = $uibModal.open({
backdrop: 'static',
keyboard: false,
controller: 'QuickAddNodeModalController',
templateUrl: quickAddNodeView,
size: 'lg',
resolve: {
foreignSources: function() { return availableForeignSources; }
}
});
modalInstance.result.then(function(node) {
var r = $scope.requisitionsData.getRequisition(node.foreignSource);
r.setNode(node);
});
};
/**
* @description Clones the detectors and policies of a specific requisition
*
* @name RequisitionsController:clone
* @ngdoc method
* @methodOf RequisitionsController
* @param {string} foreignSource The name of the requisition
*/
$scope.clone = function(foreignSource) {
var availableForeignSources = [];
angular.forEach($scope.requisitionsData.requisitions, function(r) {
if (r.foreignSource !== foreignSource) {
availableForeignSources.push(r.foreignSource);
}
});
var modalInstance = $uibModal.open({
backdrop: 'static',
keyboard: false,
controller: 'CloneForeignSourceController',
templateUrl: cloneForeignsourceView,
resolve: {
foreignSource: function() { return foreignSource; },
availableForeignSources: function() { return availableForeignSources; }
}
});
modalInstance.result.then(function(targetForeignSource) {
bootbox.confirm('This action will override the existing foreign source definition for the requisition named ' + _.escape(targetForeignSource) + ', using ' + _.escape(foreignSource) + ' as a template. Are you sure you want to continue ? This cannot be undone.', function(ok) {
if (!ok) {
return;
}
RequisitionsService.startTiming();
RequisitionsService.cloneForeignSourceDefinition(foreignSource, targetForeignSource).then(
function() { // success
growl.success('The foreign source definition for ' + _.escape(foreignSource) + ' has been cloned to ' + _.escape(targetForeignSource));
},
$scope.errorHandler
);
});
});
};
/**
* @description Adds a new requisition on the server.
*
* A dialog box will be displayed to request the name of the requisition to the user.
*
* @name RequisitionsController:add
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.add = function() {
bootbox.prompt('Please enter the name for the new requisition', function(foreignSource) {
if (foreignSource) {
// Validate Requisition
if (foreignSource.match(/[/\\?:&*'"]/)) {
bootbox.alert('Cannot add the requisition ' + _.escape(foreignSource) + ' because the following characters are invalid:<br/>:, /, \\, ?, &, *, \', "');
return;
}
var r = $scope.requisitionsData.getRequisition(foreignSource);
if (r) {
bootbox.alert('Cannot add the requisition ' + _.escape(foreignSource) + ' because there is already a requisition with that name');
return;
}
// Create Requisition
RequisitionsService.addRequisition(foreignSource).then(
function(r) { // success
growl.success('The requisition ' + _.escape(r.foreignSource) + ' has been created.');
},
$scope.errorHandler
);
}
});
};
/**
* @description Edits the foreign source definition of an existing requisition
*
* @name RequisitionsController:editForeignSource
* @ngdoc method
* @methodOf RequisitionsController
* @param {string} foreignSource The name of the requisition
*/
$scope.editForeignSource = function(foreignSource) {
$window.location.href = Configuration.baseHref + '#/requisitions/' + encodeURIComponent(foreignSource) + '/foreignSource';
};
/**
* @description Goes to the edit page of an existing requisition (navigation)
*
* @name RequisitionsController:edit
* @ngdoc method
* @methodOf RequisitionsController
* @param {string} foreignSource The name of the requisition
*/
$scope.edit = function(foreignSource) {
$window.location.href = Configuration.baseHref + '#/requisitions/' + encodeURIComponent(foreignSource);
};
/**
* @description Requests the synchronization/import of a requisition on the server
*
* A dialog box is displayed to request to the user if the scan phase should be triggered or not.
*
* @name RequisitionsController:synchronize
* @ngdoc method
* @methodOf RequisitionsController
* @param {object} requisition The requisition object
*/
$scope.synchronize = function(requisition) {
RequisitionsService.startTiming();
SynchronizeService.synchronize(requisition, $scope.errorHandler);
};
/**
* @description Refreshes the deployed statistics of a requisition on the server
*
* @name RequisitionsController:refresh
* @ngdoc method
* @methodOf RequisitionsController
* @param {object} requisition The requisition object
*/
$scope.refresh = function(requisition) {
RequisitionsService.startTiming();
RequisitionsService.updateDeployedStatsForRequisition(requisition).then(
function() { // success
growl.success('The deployed statistics for ' + _.escape(requisition.foreignSource) + ' has been updated.');
},
$scope.errorHandler
);
};
/**
* @description Removes all the nodes form the requisition on the server
*
* @name RequisitionsController:removeAllNodes
* @ngdoc method
* @methodOf RequisitionsController
* @param {string} foreignSource The name of the requisition
*/
$scope.removeAllNodes = function(foreignSource) {
bootbox.confirm('Are you sure you want to remove all the nodes from ' + _.escape(foreignSource) + '?', function(ok) {
if (ok) {
RequisitionsService.startTiming();
RequisitionsService.removeAllNodesFromRequisition(foreignSource).then(
function() { // success
growl.success('All the nodes from ' + _.escape(foreignSource) + ' have been removed, and the requisition has been synchronized.');
var req = $scope.requisitionsData.getRequisition(foreignSource);
req.reset();
},
$scope.errorHandler
);
}
});
};
/**
* @description Removes a requisition on the server
*
* @name RequisitionsController:delete
* @ngdoc method
* @methodOf RequisitionsController
* @param {string} foreignSource The name of the requisition
*/
$scope.delete = function(foreignSource) {
bootbox.confirm('Are you sure you want to remove the requisition ' + _.escape(foreignSource) + '?', function(ok) {
if (ok) {
RequisitionsService.startTiming();
RequisitionsService.deleteRequisition(foreignSource).then(
function() { // success
growl.success('The requisition ' + _.escape(foreignSource) + ' has been deleted.');
},
$scope.errorHandler
);
}
});
};
/**
* @description Edits the default foreign source definition (navigation)
*
* @name RequisitionsController:editDefaultForeignSource
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.editDefaultForeignSource = function() {
$window.location.href = Configuration.baseHref + '#/requisitions/default/foreignSource';
};
/**
* @description Resets the default set of detectors and policies
*
* @name RequisitionsController:resetDefaultForeignSource
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.resetDefaultForeignSource = function() {
bootbox.confirm('Are you sure you want to reset the default foreign source definition ?', function(ok) {
if (ok) {
RequisitionsService.startTiming();
RequisitionsService.deleteForeignSourceDefinition('default').then(
function() { // success
growl.success('The default foreign source definition has been reseted.');
$scope.initialize();
},
$scope.errorHandler
);
}
});
};
/**
* @description Refreshes the requisitions from the server
*
* There are two main actions:
* - Retrieve all the requisitions from the server ignoring the current state.
* - Retrieve only the deployed statistics, and update the current requisitions.
*
* @name RequisitionsController:refreshData
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.refreshData = function() {
bootbox.dialog({
message: 'Are you sure you want to refresh the content of the page ?<br/><hr/>' +
'Choose <b>Reload Everything</b> to retrieve all the requisitions from the server (any existing unsaved change will be lost).<br/>' +
'Choose <b>Reload Deployed Data</b> to retrieve the deployed statistics and update the UI.<br/>' +
'Choose <b>Cancel</b> to abort the request.',
title: 'Refresh',
buttons: {
reloadAll: {
label: 'Reload Everything',
className: 'btn-primary',
callback: function() {
$scope.refreshRequisitions();
}
},
reloadDeployed: {
label: 'Reload Deployed Data',
className: 'btn-secondary',
callback: function() {
$scope.refreshDeployedStats();
}
},
main: {
label: 'Cancel',
className: 'btn-secondary'
}
}
});
};
/**
* @description Refreshes the deployed statistics for all the requisitions from the server
*
* @name RequisitionsController:refreshDeployedStats
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.refreshDeployedStats = function() {
RequisitionsService.startTiming();
growl.success('Refreshing deployed statistics...');
RequisitionsService.updateDeployedStats($scope.requisitionsData).then(
function() { // success
growl.success('The deployed statistics has been updated.');
},
$scope.errorHandler
);
};
/**
* @description Refreshes all the requisitions from the server
*
* @name RequisitionsController:refreshRequisitions
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.refreshRequisitions = function() {
bootbox.confirm('Are you sure you want to reload all the requisitions?<br/>All current changes will be lost.', function(ok) {
if (ok) {
RequisitionsService.startTiming();
growl.success('Refreshing requisitions...');
RequisitionsService.clearCache();
$scope.requisitionsData = { requisitions : [] };
$scope.initialize();
}
});
};
/**
* @description Updates the pagination variables for the requisitions.
*
* @name RequisitionsController:updateFilteredNodes
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.updateFilteredRequisitions = function() {
$scope.currentPage = 1;
$scope.totalItems = $scope.filteredRequisitions.length;
};
/**
* @description Saves the page size on a cookie
*
* @name RequisitionController:savePageSize
* @ngdoc method
* @methodOf RequisitionController
*/
$scope.savePageSize = function() {
$cookies.put('requisitions_page_size', $scope.pageSize);
}
/**
* @description Initializes the local requisitions list from the server
*
* @name RequisitionsController:initialize
* @ngdoc method
* @methodOf RequisitionsController
*/
$scope.initialize = function() {
var value = $cookies.get('requisitions_page_size');
if (value) {
$scope.pageSize = value;
}
$scope.loaded = false;
DateFormatterService.formatter.finally(function() {
RequisitionsService.getRequisitions().then(
function(data) { // success
$scope.requisitionsData = data;
$scope.filteredRequisitions = $scope.requisitionsData.requisitions;
$scope.updateFilteredRequisitions();
$scope.loaded = true;
growl.success('Loaded ' + $scope.requisitionsData.requisitions.length + ' requisitions...');
},
$scope.errorHandler
);
});
};
/**
* @description Watch for filter changes in order to update the requisitions list and updates the pagination control
*
* @name RequisitionsController:reqFilter
* @ngdoc event
* @methodOf RequisitionsController
*/
$scope.$watch('reqFilter', function() {
$scope.filteredRequisitions = $filter('filter')($scope.requisitionsData.requisitions, $scope.reqFilter);
$scope.updateFilteredRequisitions();
});
// Initialization
if ($scope.filteredRequisitions.length === 0) {
$scope.initialize();
}
}]);
}());