1/**
2 * Controller for systemOverview
3 *
4 * @module app/overview
5 * @exports systemOverviewController
6 * @name systemOverviewController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11
12  angular.module('app.overview').controller('systemOverviewController', [
13    '$scope', '$window', 'APIUtils', 'dataService', 'Constants', '$q',
14    function($scope, $window, APIUtils, dataService, Constants, $q) {
15      $scope.dataService = dataService;
16      $scope.dropdown_selected = false;
17      $scope.logs = [];
18      $scope.server_info = {};
19      $scope.bmc_firmware = '';
20      $scope.bmc_time = '';
21      $scope.server_firmware = '';
22      $scope.power_consumption = '';
23      $scope.power_cap = '';
24      $scope.bmc_ip_addresses = [];
25      $scope.loading = false;
26      $scope.edit_hostname = false;
27
28      loadOverviewData();
29
30      function loadOverviewData() {
31        $scope.loading = true;
32
33        var getLogsPromise = APIUtils.getLogs().then(
34            function(data) {
35              $scope.logs = data.data.filter(function(log) {
36                return log.severity_flags.high == true;
37              });
38            },
39            function(error) {
40              console.log(JSON.stringify(error));
41            });
42
43        var getFirmwaresPromise = APIUtils.getFirmwares().then(
44            function(data) {
45              $scope.bmc_firmware = data.bmcActiveVersion;
46              $scope.server_firmware = data.hostActiveVersion;
47            },
48            function(error) {
49              console.log(JSON.stringify(error));
50            });
51
52        var getLEDStatePromise = APIUtils.getLEDState().then(
53            function(data) {
54              if (data == APIUtils.LED_STATE.on) {
55                dataService.LED_state = APIUtils.LED_STATE_TEXT.on;
56              } else {
57                dataService.LED_state = APIUtils.LED_STATE_TEXT.off;
58              }
59            },
60            function(error) {
61              console.log(JSON.stringify(error));
62            });
63
64        var getBMCTimePromise = APIUtils.getBMCTime().then(
65            function(data) {
66              $scope.bmc_time = data.data.Elapsed / 1000;
67            },
68            function(error) {
69              console.log(JSON.stringify(error));
70            });
71
72        var getServerInfoPromise = APIUtils.getServerInfo().then(
73            function(data) {
74              $scope.server_info = data.data;
75            },
76            function(error) {
77              console.log(JSON.stringify(error));
78            });
79
80        var getPowerConsumptionPromise = APIUtils.getPowerConsumption().then(
81            function(data) {
82              $scope.power_consumption = data;
83            },
84            function(error) {
85              console.log(JSON.stringify(error));
86            });
87
88        var getPowerCapPromise = APIUtils.getPowerCap().then(
89            function(data) {
90              if (data.data.PowerCapEnable == false) {
91                $scope.power_cap = Constants.POWER_CAP_TEXT.disabled;
92              } else {
93                $scope.power_cap =
94                    data.data.PowerCap + ' ' + Constants.POWER_CAP_TEXT.unit;
95              }
96            },
97            function(error) {
98              console.log(JSON.stringify(error));
99            });
100
101        var getNetworkInfoPromise = APIUtils.getNetworkInfo().then(
102            function(data) {
103              // TODO: openbmc/openbmc#3150 Support IPV6 when
104              // officially supported by the backend
105              $scope.bmc_ip_addresses = data.formatted_data.ip_addresses.ipv4;
106            },
107            function(error) {
108              console.log(JSON.stringify(error));
109            });
110
111        var promises = [
112          getLogsPromise,
113          getFirmwaresPromise,
114          getLEDStatePromise,
115          getBMCTimePromise,
116          getServerInfoPromise,
117          getPowerConsumptionPromise,
118          getPowerCapPromise,
119          getNetworkInfoPromise,
120        ];
121
122        $q.all(promises).finally(function() {
123          $scope.loading = false;
124        });
125      }
126
127      $scope.toggleLED = function() {
128        var toggleState =
129            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
130            APIUtils.LED_STATE.off :
131            APIUtils.LED_STATE.on;
132        dataService.LED_state =
133            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
134            APIUtils.LED_STATE_TEXT.off :
135            APIUtils.LED_STATE_TEXT.on;
136        APIUtils.setLEDState(toggleState, function(status) {});
137      };
138
139      $scope.saveHostname = function(hostname) {
140        $scope.edit_hostname = false;
141        $scope.loading = true;
142        APIUtils.setHostname(hostname).then(
143            function(data) {
144              APIUtils.getNetworkInfo().then(function(data) {
145                dataService.setNetworkInfo(data);
146              });
147            },
148            function(error) {
149              console.log(error);
150            });
151        $scope.loading = false;
152      };
153
154      $scope.getEventLogTitle = function(event) {
155        var title = event.type;
156        if ((event.eventID != 'None') && (event.description != 'None')) {
157          title = event.eventID + ': ' + event.description;
158        }
159        return title;
160      };
161    }
162  ]);
163})(angular);
164