1/**
2 * Controller for sensors-overview
3 *
4 * @module app/serverHealth
5 * @exports sensorsOverviewController
6 * @name sensorsOverviewController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11  angular.module('app.overview').controller('sensorsOverviewController', [
12    '$scope', '$q', '$window', 'APIUtils', 'dataService', 'Constants',
13    function($scope, $q, $window, APIUtils, dataService, Constants) {
14      $scope.dataService = dataService;
15
16      $scope.loading = false;
17      $scope.componentList = [];
18      $scope.sensorsInfo = {};
19      $scope.fullSensorsInfo = [];
20      $scope.selectedComponent = {};
21      $scope.showThresholds = true;  // TODO: add button to toggle this..
22      $scope.showCompDropdown = false;
23      $scope.customSearch = '';
24      $scope.searchTerms = [];
25      $scope.messages = Constants.MESSAGES.SENSOR;
26      $scope.filteredVoltSensors = [];
27      $scope.filteredTempSensors = [];
28      $scope.filteredFanSensors = [];
29
30      $scope.selectedSeverity =
31          {all: true, ok: false, warning: false, critical: false};
32      $scope.export_name = 'sensors.json';
33
34      $scope.clear = function() {
35        $scope.customSearch = '';
36        $scope.searchTerms = [];
37      };
38
39      $scope.doSearchOnEnter = function(event) {
40        var search =
41            $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
42        if (event.keyCode === 13 && search.length >= 2) {
43          $scope.searchTerms = $scope.customSearch.split(' ');
44        } else {
45          if (search.length == 0) {
46            $scope.searchTerms = [];
47          }
48        }
49      };
50
51      $scope.doSearchOnClick = function() {
52        var search =
53            $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
54        if (search.length >= 2) {
55          $scope.searchTerms = $scope.customSearch.split(' ');
56        } else {
57          if (search.length == 0) {
58            $scope.searchTerms = [];
59          }
60        }
61      };
62
63      $scope.toggleSeverityAll = function() {
64        $scope.selectedSeverity.all = !$scope.selectedSeverity.all;
65
66        if ($scope.selectedSeverity.all) {
67          $scope.selectedSeverity.ok = false;
68          $scope.selectedSeverity.warning = false;
69          $scope.selectedSeverity.critical = false;
70        }
71      };
72
73      $scope.toggleSeverity = function(severity) {
74        $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
75
76        if (['ok', 'warning', 'critical'].indexOf(severity) > -1) {
77          if ($scope.selectedSeverity[severity] == false &&
78              (!$scope.selectedSeverity.ok &&
79               !$scope.selectedSeverity.warning &&
80               !$scope.selectedSeverity.critical)) {
81            $scope.selectedSeverity.all = true;
82            return;
83          }
84        }
85
86        if ($scope.selectedSeverity.ok && $scope.selectedSeverity.warning &&
87            $scope.selectedSeverity.critical) {
88          $scope.selectedSeverity.all = true;
89          $scope.selectedSeverity.ok = false;
90          $scope.selectedSeverity.warning = false;
91          $scope.selectedSeverity.critical = false;
92        } else {
93          $scope.selectedSeverity.all = false;
94        }
95      };
96
97      $scope.filterBySeverity = function(sensor) {
98        if ($scope.selectedSeverity.all) return true;
99
100        return (
101            ((sensor.Status.Health == 'OK') && $scope.selectedSeverity.ok) ||
102            ((sensor.Status.Health == 'Warning') &&
103             $scope.selectedSeverity.warning) ||
104            ((sensor.Status.Health == 'Critical') &&
105             $scope.selectedSeverity.critical));
106      };
107      $scope.filterBySearchTerms = function(sensor) {
108        if (!$scope.searchTerms.length) return true;
109
110        for (var i = 0, length = $scope.searchTerms.length; i < length; i++) {
111          // TODO: Form it while getting data
112          var search_text = sensor.Name.toLowerCase();
113          if (search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1)
114            return false;
115        }
116        return true;
117      };
118
119      $scope.selectComponent = function(index) {
120        $scope.loading = true;
121        $scope.showCompDropdown = false;
122        if (index == -1) {
123          // Flattened sensor data to display all sensors.
124          $scope.selectedComponent = {'Name': 'All'};
125          $scope.sensorsInfo = {'Temperatures': [], 'Fans': [], 'Voltages': []};
126          // Looping through all chassis collections to flattened sensors data
127          angular.forEach($scope.fullSensorsInfo, function(record) {
128            $scope.sensorsInfo.Temperatures = [].concat(
129                $scope.sensorsInfo.Temperatures, record.sensors.Temperatures);
130            $scope.sensorsInfo.Fans =
131                [].concat($scope.sensorsInfo.Fans, record.sensors.Fans);
132            $scope.sensorsInfo.Voltages =
133                [].concat($scope.sensorsInfo.Voltages, record.sensors.Voltages);
134          });
135        } else {
136          $scope.selectedComponent = $scope.fullSensorsInfo[index];
137          $scope.sensorsInfo = $scope.selectedComponent['sensors'];
138        }
139        $scope.loading = false;
140      };
141
142      function getComponentSensors(component) {
143        var data = component;
144        data['sensors'] = {'Temperatures': [], 'Fans': [], 'Voltages': []};
145        APIUtils.getSensorsInfo(component.Thermal['@odata.id'])
146            .then(function(res) {
147              if (res.hasOwnProperty('Temperatures')) {
148                data.sensors['Temperatures'] = res.Temperatures;
149              }
150              if (res.hasOwnProperty('Fans')) {
151                data.sensors['Fans'] = res.Fans;
152              }
153              return;
154            });
155        APIUtils.getSensorsInfo(component.Power['@odata.id'])
156            .then(function(res) {
157              if (res.hasOwnProperty('Voltages')) {
158                data.sensors['Voltages'] = res.Voltages;
159              }
160              return;
161            });
162        return data;
163      };
164
165      $scope.loadSensorData = function() {
166        $scope.loading = true;
167        APIUtils.getAllChassisCollection()
168            .then(
169                function(chassisList) {
170                  angular.forEach(chassisList, function(chassis) {
171                    var resData = getComponentSensors(chassis);
172                    $scope.fullSensorsInfo.push(resData);
173                  });
174                },
175                function(error) {
176                  console.log(JSON.stringify(error));
177                })
178            .finally(function() {
179              $scope.selectComponent(0);
180              $scope.loading = false;
181            });
182      };
183
184      $scope.loadSensorData();
185    }
186  ]);
187})(angular);
188