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.tmz = 'EDT';
18      $scope.logs = [];
19      $scope.server_info = {};
20      $scope.bmc_firmware = '';
21      $scope.bmc_time = '';
22      $scope.server_firmware = '';
23      $scope.power_consumption = '';
24      $scope.power_cap = '';
25      $scope.bmc_ip_addresses = [];
26      $scope.loading = false;
27      $scope.edit_hostname = false;
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            },
108            function(error) {
109              console.log(JSON.stringify(error));
110            });
111
112        var promises = [
113          getLogsPromise,
114          getFirmwaresPromise,
115          getLEDStatePromise,
116          getBMCTimePromise,
117          getServerInfoPromise,
118          getPowerConsumptionPromise,
119          getPowerCapPromise,
120          getNetworkInfoPromise,
121        ];
122
123        $q.all(promises).finally(function() {
124          $scope.loading = false;
125        });
126      }
127
128      $scope.toggleLED = function() {
129        var toggleState =
130            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
131            APIUtils.LED_STATE.off :
132            APIUtils.LED_STATE.on;
133        dataService.LED_state =
134            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
135            APIUtils.LED_STATE_TEXT.off :
136            APIUtils.LED_STATE_TEXT.on;
137        APIUtils.setLEDState(toggleState, function(status) {});
138      };
139
140      $scope.saveHostname = function(hostname) {
141        $scope.edit_hostname = false;
142        $scope.loading = true;
143        APIUtils.setHostname(hostname).then(
144            function(data) {
145              APIUtils.getNetworkInfo().then(function(data) {
146                dataService.setNetworkInfo(data);
147              });
148            },
149            function(error) {
150              console.log(error);
151            });
152        $scope.loading = false;
153      };
154
155      $scope.getEventLogTitle = function(event) {
156        var title = event.type;
157        if ((event.eventID != 'None') && (event.description != 'None')) {
158          title = event.eventID + ': ' + event.description;
159        }
160        return title;
161      };
162    }
163  ]);
164
165})(angular);
166