1/**
2 * Controller for power-usage
3 *
4 * @module app/serverControl
5 * @exports powerUsageController
6 * @name powerUsageController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11
12  angular.module('app.serverControl').controller('powerUsageController', [
13    '$scope', '$window', 'APIUtils', '$route', '$q', 'toastService',
14    function($scope, $window, APIUtils, $route, $q, toastService) {
15      $scope.power_consumption = '';
16      $scope.power_cap = {};
17      $scope.loading = false;
18      loadPowerData();
19
20      function loadPowerData() {
21        $scope.loading = true;
22
23        var getPowerCapPromise = APIUtils.getPowerCap().then(
24            function(data) {
25              $scope.power_cap = data.data;
26            },
27            function(error) {
28              console.log(JSON.stringify(error));
29            });
30
31        var getPowerConsumptionPromise = APIUtils.getPowerConsumption().then(
32            function(data) {
33              $scope.power_consumption = data;
34            },
35            function(error) {
36              console.log(JSON.stringify(error));
37            });
38
39        var promises = [
40          getPowerConsumptionPromise,
41          getPowerCapPromise,
42        ];
43
44        $q.all(promises).finally(function() {
45          $scope.loading = false;
46        });
47      }
48
49      $scope.setPowerCap = function() {
50        // The power cap value will be undefined if outside range
51        if (!$scope.power_cap.PowerCap) {
52          toastService.error(
53              'Power cap value between 100 and 10,000 is required');
54          return;
55        }
56        $scope.loading = true;
57        var promises = [
58          setPowerCapValue(),
59          setPowerCapEnable(),
60        ];
61
62        $q.all(promises)
63            .then(
64                function() {
65                  toastService.success('Power cap settings saved');
66                },
67                function(errors) {
68                  toastService.error('Power cap settings could not be saved');
69                })
70            .finally(function() {
71              $scope.loading = false;
72            });
73      };
74      $scope.refresh = function() {
75        $route.reload();
76      };
77
78      function setPowerCapValue() {
79        return APIUtils.setPowerCap($scope.power_cap.PowerCap)
80            .then(
81                function(data) {},
82                function(error) {
83                  console.log(JSON.stringify(error));
84                  return $q.reject();
85                });
86      }
87
88      function setPowerCapEnable() {
89        return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable)
90            .then(
91                function(data) {},
92                function(error) {
93                  console.log(JSON.stringify(error));
94                  return $q.reject();
95                });
96      }
97    }
98  ]);
99})(angular);
100