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