/
process_stats_presenter.rb
65 lines (59 loc) · 1.79 KB
/
process_stats_presenter.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
module VCAP::CloudController
class ProcessStatsPresenter
def present_stats_hash(type, process_stats)
process_stats.each.map do |index, instance_stats|
instance_stats_hash(type, index, instance_stats)
end.sort_by { |s| s[:index] }
end
private
def instance_stats_hash(type, index, stats)
case stats[:state]
when 'DOWN'
down_instance_stats_hash(type, index, stats)
else
found_instance_stats_hash(type, index, stats)
end
end
def found_instance_stats_hash(type, index, stats)
{
type: type,
index: index,
state: stats[:state],
usage: {
time: stats[:stats][:usage][:time],
cpu: stats[:stats][:usage][:cpu],
mem: stats[:stats][:usage][:mem],
disk: stats[:stats][:usage][:disk],
},
host: stats[:stats][:host],
uptime: stats[:stats][:uptime],
mem_quota: stats[:stats][:mem_quota],
disk_quota: stats[:stats][:disk_quota],
fds_quota: stats[:stats][:fds_quota]
}.tap { |presented_stats| add_port_info(presented_stats, stats) }
end
def down_instance_stats_hash(type, index, stats)
{
type: type,
index: index,
state: stats[:state],
uptime: stats[:uptime]
}
end
def add_port_info(presented_stats, stats)
if stats[:stats][:net_info]
presented_stats[:instance_ports] = net_info_to_instance_ports(stats[:stats][:net_info])
else
presented_stats[:port] = stats[:stats][:port]
end
end
def net_info_to_instance_ports(net_info)
net_info[:ports].map do |ports|
{
external: ports[:host_port],
internal: ports[:container_port],
}
end
end
end
end