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', '$q',
14    function($scope, $window, APIUtils, dataService, $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              $scope.power_cap = data;
92            },
93            function(error) {
94              console.log(JSON.stringify(error));
95            });
96
97        var getNetworkInfoPromise = APIUtils.getNetworkInfo().then(
98            function(data) {
99              // TODO: openbmc/openbmc#3150 Support IPV6 when
100              // officially supported by the backend
101              $scope.bmc_ip_addresses = data.formatted_data.ip_addresses.ipv4;
102            },
103            function(error) {
104              console.log(JSON.stringify(error));
105            });
106
107        var promises = [
108          getLogsPromise,
109          getFirmwaresPromise,
110          getLEDStatePromise,
111          getBMCTimePromise,
112          getServerInfoPromise,
113          getPowerConsumptionPromise,
114          getPowerCapPromise,
115          getNetworkInfoPromise,
116        ];
117
118        $q.all(promises).finally(function() {
119          $scope.loading = false;
120        });
121      }
122
123      $scope.toggleLED = function() {
124        var toggleState =
125            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
126            APIUtils.LED_STATE.off :
127            APIUtils.LED_STATE.on;
128        dataService.LED_state =
129            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
130            APIUtils.LED_STATE_TEXT.off :
131            APIUtils.LED_STATE_TEXT.on;
132        APIUtils.setLEDState(toggleState, function(status) {});
133      };
134
135      $scope.saveHostname = function(hostname) {
136        $scope.edit_hostname = false;
137        $scope.loading = true;
138        APIUtils.setHostname(hostname).then(
139            function(data) {
140              APIUtils.getNetworkInfo().then(function(data) {
141                dataService.setNetworkInfo(data);
142              });
143            },
144            function(error) {
145              console.log(error);
146            });
147        $scope.loading = false;
148      };
149
150      $scope.getEventLogTitle = function(event) {
151        var title = event.type;
152        if ((event.eventID != 'None') && (event.description != 'None')) {
153          title = event.eventID + ': ' + event.description;
154        }
155        return title;
156      };
157    }
158  ]);
159
160})(angular);
161