blob: 70f3e9c0a732300ce4d6990b101e1e6c66d776dd [file] [log] [blame]
Michael Davisaeedf9e2017-04-06 14:35:56 -05001/**
Iftekharul Islamcd789502017-04-19 14:37:55 -05002 * Controller for sensors
Michael Davisaeedf9e2017-04-06 14:35:56 -05003 *
Iftekharul Islamcd789502017-04-19 14:37:55 -05004 * @module app/serverHealth
5 * @exports sensorsController
6 * @name sensorsController
Michael Davisaeedf9e2017-04-06 14:35:56 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
11 var sensorData = [];
12 angular
13 .module('app.overview')
14 .controller('sensorsController', [
15 '$scope',
16 '$log',
17 '$window',
18 'APIUtils',
19 'dataService',
20 '$routeParams',
21 function($scope, $log, $window, APIUtils, dataService, $routeParams) {
22 $scope.dataService = dataService;
23 $scope.customSearch = '';
24 $scope.dropdown_selected = false;
25 $scope.$log = $log;
26 $scope.data = {};
27 $scope.searchTerms = [];
Iftekharul Islamd2269e22017-05-02 09:32:45 -050028
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070029 $scope.selectedSeverity = {
30 all: true,
31 normal: false,
32 warning: false,
33 critical: false
34 };
Iftekharul Islamd2269e22017-05-02 09:32:45 -050035
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070036 var sensorType = $routeParams.type;
Iftekharul Islamd2269e22017-05-02 09:32:45 -050037
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070038 $scope.export_name = sensorType + '_sensors.json';
Iftekharul Islamd2269e22017-05-02 09:32:45 -050039
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070040 $scope.toggleSeverityAll = function() {
41 $scope.selectedSeverity.all = !$scope.selectedSeverity.all;
Iftekharul Islamd2269e22017-05-02 09:32:45 -050042
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070043 if ($scope.selectedSeverity.all) {
44 $scope.selectedSeverity.normal = false;
45 $scope.selectedSeverity.warning = false;
46 $scope.selectedSeverity.critical = false;
47 }
48 };
Iftekharul Islamd2269e22017-05-02 09:32:45 -050049
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070050 $scope.toggleSeverity = function(severity) {
51 $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
Iftekharul Islamd2269e22017-05-02 09:32:45 -050052
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070053 if ($scope.selectedSeverity.normal &&
54 $scope.selectedSeverity.warning &&
55 $scope.selectedSeverity.critical) {
56 $scope.selectedSeverity.all = true;
57 $scope.selectedSeverity.normal = false;
58 $scope.selectedSeverity.warning = false;
59 $scope.selectedSeverity.critical = false;
60 }
61 else {
62 $scope.selectedSeverity.all = false;
63 }
64 };
Iftekharul Islamd2269e22017-05-02 09:32:45 -050065
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070066 $scope.doSearchOnEnter = function(event) {
67 var search = $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
68 if (event.keyCode === 13 &&
69 search.length >= 2) {
70 $scope.searchTerms = $scope.customSearch.split(' ');
71 }
72 else {
73 if (search.length == 0) {
74 $scope.searchTerms = [];
Michael Davisaeedf9e2017-04-06 14:35:56 -050075 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070076 }
77 };
Michael Davisaeedf9e2017-04-06 14:35:56 -050078
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070079 $scope.doSearchOnClick = function() {
80 var search = $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
81 if (search.length >= 2) {
82 $scope.searchTerms = $scope.customSearch.split(' ');
83 }
84 else {
85 if (search.length == 0) {
86 $scope.searchTerms = [];
87 }
88 }
89 };
90
91 $scope.jsonData = function(data) {
92 var dt = {};
93 data.data.forEach(function(item) {
94 dt[item.original_data.key] = item.original_data.value;
95 });
96 return JSON.stringify(dt);
97 };
98
99 $scope.filterBySeverity = function(sensor) {
100 if ($scope.selectedSeverity.all) return true;
101
102 return ((sensor.severity_flags.normal && $scope.selectedSeverity.normal) ||
103 (sensor.severity_flags.warning && $scope.selectedSeverity.warning) ||
104 (sensor.severity_flags.critical && $scope.selectedSeverity.critical)
105 );
106 };
107 $scope.filterBySearchTerms = function(sensor) {
108
109 if (!$scope.searchTerms.length) return true;
110
111 for (var i = 0, length = $scope.searchTerms.length; i < length; i++) {
112 if (sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
113 }
114 return true;
115 };
116
117 function setSensorData() {
118 var data = dataService.sensorData.sensors.filter(function(item) {
119 return item.type == sensorType;
120 });
121 if (data.length) {
122 sensorData = data[0];
123 $scope.data = sensorData;
124 $scope.export_data = $scope.jsonData($scope.data);
125 }
126 }
127
128 if (!dataService.sensorData.sensors) {
129 APIUtils.getAllSensorStatus(function(data, originalData) {
130 dataService.sensorData = data;
131 setSensorData();
132 });
133 }
134 else {
135 setSensorData();
136 }
137
138 }
139 ]);
140
141})(angular);