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
|
# File 'lib/active_resource/remote_connection.rb', line 52
def start_status(service)
begin
items = Hash.new
service.instanceIds.each do |id|
container = Container.find(id)
items[container.externalId] = {
id: container.id,
name: container.name,
state: container.state,
cpu: 0
}
end
EM.run do
ws = Faye::WebSocket::Client.new("#{uri}&&sockId=5", nil, {ping: 10 })
pastel = Pastel.new
i = 0
ws.on :message do |event|
content = JSON.parse(event.data).first
table = TTY::Table.new header: ['name','state','memory', 'cpu']
memory = content['memory']['usage'].to_f
memory_limit = content['memLimit'].to_f
memory_rate = (memory/memory_limit)
formated_memory = ActiveSupport::NumberHelper::number_to_human_size(memory)
if items[content['id']][:cpu_total]
delta_total_usage = (content['cpu']['usage']['total'] - items[content['id']][:cpu_total]).to_f / content['cpu']['usage']['total']
delta_system_usage = (content['cpu']['usage']['system'] - items[content['id']][:cpu_system_total]).to_f / content['cpu']['usage']['system']
if delta_system_usage>0
y = (delta_total_usage / delta_system_usage)*content['cpu']['usage']['per_cpu_usage'].size * 100.0
else
y = 0
end
end
items[content['id']][:cpu_total] = content['cpu']['usage']['total']
items[content['id']][:cpu_system_total] = content['cpu']['usage']['system']
items[content['id']][:cpu] = y
items[content['id']][:memory] = memory
items[content['id']][:memory_limit] = memory_limit
items[content['id']][:memory_rate] = memory_rate
items[content['id']][:formated_memory] = formated_memory
items.each_with_index.each do |item, index|
table << [item[1][:name], item[1][:state], item[1][:formated_memory], "#{items[content['id']][:cpu].to_f.round(1)}%"]
end
lines = "#{table.render(:ascii, border:{separator: :each_row})}".lines.count
print "\r" + ("\e[A\e[J"*(lines-1)) if i>table.rows_size
STDOUT.syswrite "#{table.render(:ascii, border:{separator: :each_row})}" if i>table.rows_size-1
i+=1
end
ws.on :close do |event|
EM.stop
ws = nil
end
end
ensure
IO.console.cooked!
end
end
|