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
|
# File 'lib/kakin/cli.rb', line 21
def calc
Kakin::Configuration.setup
yaml = YAML.load_file(options[:f])
start_time = Time.parse(options[:s]).strftime("%FT%T")
end_time = Time.parse(options[:e]).strftime("%FT%T")
skip = options[:S]
tenants = if tenants = options[:t]
tenants.map do |tenant|
Yao.tenant_klass.get(tenant)
end
else
Yao.tenant_klass.list
end.select do |tenant|
name = tenant.name || tenant.id
!skip.include?(name)
end
STDERR.puts "Start: #{start_time}"
STDERR.puts "End: #{end_time}"
result = tenants.each_with_object({}) do |tenant, result|
usage = tenant.server_usage(start: start_time, end: end_time)
next if usage.empty?
total_vcpus_usage = usage["total_vcpus_usage"]
total_memory_mb_usage = usage["total_memory_mb_usage"]
total_local_gb_usage = usage["total_local_gb_usage"]
bill_vcpu = total_vcpus_usage * yaml["vcpu_per_hour"]
bill_memory = total_memory_mb_usage * yaml["memory_mb_per_hour"]
bill_disk = total_local_gb_usage * yaml["disk_gb_per_hour"]
name = tenant.name || tenant.id
result[name] = {
'bill_total' => bill_vcpu + bill_memory + bill_disk,
'bill_vcpu' => bill_vcpu,
'bill_memory' => bill_memory,
'bill_disk' => bill_disk,
'total_hours' => usage["total_hours"],
'total_vcpus_usage' => total_vcpus_usage,
'total_memory_mb_usage' => total_memory_mb_usage,
'total_local_gb_usage' => total_local_gb_usage,
}
end
puts YAML.dump(result)
end
|