blob: 9398259b0c660466cf9ad51f903481c71efc25d9 [file] [log] [blame]
Gunnar Mills52b8bde2018-06-21 13:16:54 -05001/**
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', [
beccabroek27ce84d2019-02-05 15:43:17 -060013 '$scope', '$window', 'APIUtils', '$route', '$q', 'toastService',
14 function($scope, $window, APIUtils, $route, $q, toastService) {
Gunnar Mills52b8bde2018-06-21 13:16:54 -050015 $scope.power_consumption = '';
Gunnar Mills006aaa02018-06-26 16:08:59 -050016 $scope.power_cap = {};
Gunnar Mills52b8bde2018-06-21 13:16:54 -050017 $scope.loading = false;
18 loadPowerData();
19
20 function loadPowerData() {
21 $scope.loading = true;
Gunnar Mills006aaa02018-06-26 16:08:59 -050022
23 var getPowerCapPromise = APIUtils.getPowerCap().then(
24 function(data) {
25 $scope.power_cap = data.data;
Gunnar Mills006aaa02018-06-26 16:08:59 -050026 },
27 function(error) {
28 console.log(JSON.stringify(error));
29 });
30
Gunnar Mills52b8bde2018-06-21 13:16:54 -050031 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,
Gunnar Mills006aaa02018-06-26 16:08:59 -050041 getPowerCapPromise,
Gunnar Mills52b8bde2018-06-21 13:16:54 -050042 ];
43
44 $q.all(promises).finally(function() {
45 $scope.loading = false;
46 });
47 }
Gunnar Mills006aaa02018-06-26 16:08:59 -050048
49 $scope.setPowerCap = function() {
Gunnar Mills006aaa02018-06-26 16:08:59 -050050 // The power cap value will be undefined if outside range
51 if (!$scope.power_cap.PowerCap) {
beccabroek27ce84d2019-02-05 15:43:17 -060052 toastService.error(
53 'Power cap value between 100 and 10,000 is required');
Gunnar Mills006aaa02018-06-26 16:08:59 -050054 return;
55 }
56 $scope.loading = true;
57 var promises = [
58 setPowerCapValue(),
59 setPowerCapEnable(),
60 ];
61
beccabroek2264b422019-01-07 11:43:13 -060062 $q.all(promises)
63 .then(
64 function() {
beccabroek27ce84d2019-02-05 15:43:17 -060065 toastService.success('Power cap settings saved');
beccabroek2264b422019-01-07 11:43:13 -060066 },
67 function(errors) {
beccabroek27ce84d2019-02-05 15:43:17 -060068 toastService.error('Power cap settings could not be saved');
beccabroek2264b422019-01-07 11:43:13 -060069 })
70 .finally(function() {
71 $scope.loading = false;
72 });
Gunnar Mills006aaa02018-06-26 16:08:59 -050073 };
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) {
Gunnar Mills006aaa02018-06-26 16:08:59 -050083 console.log(JSON.stringify(error));
beccabroek2264b422019-01-07 11:43:13 -060084 return $q.reject();
Gunnar Mills006aaa02018-06-26 16:08:59 -050085 });
86 }
87
88 function setPowerCapEnable() {
89 return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable)
90 .then(
91 function(data) {},
92 function(error) {
Gunnar Mills006aaa02018-06-26 16:08:59 -050093 console.log(JSON.stringify(error));
beccabroek2264b422019-01-07 11:43:13 -060094 return $q.reject();
Gunnar Mills006aaa02018-06-26 16:08:59 -050095 });
96 }
Gunnar Mills52b8bde2018-06-21 13:16:54 -050097 }
98 ]);
Gunnar Mills52b8bde2018-06-21 13:16:54 -050099})(angular);