-
Notifications
You must be signed in to change notification settings - Fork 10
/
stats.rb
145 lines (111 loc) · 2.52 KB
/
stats.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
class Stats
class Worker
def initialize(wstats)
@wstats = wstats
end
def pid
@wstats['pid']
end
def killed=(killed)
@wstats['killed'] = killed
end
def killed?
!!@wstats['killed']
end
def mem=(mem)
@wstats['mem'] = mem
end
def mem
@wstats['mem']
end
def pcpu=(pcpu)
@wstats['pcpu'] = pcpu
end
def pcpu
@wstats['pcpu']
end
def booting?
@wstats.key?('last_status') && @wstats['last_status'].empty?
end
def running
@wstats.dig('last_status', 'running') || @wstats['running'] || 0
end
alias :total_threads :running
def max_threads
@wstats.dig('last_status', 'max_threads') || @wstats['max_threads'] || 0
end
def pool_capacity
@wstats.dig('last_status', 'pool_capacity') || @wstats['pool_capacity'] || 0
end
def running_threads
max_threads - pool_capacity
end
def phase
@wstats['phase']
end
def load
running_threads/total_threads.to_f*100
end
def uptime
return 0 unless @wstats.key?('started_at')
(Time.now - Time.parse(@wstats['started_at'])).to_i
end
def requests_count
@wstats.dig('last_status', 'requests_count') || @wstats['requests_count'] || 0
end
def backlog
@wstats.dig('last_status', 'backlog') || 0
end
def last_checkin
(Time.now - Time.parse(@wstats['last_checkin'])).round
rescue
0
end
end
def initialize(stats)
@stats = stats
end
def workers
@workers ||= (@stats['worker_status'] || [@stats]).map { |wstats| Worker.new(wstats) }
end
def pid=(pid)
@stats['pid'] = pid
end
def pid
@stats['pid']
end
def state_file_path=(state_file_path)
@stats['state_file_path'] = state_file_path
end
def state_file_path
@stats['state_file_path']
end
def uptime
return 0 unless @stats.key?('started_at')
(Time.now - Time.parse(@stats['started_at'])).to_i
end
def booting?
workers.all?(&:booting?)
end
def total_threads
workers.reduce(0) { |total, wstats| total + wstats.max_threads }
end
def running_threads
workers.reduce(0) { |total, wstats| total + wstats.running_threads }
end
def max_threads
workers.reduce(0) { |total, wstats| total + wstats.max_threads }
end
def running
@stats['running'] || 0
end
def pool_capacity
@stats['pool_capacity'] || 0
end
def phase
@stats['phase']
end
def load
running_threads/total_threads.to_f*100
end
end