blob: 5dc848ed68f87de4a2364b2f01cd1ff04da3a560 [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', [
beccabroek2264b422019-01-07 11:43:13 -060013 '$scope', '$window', 'APIUtils', '$route', '$q', 'ngToast',
14 function($scope, $window, APIUtils, $route, $q, ngToast) {
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) {
beccabroek2264b422019-01-07 11:43:13 -060052 ngToast.danger('Power cap value between 100 and 10,000 is required');
Gunnar Mills006aaa02018-06-26 16:08:59 -050053 return;
54 }
55 $scope.loading = true;
56 var promises = [
57 setPowerCapValue(),
58 setPowerCapEnable(),
59 ];
60
beccabroek2264b422019-01-07 11:43:13 -060061 $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 });
Gunnar Mills006aaa02018-06-26 16:08:59 -050072 };
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) {
Gunnar Mills006aaa02018-06-26 16:08:59 -050082 console.log(JSON.stringify(error));
beccabroek2264b422019-01-07 11:43:13 -060083 return $q.reject();
Gunnar Mills006aaa02018-06-26 16:08:59 -050084 });
85 }
86
87 function setPowerCapEnable() {
88 return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable)
89 .then(
90 function(data) {},
91 function(error) {
Gunnar Mills006aaa02018-06-26 16:08:59 -050092 console.log(JSON.stringify(error));
beccabroek2264b422019-01-07 11:43:13 -060093 return $q.reject();
Gunnar Mills006aaa02018-06-26 16:08:59 -050094 });
95 }
Gunnar Mills52b8bde2018-06-21 13:16:54 -050096 }
97 ]);
Gunnar Mills52b8bde2018-06-21 13:16:54 -050098})(angular);