Class: Liteboard

Inherits:
Object
  • Object
show all
Defined in:
lib/litestack/liteboard/liteboard.rb

Constant Summary collapse

@@resolutions =
{"minute" => [300, 12], "hour" => [3600, 24], "day" => [3600 * 24, 7], "week" => [3600 * 24 * 7, 53], "year" => [3600 * 24 * 365, 100]}
@@res_mapping =
{"hour" => "minute", "day" => "hour", "week" => "day", "year" => "week"}
@@templates =
{}
@@app =
proc do |env|
  case path = env["PATH_INFO"]
  when "/"
    Liteboard.new(env).call(:index)
  when "/topics/Litejob"
    Liteboard.new(env).call(:litejob)
  when "/topics/Litecache"
    Liteboard.new(env).call(:litecache)
  when "/topics/Litedb"
    Liteboard.new(env).call(:litedb)
  when "/topics/Litecable"
    Liteboard.new(env).call(:litecable)
  end
 end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Liteboard

Returns a new instance of Liteboard.



30
31
32
33
34
35
36
# File 'lib/litestack/liteboard/liteboard.rb', line 30

def initialize(env)
  @env = env
  @req = Rack::Request.new(@env)
  @params = @req.params 
  @running = true
  @lm = Litemetric.instance
end

Class Method Details

.appObject



397
398
399
# File 'lib/litestack/liteboard/liteboard.rb', line 397

def self.app
  @@app
end

Instance Method Details

#after(body = nil) ⇒ Object



48
49
50
# File 'lib/litestack/liteboard/liteboard.rb', line 48

def after(body = nil)
  [200, {"Cache-Control" => "no-cache"}, [body]]
end

#beforeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/litestack/liteboard/liteboard.rb', line 52

def before
  @res = params(:res) || "day"
  @resolution = @@res_mapping[@res]
  if !@resolution
    @res = "day"
    @resolution = @@res_mapping[@res]
  end
  @step = @@resolutions[@resolution][0]
  @count = @@resolutions[@resolution][1]
  @order = params(:order)
  @order = nil if @order == ""
  @dir = params(:dir)
  @dir = "desc" if @dir.nil? || @dir == ""
  @dir = @dir.downcase
  @idir = if @dir == "asc"
    "desc"
  else
    "asc"
  end
  @search = params(:search)
  @search = nil if @search == ""
  @topics = @lm.topic_summaries(@resolution, @step * @count, @order, @dir, @search)
end

#call(method) ⇒ Object



42
43
44
45
46
# File 'lib/litestack/liteboard/liteboard.rb', line 42

def call(method)
  before
  res = send(method)
  after(res)
end

#compose_query(field) ⇒ Object



359
360
361
362
# File 'lib/litestack/liteboard/liteboard.rb', line 359

def compose_query(field)
  field.downcase!
  "res=#{@res}&order=#{field}&dir=#{(@order == field) ? @idir : @dir}&search=#{@search}"
end

#dir(field) ⇒ Object



368
369
370
371
372
373
374
375
376
377
# File 'lib/litestack/liteboard/liteboard.rb', line 368

def dir(field)
  if sorted?(field)
    if @dir == "asc"
      return "<span class='material-icons'>arrow_drop_up</span>"
    else
      return "<span class='material-icons'>arrow_drop_down</span>"
    end
  end
  "&nbsp;&nbsp;"
end

#encode(text) ⇒ Object



379
380
381
# File 'lib/litestack/liteboard/liteboard.rb', line 379

def encode(text)
  URI.encode_uri_component(text)
end

#event_sort_url(field) ⇒ Object



355
356
357
# File 'lib/litestack/liteboard/liteboard.rb', line 355

def event_sort_url(field)
  "/topics/#{encode(@topic)}/events/#{encode(@event)}?#{compose_query(field)}"
end

#format(float) ⇒ Object



388
389
390
391
392
393
394
395
# File 'lib/litestack/liteboard/liteboard.rb', line 388

def format(float)
  float = float.round(3)
  string = float.to_s
  whole, decimal = string.split(".")
  whole = whole.chars.reverse.each_slice(3).map(&:join).join(",").reverse
  whole = [whole, decimal].join(".") if decimal
  whole
end

#indexObject



76
77
78
79
80
81
82
83
# File 'lib/litestack/liteboard/liteboard.rb', line 76

def index
  @order ||= "topic"
  @topics.each do |topic|
    data_points = @lm.topic_data_points(@step, @count, @resolution, topic[0])
    topic << data_points.collect { |r| [r[0], r[2] || 0] }
  end
  render :index
end

#index_sort_url(field) ⇒ Object



347
348
349
# File 'lib/litestack/liteboard/liteboard.rb', line 347

def index_sort_url(field)
  "/?#{compose_query(field)}"
end

#index_urlObject



339
340
341
# File 'lib/litestack/liteboard/liteboard.rb', line 339

def index_url
  "/?res=#{@res}&order=#{@order}&dir=#{@dir}&search=#{@search}"
end

#litecableObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/litestack/liteboard/liteboard.rb', line 292

def litecable
  @order ||= "rcount"
  @topic = "Litecable"
  @events = @lm.events_summaries(@topic, @resolution, @order, @dir, @search, @step * @count)
  @events.each do |event|
    data_points = @lm.event_data_points(@step, @count, @resolution, @topic, event["name"])
    event["counts"] = data_points.collect { |r| [r["rtime"], r["rcount"] || 0] }
  end

  @subscription_count = begin
    @events.find { |t| t["name"] == "subscribe" }["rcount"]
  rescue
    0
  end
  @broadcast_count = begin
    @events.find { |t| t["name"] == "broadcast" }["rcount"]
  rescue
    0
  end
  @message_count = begin
    @events.find { |t| t["name"] == "message" }["rcount"]
  rescue
    0
  end

  @subscriptions_over_time = begin
    @events.find { |t| t["name"] == "subscribe" }["counts"]
  rescue
    []
  end
  @broadcasts_over_time = begin
    @events.find { |t| t["name"] == "broadcast" }["counts"]
  rescue
    []
  end
  @messages_over_time = begin
    @events.find { |t| t["name"] == "message" }["counts"]
  rescue
    []
  end
  @messages_over_time = @messages_over_time.collect.with_index { |msg, i| [msg[0], @broadcasts_over_time[i][1], msg[1]] }

  @top_subscribed_channels = @lm.keys_summaries(@topic, "subscribe", @resolution, @order, @dir, @search, @step * @count).first(8)
  @top_messaged_channels = @lm.keys_summaries(@topic, "message", @resolution, @order, @dir, @search, @step * @count).first(8)
  render :litecable
end

#litecacheObject



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
146
147
# File 'lib/litestack/liteboard/liteboard.rb', line 85

def litecache
  @order ||= "rcount"
  @topic = "Litecache"
  @events = @lm.events_summaries(@topic, @resolution, @order, @dir, @search, @step * @count)
  @events.each do |event|
    data_points = @lm.event_data_points(@step, @count, @resolution, @topic, event["name"])
    event["counts"] = data_points.collect { |r| [r["rtime"], r["rcount"]] }
    event["values"] = data_points.collect { |r| [r["rtime"], r["ravg"]] }
  end
  @snapshot = read_snapshot(@topic)
  @size = begin
    @snapshot[0][:summary][:size]
  rescue
    0
  end
  @max_size = begin
    @snapshot[0][:summary][:max_size]
  rescue
    0
  end
  @full = begin
    (@size / @max_size) * 100
  rescue
    0
  end
  @entries = begin
    @snapshot[0][:summary][:entries]
  rescue
    0
  end
  @gets = @events.find { |t| t["name"] == "get" }
  @sets = @events.find { |t| t["name"] == "set" }
  @reads = begin
    @gets["rcount"]
  rescue
    0
  end
  @writes = begin
    @sets["rcount"]
  rescue
    0
  end
  @hitrate = begin
    @gets["ravg"]
  rescue
    0
  end
  @hits = @reads * @hitrate
  @misses = @reads - @hits
  @reads_vs_writes = begin
    @gets["counts"].collect.with_index { |obj, i| obj.clone << @sets["counts"][i][1] }
  rescue
    []
  end
  @hits_vs_misses = begin
    @gets["values"].collect.with_index { |obj, i| [obj[0], obj[1].to_f * @gets["counts"][i][1].to_f, (1 - obj[1].to_f) * @gets["counts"][i][1].to_f] }
  rescue
    []
  end
  @top_reads = @lm.keys_summaries(@topic, "get", @resolution, @order, @dir, nil, @step * @count).first(8)
  @top_writes = @lm.keys_summaries(@topic, "set", @resolution, @order, @dir, nil, @step * @count).first(8)
  render :litecache
end

#litedbObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/litestack/liteboard/liteboard.rb', line 149

def litedb
  @order ||= "rcount"
  @topic = "Litedb"
  @events = @lm.events_summaries(@topic, @resolution, @order, @dir, @search, @step * @count)
  @events.each do |event|
    data_points = @lm.event_data_points(@step, @count, @resolution, @topic, event["name"])
    event["counts"] = data_points.collect { |r| [r["rtime"], r["rcount"] || 0] }
    event["values"] = data_points.collect { |r| [r["rtime"], r["rtotal"] || 0] }
  end
  @snapshot = read_snapshot(@topic)
  @size = begin
    @snapshot[0][:summary][:size]
  rescue
    0
  end
  @tables = begin
    @snapshot[0][:summary][:tables]
  rescue
    0
  end
  @indexes = begin
    @snapshot[0][:summary][:indexes]
  rescue
    0
  end
  @gets = @events.find { |t| t["name"] == "Read" }
  @sets = @events.find { |t| t["name"] == "Write" }
  @reads = begin
    @gets["rcount"]
  rescue
    0
  end
  @writes = begin
    @sets["rcount"]
  rescue
    0
  end
  @time = begin
    @gets["ravg"]
  rescue
    0
  end
  @reads_vs_writes = begin
    @gets["counts"].collect.with_index { |obj, i| obj.clone << @sets["counts"][i][1] }
  rescue
    []
  end
  @reads_vs_writes_times = begin
    @gets["values"].collect.with_index { |obj, i| [obj[0], obj[1], @sets["values"][i][1].to_f] }
  rescue
    []
  end
  @read_times = begin
    @gets["rtotal"]
  rescue
    0
  end
  @write_times = begin
    @sets["rtotal"]
  rescue
    0
  end
  @slowest = @lm.keys_summaries(@topic, "Read", @resolution, "ravg", "desc", nil, @step * @count).first(8)
  @slowest += @lm.keys_summaries(@topic, "Write", @resolution, "ravg", "desc", nil, @step * @count).first(8)
  @slowest = @slowest.sort_by { |a| a["ravg"] }.last(8).reverse
  @popular = @lm.keys_summaries(@topic, "Read", @resolution, "rtotal", "desc", nil, @step * @count).first(8)
  @popular += @lm.keys_summaries(@topic, "Write", @resolution, "rtotal", "desc", nil, @step * @count).first(8)
  @popular = @popular.sort_by { |a| a["rtotal"] }.last(8).reverse
  render :litedb
end

#litejobObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/litestack/liteboard/liteboard.rb', line 220

def litejob
  @order ||= "rcount"
  @topic = "Litejob"
  @events = @lm.events_summaries(@topic, @resolution, @order, @dir, @search, @step * @count)
  @events.each do |event|
    data_points = @lm.event_data_points(@step, @count, @resolution, @topic, event[:name])
    event["counts"] = data_points.collect { |r| [r["rtime"], r["rcount"] || 0] }
    event["values"] = data_points.collect { |r| [r["rtime"], (r["rtotal"] || 0.0)] }
  end
  @snapshot = read_snapshot(@topic)
  @size = begin
    @snapshot[0][:summary][:size]
  rescue
    0
  end
  @jobs = begin
    @snapshot[0][:summary][:jobs]
  rescue
    0
  end
  @queues = begin
    @snapshot[0][:queues]
  rescue
    {}
  end
  @processed_jobs = @events.find { |e| e["name"] == "perform" }
  @processed_count = begin
    @processed_jobs["rcount"]
  rescue
    0
  end
  @processing_time = begin
    @processed_jobs["rtotal"]
  rescue
    0
  end
  keys_summaries = @lm.keys_summaries(@topic, "perform", @resolution, "rcount", "desc", nil, @step * @count)
  @processed_count_by_queue = keys_summaries.collect { |r| [r["key"], r["rcount"]] }
  @processing_time_by_queue = keys_summaries.collect { |r| [r["key"], r["rtotal"]] } # .sort{|r1, r2| r1['rtotal'] > r2['rtotal'] }
  @processed_count_over_time = begin
    @events.find { |e| e["name"] == "perform" }["counts"]
  rescue
    []
  end
  @processing_time_over_time = begin
    @events.find { |e| e["name"] == "perform" }["values"]
  rescue
    []
  end
  @processed_count_over_time_by_queues = []
  @processing_time_over_time_by_queues = []
  keys = ["Time"]
  keys_summaries.each_with_index do |summary, i|
    key = summary["key"]
    keys << key
    data_points = @lm.key_data_points(@step, @count, @resolution, @topic, "perform", key)
    if i == 0
      data_points.each do |dp|
        @processed_count_over_time_by_queues << [dp["rtime"]]
        @processing_time_over_time_by_queues << [dp["rtime"]]
      end
    end
    data_points.each_with_index do |dp, j|
      @processed_count_over_time_by_queues[j] << (dp["rcount"] || 0)
      @processing_time_over_time_by_queues[j] << (dp["rtotal"] || 0)
    end
  end
  @processed_count_over_time_by_queues.unshift(keys)
  @processing_time_over_time_by_queues.unshift(keys)
  render :litejob
end

#params(key) ⇒ Object



38
39
40
# File 'lib/litestack/liteboard/liteboard.rb', line 38

def params(key)
  URI.decode_uri_component(@params[key.to_s].to_s)
end

#round(float) ⇒ Object



383
384
385
386
# File 'lib/litestack/liteboard/liteboard.rb', line 383

def round(float)
  return 0 unless float.is_a? Numeric
  (float * 100).round.to_f / 100
end

#sorted?(field) ⇒ Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/litestack/liteboard/liteboard.rb', line 364

def sorted?(field)
  @order == field
end

#topic_sort_url(field) ⇒ Object



351
352
353
# File 'lib/litestack/liteboard/liteboard.rb', line 351

def topic_sort_url(field)
  "/topics/#{encode(@topic)}?#{compose_query(field)}"
end

#topic_url(topic) ⇒ Object



343
344
345
# File 'lib/litestack/liteboard/liteboard.rb', line 343

def topic_url(topic)
  "/topics/#{encode(topic)}?res=#{@res}&order=#{@order}&dir=#{@dir}&search=#{@search}"
end