-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateCostsPages.rb
executable file
·483 lines (440 loc) · 16.2 KB
/
generateCostsPages.rb
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env ruby
#coding: utf-8
require "json"
require "pp"
filename = "./data/ss.info.json"
ssInfoJson = File.read(filename)
ssInfo = JSON.parse(ssInfoJson)
# costs are in pence
# (G-cloud 7 costing Computer as a Service, 2015)
# "assured" / hour, not "elevated" / hour
# standard service level
# unoptimised storage
$costmodel = [
{ "name" => "micro", "cost" => 4, "cpus" => 1, "ram" => 512, "disksize" => 10 },
{ "name" => "tiny", "cost" => 13, "cpus" => 1, "ram" => 2048, "disksize" => 60 },
{ "name" => "small", "cost" => 18, "cpus" => 2, "ram" => 4096, "disksize" => 60 },
{ "name" => "medium", "cost" => 30, "cpus" => 4, "ram" => 8192, "disksize" => 60 },
{ "name" => "medium_high_mem", "cost" => 43, "cpus" => 4, "ram" => 16384, "disksize" => 60 },
{ "name" => "large", "cost" => 63, "cpus" => 8, "ram" => 16384, "disksize" => 60 },
{ "name" => "large_high_mem", "cost" => 100, "cpus" => 8, "ram" => 32768, "disksize" => 60 },
{ "name" => "t1apps_small", "cost" => 140, "cpus" => 8, "ram" => 49152, "disksize" => 60 },
{ "name" => "t1apps_medium", "cost" => 180, "cpus" => 8, "ram" => 65536, "disksize" => 60 },
{ "name" => "t1apps_large", "cost" => 255, "cpus" => 8, "ram" => 98304, "disksize" => 60 }
]
$diskCostGbPerMon = 20 # n.b. per month
class CostBucket
def initialize(name)
@name = name
@children = []
@cost_ph = 0
@cost_pd = 0
@cost_pw = 0
@cost_pm = 0
@cost_py = 0
@disk_cost_ph = 0
@disk_cost_pd = 0
@disk_cost_pw = 0
@disk_cost_pm = 0
@disk_cost_py = 0
@vm_cost_ph = 0
@vm_cost_pd = 0
@vm_cost_pw = 0
@vm_cost_pm = 0
@vm_cost_py = 0
@cpus = 0
@mem = 0
@diskSize = 0
end
def setName(name)
@name = name
end
def getName
@name
end
def getCleanedName
@name.tr("()","").tr(". ","_")
end
def getChildren
@children
end
def setCostPerHour(cost)
@cost_ph = cost.to_i
end
def getCostPerHour
@cost_ph
end
def setCostPerDay(cost)
@cost_pd = cost.to_i
end
def getCostPerDay
@cost_pd
end
def setCostPerWeek(cost)
@cost_pw = cost.to_i
end
def getCostPerWeek
@cost_pw
end
def setCostPerMonth(cost)
@cost_pm = cost.to_i
end
def getCostPerMonth
@cost_pm
end
def setCostPerYear(cost)
@cost_py = cost.to_i
end
def getCostPerYear
@cost_py
end
def setDiskCostPerHour(cost)
@disk_cost_ph = cost.to_i
end
def getDiskCostPerHour
@disk_cost_ph
end
def setDiskCostPerDay(cost)
@disk_cost_pd = cost.to_i
end
def getDiskCostPerDay
@disk_cost_pd
end
def setDiskCostPerWeek(cost)
@disk_cost_pw = cost.to_i
end
def getDiskCostPerWeek
@disk_cost_pw
end
def setDiskCostPerMonth(cost)
if(cost<0) then
@disk_cost_pm = 0
else
@disk_cost_pm = cost.to_i
end
end
def getDiskCostPerMonth
@disk_cost_pm
end
def setDiskCostPerYear(cost)
@disk_cost_py = cost.to_i
end
def getDiskCostPerYear
@disk_cost_py
end
def setVMCostPerHour(cost)
@vm_cost_ph = cost.to_i
end
def getVMCostPerHour
@vm_cost_ph
end
def setVMCostPerDay(cost)
@vm_cost_pd = cost.to_i
end
def getVMCostPerDay
@vm_cost_pd
end
def setVMCostPerWeek(cost)
@vm_cost_pw = cost.to_i
end
def getVMCostPerWeek
@vm_cost_pw
end
def setVMCostPerMonth(cost)
@vm_cost_pm = cost.to_i
end
def getVMCostPerMonth
@vm_cost_pm
end
def setVMCostPerYear(cost)
@vm_cost_py = cost.to_i
end
def getVMCostPerYear
@vm_cost_py
end
def addChild(item)
@children.push(item)
end
def getCpus
@cpus
end
def setCpus(num)
@cpus = num
end
def getMem
@mem
end
def setMem(num)
@mem = num
end
def getDiskSize
@diskSize
end
def setDiskSize(num)
@diskSize = num
end
def calculateCost
@children.each do |child|
child.calculateCost
self.setDiskSize(self.getDiskSize + child.getDiskSize)
self.setCpus(self.getCpus + child.getCpus)
self.setMem(self.getMem + child.getMem)
self.setDiskCostPerHour(self.getDiskCostPerHour + child.getDiskCostPerHour)
self.setDiskCostPerDay(self.getDiskCostPerDay + child.getDiskCostPerDay)
self.setDiskCostPerWeek(self.getDiskCostPerWeek + child.getDiskCostPerWeek)
self.setDiskCostPerMonth(self.getDiskCostPerMonth + child.getDiskCostPerMonth)
self.setDiskCostPerYear(self.getDiskCostPerYear + child.getDiskCostPerYear)
self.setVMCostPerHour(self.getVMCostPerHour + child.getVMCostPerHour)
self.setVMCostPerDay(self.getVMCostPerDay + child.getVMCostPerDay)
self.setVMCostPerWeek(self.getVMCostPerWeek + child.getVMCostPerWeek)
self.setVMCostPerMonth(self.getVMCostPerMonth + child.getVMCostPerMonth)
self.setVMCostPerYear(self.getVMCostPerYear + child.getVMCostPerYear)
end
self.setCostPerHour(self.getDiskCostPerHour + self.getVMCostPerHour)
self.setCostPerDay(self.getDiskCostPerDay + self.getVMCostPerDay)
self.setCostPerWeek(self.getDiskCostPerWeek + self.getVMCostPerWeek)
self.setCostPerMonth(self.getDiskCostPerMonth + self.getVMCostPerMonth)
self.setCostPerYear(self.getDiskCostPerYear + self.getVMCostPerYear)
end
def toHash
hash_self = {
"name" => self.getName,
"vm_cost_ph" => self.getVMCostPerHour,
"vm_cost_pd" => self.getVMCostPerDay,
"vm_cost_pw" => self.getVMCostPerWeek,
"vm_cost_pm" => self.getVMCostPerMonth,
"vm_cost_py" => self.getVMCostPerYear,
"disk_cost_ph" => self.getDiskCostPerHour,
"disk_cost_pd" => self.getDiskCostPerDay,
"disk_cost_pw" => self.getDiskCostPerWeek,
"disk_cost_pm" => self.getDiskCostPerMonth,
"disk_cost_py" => self.getDiskCostPerYear,
"cost_ph" => self.getCostPerHour,
"cost_pd" => self.getCostPerDay,
"cost_pw" => self.getCostPerWeek,
"cost_pm" => self.getCostPerMonth,
"cost_py" => self.getCostPerYear,
"children" => []
}
@children.each do |child|
hash_self["children"].push(child.toHash)
end
return hash_self
end
end
class VM < CostBucket
def initialize(name)
@diskCount = 0
@status = "unknown"
@vmType = "unknown"
super
end
def getStatus
@status
end
def setStatus(stat)
@status = stat
end
def getVMType
@vmType
end
def setVMType(type)
@vmType=type
end
def getDiskCount
@diskCount
end
def setDiskCount(num)
@diskCount = num
end
def calculateCost
if(self.getStatus == "on") then
thisCostModel = ""
$costmodel.each do |vmtype|
thisCostModel = vmtype
if(self.getCpus <= vmtype["cpus"].to_i && self.getMem <= vmtype["ram"].to_i ) then
self.setVMType(vmtype["name"])
break
end
self.setVMCostPerHour(thisCostModel["cost"])
end
if(self.getDiskCount.to_i > 1) then
self.setDiskCostPerMonth(((self.getDiskSize - thisCostModel["disksize"])*$diskCostGbPerMon)/1024)
else
self.setDiskCostPerMonth(0)
end
end
self.setDiskCostPerYear(self.getDiskCostPerMonth * 12 )
self.setDiskCostPerDay(self.getDiskCostPerYear / 365)
self.setDiskCostPerHour(self.getDiskCostPerDay / 24)
self.setDiskCostPerWeek(self.getDiskCostPerDay * 7)
self.setVMCostPerDay(self.getVMCostPerHour * 24)
self.setVMCostPerWeek(self.getVMCostPerDay * 7)
self.setVMCostPerYear(self.getVMCostPerDay * 365)
self.setVMCostPerMonth(self.getVMCostPerYear / 12)
self.setCostPerHour(self.getDiskCostPerHour + self.getVMCostPerHour)
self.setCostPerDay(self.getDiskCostPerDay + self.getVMCostPerDay)
self.setCostPerWeek(self.getDiskCostPerWeek + self.getVMCostPerWeek)
self.setCostPerMonth(self.getDiskCostPerMonth + self.getVMCostPerMonth)
self.setCostPerYear(self.getDiskCostPerYear + self.getVMCostPerYear)
end
def toHash
hash_self = {
"name" => self.getName,
"vm_cost_ph" => self.getVMCostPerHour,
"vm_cost_pd" => self.getVMCostPerDay,
"vm_cost_pw" => self.getVMCostPerWeek,
"vm_cost_pm" => self.getVMCostPerMonth,
"vm_cost_py" => self.getVMCostPerYear,
"disk_cost_ph" => self.getDiskCostPerHour,
"disk_cost_pd" => self.getDiskCostPerDay,
"disk_cost_pw" => self.getDiskCostPerWeek,
"disk_cost_pm" => self.getDiskCostPerMonth,
"disk_cost_py" => self.getDiskCostPerYear,
"cost_ph" => self.getCostPerHour,
"cost_pd" => self.getCostPerDay,
"cost_pw" => self.getCostPerWeek,
"cost_pm" => self.getCostPerMonth,
"cost_py" => self.getCostPerYear,
"vmtype" => self.getVMType,
"status" => self.getStatus
}
return hash_self
end
end
bigBucket = CostBucket.new("environments")
ssInfo.each do |acc|
acc.each do |accName,vdcs|
thisAccount = CostBucket.new(accName)
vdcs.each do |vdcName,vdc|
thisVdc = CostBucket.new(vdcName)
vdc["vapps"].each do |vappName,vapp|
thisVapp = CostBucket.new(vappName)
vapp["vms"].each do |vmName,vm|
thisVm = VM.new(vmName)
if(vm["textStatus"] == "POWERED_ON") then
thisVm.setStatus("on")
else
thisVm.setStatus("off")
end
thisVm.setCpus(vm["ncpus"].to_i)
thisVm.setMem(vm["memMb"].to_i)
vm["disks"].each do |disk|
thisVm.setDiskCount(thisVm.getDiskCount + 1)
thisVm.setDiskSize(thisVm.getDiskSize + disk["size"])
end
thisVapp.addChild(thisVm)
end
thisVdc.addChild(thisVapp)
end
thisAccount.addChild(thisVdc)
end
bigBucket.addChild(thisAccount)
end
end
bigBucket.calculateCost()
htmlStr = "<!DOCTYPE html>" +
"<meta charset=\"ISO-8859-1\">" +
"<!-- charset=\"utf-8\"> -->" +
"<head><link rel=\"stylesheet\" type=\"text/css\" href=\"style/cost.css\"/></head>"
htmlStr = htmlStr + "<body>" +
"<table id=\"environmentCosts\">
<tr class=\"title\"><th>Account</th><th>VDC name</th><th>Vapp name</th><th>VM name</th><th>State</th><th>CPU</th><th>Mem</th><th>Disks#</th><th>Disk size</th><th>Disk Cost/month</th><th>VM/month</th><th>Cost/month</th></tr>"
bigBucket.getChildren.each { |account|
htmlStr = htmlStr + "<tr class=\"account childrenClosed \" id=\"account-" + account.getCleanedName + "\">" +
"<td colspan=\"5\" id=\"" + account.getCleanedName + "\">" + account.getName + "</td>" +
"<td/>" +
"<td/>" +
"<td/>" +
"<td/>" +
"<td>£" + (account.getDiskCostPerMonth/100).to_s + "</td>" +
"<td>£" + (account.getVMCostPerMonth/100).to_s + "</td>" +
"<td>£" + (account.getCostPerMonth/100).to_s + "</td>" +
"</tr>\n"
account.getChildren.each { |vdc|
htmlStr = htmlStr + "<tr class=\"vdc notshown childrenClosed account-" + account.getCleanedName + "\" id=\"vdc-" + vdc.getCleanedName + "\">" +
"<td/>" +
"<td colspan=\"3\" id=\"" + vdc.getCleanedName + "\">" + vdc.getName + "</td>\n" +
"<td/>" +
"<td/>" +
"<td/>" +
"<td />" +
"<td />" +
"<td>£" + (vdc.getDiskCostPerMonth/100).to_s + "</td>" +
"<td>£" + (vdc.getVMCostPerMonth/100).to_s + "</td>" +
"<td>£" + (vdc.getCostPerMonth/100).to_s + "</td>" +
"</tr>\n"
vdc.getChildren.each{ |vapp|
htmlStr = htmlStr + "<tr class=\"vapp notshown childrenClosed account-" + account.getCleanedName + " vdc-" + vdc.getCleanedName + "\" id=\"vapp-" + vapp.getCleanedName + "\">" +
"<td/>" + "<td/>" +
"<td colspan=\"3\" id=\"" + vapp.getCleanedName + "\">" + vapp.getName + "</td>\n" +
"<td/>" + "<td/>" + "<td/>" + "<td/>" +
"<td/>£" + (vapp.getDiskCostPerMonth/100).to_s + "</td>" +
"<td/>£" + (vapp.getVMCostPerMonth/100).to_s + "</td>" +
"<td/>£" + (vapp.getCostPerMonth/100).to_s + "</td>" +
"</tr>\n"
lineCount=1
vapp.getChildren.each { |vm|
if(lineCount % 2 == 1) then
rowClass="odd"
else
rowClass="even"
end
vmClass="unknown"
if(vm.getStatus == "off") then
vmClass="vmoff"
else
vmClass="vmon"
end
htmlStr = htmlStr + "<tr class=\"vm notshown vapp-" + vapp.getCleanedName + " vdc-" + vdc.getCleanedName + " account-" + account.getCleanedName + " " + rowClass + " " + vmClass + "\">" +
"<td/>\n" + "<td/>\n" + "<td/>\n" +
"<td/>" + vm.getName + "</td>\n" +
"<td/>" + vm.getStatus + "</td>\n" +
"<td/>" + vm.getCpus.to_s + "</td>\n" +
"<td/>" + vm.getMem.to_s + "</td>\n" +
"<td/>" + vm.getDiskCount.to_s + "</td>\n" +
"<td/>" + vm.getDiskSize.to_s + "</td>\n" +
"<td/>£" + (vm.getDiskCostPerMonth/100).to_s + "</th>\n" +
"<td/>£" + (vm.getVMCostPerMonth/100).to_s + "</th>\n" +
"<td/>£" + (vm.getCostPerMonth/100).to_s + "</th>\n" +
"</tr>"
lineCount = lineCount + 1
}
}
}
}
htmlStr = htmlStr + "<tr class=\"total\">" +
"<td colspan=9>TOTAL</td>" +
"<td>£" + (bigBucket.getDiskCostPerMonth/100).to_s + "</td>" +
"<td>£" + (bigBucket.getVMCostPerMonth/100).to_s + "</td>" +
"<td>£" + (bigBucket.getCostPerMonth/100).to_s + "</td>" +
"</tr>\n"
htmlStr=htmlStr + "</table>"
htmlStr=htmlStr + "
<script src=\"./jslib/d3.v3.min.js\"> </script>
<script src=\"./jslib/jquery-latest.min.js\"></script>
<script src=\"./jslib/costcircles.js\"></script>
<script>
$(function() {
$('tr').click(function() {
if($(this).hasClass(\"childrenOpen\")) {
$(this).removeClass(\"childrenOpen\").addClass(\"childrenClosed\");
unshownChildRows=$(\"tr.notshown.\" + $(this).attr('id'));
shownChildRows=$(\"tr.shown.\" + $(this).attr('id'));
shownChildRows.removeClass(\"shown\").addClass(\"notshown\").addClass(\"childrenOpen\");
unshownChildRows.addClass(\"childrenOpen\");
} else {
$(this).removeClass(\"childrenClosed\").addClass(\"childrenOpen\");
unshownChildRows=$(\"tr.notshown.\" + $(this).attr('id'));
shownChildRows=$(\"tr.shown.\" + $(this).attr('id'));
unshownChildRows.removeClass(\"notshown\").addClass(\"shown\").addClass(\"childrenOpen\");
shownChildRows.addClass(\"childrenOpen\");
}
});
});
</script>
"
htmlStr=htmlStr + "</body></html>\n"
File.open("./web/cost.html", 'w'){ |file| file.write(htmlStr)}
File.open("./web/ss.costcircles.json", 'w'){ |file| file.write(bigBucket.toHash.to_json)}
exit 0