forked from benc-uk/csa-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquotas-csa45.html
302 lines (271 loc) · 8.32 KB
/
quotas-csa45.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!--
CSA Widget - Resource usage quota display
Ben Coleman, March 2014
v2.0
v2.1 - Alternative version, less restrictive checking of properties, no longer only works with measurable properties
v2.3 - Changed for CSA 4.5 WARNING! This version is not backward compatible
-->
<!-- -------------------------------------------------------------------------------------- -->
<!-- HTML and widget display -->
<!-- -------------------------------------------------------------------------------------- -->
<div id="quota_widget">
<h4>Resource Quotas</h4>
<hr/>
<table width="100%" cellspacing="0">
<tr>
<td class="pietd">
<div id="server_chart" class="piechart"></div>
Servers
<div id="server_chart_label" class="pielabel">0</div>
</td>
<td class="pietd">
<div id="cpu_chart" class="piechart"></div>
CPUs
<div id="cpu_chart_label" class="pielabel">0</div>
</td>
</tr>
<tr>
<td class="pietd">
<div id="mem_chart" class="piechart"></div>
Memory (GB)
<div id="mem_chart_label" class="pielabel">0</div>
</td>
<td class="pietd">
<div id="disk_chart" class="piechart"></div>
Disk (GB)
<div id="disk_chart_label" class="pielabel">0</div>
</td>
</tr>
</table>
</div>
<!-- -------------------------------------------------------------------------------------- -->
<!-- Javascript and main code -->
<!-- -------------------------------------------------------------------------------------- -->
<script language="javascript" type="text/javascript" src="scripts/jquery.flot.js"></script>
<script language="javascript" type="text/javascript" src="scripts/jquery.flot.pie.js"></script>
<script type="text/javascript">
var server_quota = 0;
var cpu_quota = 0;
var mem_quota = 0;
var disk_quota = 0;
loadQuotas();
var server_total = 0;
var cpu_total = 0;
var mem_total = 0;
var disk_total = 0;
// Our journey starts here!
refreshAllPies();
getAllServicesQuota();
//
// Callback function used to asynchronously handle the result of getAllServices
//
function processServicesQuota(data, textStatus, apiXHR) {
// Loop over each svc instance
svc_index = 0;
for(svc_index = 0; svc_index < data.length; svc_index++) {
svc = data[svc_index]; // Get instance
// Invoke getTopology for each service
getTopologyQuota(svc.id)
}
}
//
// Callback function used to asynchronously handle the result of getTopology
//
function processTopologyQuota(data, textStatus, apiXHR) {
// Loop over the nodes sub-array inside topology data
for(node_index = 0; node_index < data.nodes.length; node_index++) {
node = data.nodes[node_index]; // Get node
// Skip pattern nodes
if(node.pattern) { continue }
// If server - increase server count & grab server node properties for more detail
if(node.type.key == "SERVER") {
server_total++;
drawPie(server_total, server_quota, "server_chart");
getNodePropertiesQuota(data.serviceInstance.id, node.id);
}
// If storage - grab storage node properties for more detail
if(node.type.key == "STORAGE_VOLUME") {
getNodePropertiesQuota(data.serviceInstance.id, node.id)
}
}
}
//
// Callback function used to asynchronously handle the result of getNodeProperties
//
function processNodeQuota(data, textStatus, apiXHR) {
// Loop over properties array inside node data
for(prop_index = 0; prop_index < data.properties.length; prop_index++) {
prop = data.properties[prop_index];
// ALTERNATE VERSION Tweaked to pick up any properties not just those with measurable units
//if (prop.unit != null && prop.unit !== undefined) {
// Handle CPU properties
if(prop.key.toLowerCase().indexOf("cpu") != -1) {
cpu_total += parseInt(prop.values[0]);
drawPie(cpu_total, cpu_quota, "cpu_chart");
}
// Try to guess memory properties
if(prop.key.toLowerCase().indexOf("mem") != -1) {
mem = prop.values[0];
mem = mem / 1024;
mem_total += mem;
drawPie(mem_total, mem_quota, "mem_chart");
}
// Try to guess disk properties
if(prop.key.toLowerCase().indexOf("disk") != -1 || prop.key.toLowerCase().indexOf("vol")) {
disk = prop.values[0];
disk_total += parseInt(disk);
drawPie(disk_total, disk_quota, "disk_chart");
}
//}
}
}
//
// Make AJAX call to CSA API to get all ACTIVE services
//
function getAllServicesQuota() {
$.ajax({
type: "GET",
url: "/api/myservice?count=256&status=ACTIVE&offset=0&sort=newest",
success: processServicesQuota,
error: errorLog
});
}
//
// Make AJAX call to CSA API to get the topology for a single service
//
function getTopologyQuota(svc_id) {
$.ajax({
type: "GET",
url: "/api/topology/" + svc_id,
success: processTopologyQuota,
error: errorLog
});
}
//
// Make AJAX call to CSA API to get the properties of a node inside a service topology
//
function getNodePropertiesQuota(svc_id, node_id) {
$.ajax({
type: "GET",
url: "/api/topology/" + svc_id + "/properties/" + node_id,
success: processNodeQuota,
error: errorLog
});
}
//
// Make AJAX call to CSA API to get user info and load quota files
//
function loadQuotas() {
$.ajax({
type: "GET",
url: "/api/user",
success: function(data, textStatus, apiXHR) {
// get Username and Org name
uname = data.name;
org_name = data.tenant.name;
// Load default system wide quota file
var quotas = null;
quotas = readTextFile("quotas/defaults.txt");
// Over ride with org specific quota file if found
temp = readTextFile("quotas/"+org_name+".txt");
if(temp.indexOf("QUOTA FILE") > 0) quotas = temp;
// Over ride with user specific quota file if found
temp = readTextFile("quotas/"+uname+".txt");
if(temp.indexOf("QUOTA FILE") > 0) quotas = temp;
// Eval quotas
eval(quotas);
},
error: errorLog,
async: false
});
}
//
// Simple error logging
//
function errorLog(apiXHR, textStatus, errorThrown) {
console.log("CSA API Error: " + errorThrown);
}
//
// Update a pie chart
//
function drawPie(total, quota, chart) {
// CSS and DOM stuff to cope with resizing
var wid = $(".pietd").width();
var hi = $(".pietd").height();
var new_wid = wid-40;
label_offset = -(hi/2)-25;
$(".piechart").each(function(index, value) {
$(value).css("width", new_wid);
$(value).css("height", new_wid);
});
$(".pielabel").each(function(index, value) {
$(value).css("top", label_offset);
});
var col = "#26752C";
var label_total = total;
// Maths and colouring stuff
if(total / quota > 0.75) {
col = "#f08100";
}
if(total >= quota) {
total = quota;
col = "#cb1b1f";
}
var data = [
{data: total, color: col},
{data: quota - total, color: "#eeeeee"},
];
// Update inner text label
$('#' + chart + '_label').text(label_total);
// Update pie plot
$.plot('#' + chart, data, {
series: {
pie: {
radius: 0.98,
innerRadius: 0.5,
show: true,
stroke: {color: '#7A7896', width: 3},
}
}
});
// Add tooltip to the chart
$('#'+chart).prop('title', label_total + " / " + quota);
}
//
// Detect and handle resizing
//
var id;
$(window).resize(function() {
// use timer to prevent resize bouncing
clearTimeout(id);
id = setTimeout(refreshAllPies, 100);
});
//
// Update all pie charts
//
function refreshAllPies() {
drawPie(server_total, server_quota, "server_chart");
drawPie(cpu_total, cpu_quota, "cpu_chart");
drawPie(mem_total, mem_quota, "mem_chart");
drawPie(disk_total, disk_quota, "disk_chart");
}
//
// Helper function to read any text file
//
function readTextFile(file) {
var rawFileXHR = new XMLHttpRequest();
rawFileXHR.open("GET", file, false);
rawFileXHR.send(null);
//console.log(file +"STATUS="+rawFileXHR);
if(rawFileXHR.status === 200 || rawFileXHR.status == 0) {
return rawFileXHR.responseText;
}
}
</script>
<style type="text/css">
#quota_widget {background-color:#7A7896; padding:5px; color:white;}
#quota_widget h4, #quota_widget hr { margin:0 0 5px 0; padding:0px;}
.pietd {text-align:center; color: white; margin:0px; padding:0px; vertical-align:middle;}
.piechart {width:75px; height:75px; margin: 0 auto; background-color:#7A7896;}
.pielabel {position: relative; top:-70px; z-index: 1; margin: 0 auto; margin-bottom:-25px; padding:0px; float:both;}
</style>