-
Notifications
You must be signed in to change notification settings - Fork 359
/
process_stats_presenter.rb
91 lines (81 loc) · 2.87 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
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
module VCAP::CloudController
module Presenters
module V3
class ProcessStatsPresenter
def initialize(type, process_stats)
@type = type
@process_stats = process_stats
end
def to_hash
{
resources: present_stats_hash
}
end
def present_stats_hash
@process_stats.map do |index, instance_stats|
instance_stats_hash(index, instance_stats)
end.sort_by { |s| s[:index] }
end
private
def instance_stats_hash(index, stats)
case stats[:state]
when 'DOWN'
down_instance_stats_hash(index, stats)
else
found_instance_stats_hash(index, stats)
end
end
def found_instance_stats_hash(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],
isolation_segment: stats[:isolation_segment],
details: stats[:details]
}.tap { |presented_stats| add_port_info(presented_stats, stats) }
end
def down_instance_stats_hash(index, stats)
{
type: @type,
index: index,
state: stats[:state],
uptime: stats[:uptime],
isolation_segment: stats[:isolation_segment],
details: stats[:details]
}
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][:ports])
else
presented_stats[:port] = stats[:stats][:port]
end
end
def net_info_to_instance_ports(net_info_ports)
return [] if net_info_ports.nil?
net_info_ports.map do |ports|
external_tls_proxy_port_raw = HashUtils.dig(ports, :host_tls_proxy_port)
internal_tls_proxy_port_raw = HashUtils.dig(ports, :container_tls_proxy_port)
{
external: ports[:host_port],
internal: ports[:container_port],
external_tls_proxy_port: external_tls_proxy_port_raw.to_i == 0 ? nil : external_tls_proxy_port_raw,
internal_tls_proxy_port: internal_tls_proxy_port_raw.to_i == 0 ? nil : internal_tls_proxy_port_raw
}
end
end
end
end
end
end