forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquota.js
164 lines (145 loc) · 6.08 KB
/
quota.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
/* jshint unused: false */
/**
* @ngdoc function
* @name openshiftConsole.controller:QuotaController
* @description
* # QuotaController
* Controller of the openshiftConsole
*/
angular.module('openshiftConsole')
.controller('QuotaController', function ($filter,
$routeParams,
$scope,
DataService,
ProjectsService,
Logger) {
$scope.projectName = $routeParams.project;
$scope.limitRanges = {};
$scope.limitsByType = {};
$scope.labelSuggestions = {};
$scope.alerts = $scope.alerts || {};
$scope.quotaHelp = "Limits resource usage within this project.";
$scope.emptyMessageLimitRanges = "Loading...";
$scope.limitRangeHelp = "Defines minimum and maximum constraints for runtime resources such as memory and CPU.";
$scope.renderOptions = $scope.renderOptions || {};
$scope.renderOptions.hideFilterWidget = true;
var watches = [];
var usageValue = $filter('usageValue');
$scope.isAtLimit = function(quota, resourceType) {
var q = quota.status.total || quota.status;
var hard = usageValue(_.get(q, ['hard', resourceType]));
if (!hard) {
return false;
}
var used = usageValue(_.get(q, ['used', resourceType]));
if (!used) {
return false;
}
return used >= hard;
};
// Order the table rows first in the order of the donuts above the table,
// then alphabetically by humanized label.
var humanizeQuotaResource = $filter('humanizeQuotaResource');
var compareResourceType = function(left, right) {
// CPU Request
if (left === 'cpu' || left === 'requests.cpu') {
return right === 'cpu' || right === 'requests.cpu' ? 0 : -1;
}
if (right === 'cpu' || right === 'requests.cpu') {
return 1;
}
// Memory Request
if (left === 'memory' || left === 'requests.memory') {
return right === 'memory' || right === 'requests.memory' ? 0 : -1;
}
if (right === 'memory' || right === 'requests.memory') {
return 1;
}
// CPU Limit
if (left === 'limits.cpu') {
return right === 'limits.cpu' ? 0 : -1;
}
if (right === 'limits.cpu') {
return 1;
}
// Memory Limit
if (left === 'limits.memory') {
return right === 'limits.memory' ? 0 : -1;
}
if (right === 'limits.memory') {
return 1;
}
left = humanizeQuotaResource(left);
right = humanizeQuotaResource(right);
return left.localeCompare(right);
};
var orderTypes = function(quotas) {
var orderedTypesByQuota = {};
_.each(quotas, function(quota) {
var specHard = _.get(quota, 'spec.quota.hard') || _.get(quota, 'spec.hard');
var orderedTypes = _.keys(specHard).sort(compareResourceType);
orderedTypesByQuota[quota.metadata.name] = orderedTypes;
});
return orderedTypesByQuota;
};
ProjectsService
.get($routeParams.project)
.then(_.spread(function(project, context) {
$scope.project = project;
DataService.list("resourcequotas", context, function(quotas) {
$scope.quotas = _.sortBy(quotas.by("metadata.name"), "metadata.name");
$scope.orderedTypesByQuota = orderTypes($scope.quotas);
Logger.log("quotas", $scope.quotas);
});
DataService.list("appliedclusterresourcequotas", context, function(quotas) {
$scope.clusterQuotas = _.sortBy(quotas.by("metadata.name"), "metadata.name");
$scope.orderedTypesByClusterQuota = orderTypes($scope.clusterQuotas);
$scope.namespaceUsageByClusterQuota = {};
_.each($scope.clusterQuotas, function(quota, quotaName) {
if (quota.status) {
var namespaceUsage = _.find(quota.status.namespaces, { namespace: $routeParams.project });
$scope.namespaceUsageByClusterQuota[quotaName] = namespaceUsage.status;
}
});
Logger.log("cluster quotas", $scope.clusterQuotas);
});
DataService.list("limitranges", context, function(limitRanges) {
$scope.limitRanges = _.sortBy(limitRanges.by("metadata.name"), "metadata.name");
$scope.emptyMessageLimitRanges = "There are no limit ranges set on this project.";
// Convert to a sane format for a view to a build a table with rows per resource type
angular.forEach($scope.limitRanges, function(limitRange){
var name = limitRange.metadata.name;
$scope.limitsByType[name] = {};
angular.forEach(limitRange.spec.limits, function(limit) {
// We have nested types, top level type is something like "Container"
var typeLimits = $scope.limitsByType[name][limit.type] = {};
angular.forEach(limit.max, function(value, type) {
typeLimits[type] = typeLimits[type] || {};
typeLimits[type].max = value;
});
angular.forEach(limit.min, function(value, type) {
typeLimits[type] = typeLimits[type] || {};
typeLimits[type].min = value;
});
angular.forEach(limit["default"], function(value, type) {
typeLimits[type] = typeLimits[type] || {};
typeLimits[type]["default"] = value;
});
angular.forEach(limit.defaultRequest, function(value, type) {
typeLimits[type] = typeLimits[type] || {};
typeLimits[type].defaultRequest = value;
});
angular.forEach(limit.maxLimitRequestRatio, function(value, type) {
typeLimits[type] = typeLimits[type] || {};
typeLimits[type].maxLimitRequestRatio = value;
});
});
});
Logger.log("limitRanges", $scope.limitRanges);
});
$scope.$on('$destroy', function(){
DataService.unwatchAll(watches);
});
}));
});