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', 'ngToast',
14    function($scope, $window, APIUtils, $route, $q, ngToast) {
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          ngToast.danger('Power cap value between 100 and 10,000 is required');
53          return;
54        }
55        $scope.loading = true;
56        var promises = [
57          setPowerCapValue(),
58          setPowerCapEnable(),
59        ];
60
61        $q.all(promises)
62            .then(
63                function() {
64                  ngToast.success('Power cap settings saved');
65                },
66                function(errors) {
67                  ngToast.danger('Power cap settings could not be saved');
68                })
69            .finally(function() {
70              $scope.loading = false;
71            });
72      };
73      $scope.refresh = function() {
74        $route.reload();
75      };
76
77      function setPowerCapValue() {
78        return APIUtils.setPowerCap($scope.power_cap.PowerCap)
79            .then(
80                function(data) {},
81                function(error) {
82                  console.log(JSON.stringify(error));
83                  return $q.reject();
84                });
85      }
86
87      function setPowerCapEnable() {
88        return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable)
89            .then(
90                function(data) {},
91                function(error) {
92                  console.log(JSON.stringify(error));
93                  return $q.reject();
94                });
95      }
96    }
97  ]);
98})(angular);
99