-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhumpty.rb
113 lines (94 loc) · 2.64 KB
/
humpty.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
begin
require File.expand_path('../.bundle/environment', __FILE__)
rescue LoadError
require "rubygems"
require "bundler"
Bundler.setup
end
require 'sinatra/base'
Bundler.require
module Humpty
class Error < StandardError; end
class App < Sinatra::Base
require 'server'
require 'partials'
enable :static
disable :run
set :public, File.expand_path('../public', __FILE__)
set :queue_threshold_file, 'config/queue_thresholds.yml'
set :sessions, true
before do
set_server
end
get '/' do
@control = @server.control
@queues = @server.queues
haml :overview
end
get '/queues/?' do
@queues = @server.queues
haml :queues
end
get '/bindings' do
@bindings = @server.bindings
haml :bindings
end
get '/config' do
@queues = @server.queues
@config = queue_config[@server.id] || {}
haml :config
end
get '/queues/:name/delete' do
@server.queue(params[:name]).delete
redirect '/'
end
get '/queues/:name/purge' do
@server.queue(params[:name]).purge
redirect '/'
end
get '/queues/:name' do
@bindings = @server.bindings.select {|b| b["queue"] == params[:name]}
@queue = @server.queues.detect{|q| q["name"] == params[:name]}
haml :queue
end
post '/config' do
queue_config.update(@server.id => params["queues"])
File.open(options.queue_threshold_file, 'w') do |file|
file.puts queue_config.to_yaml
end
redirect '/config'
end
get '/exchanges/?' do
@exchanges = @server.exchanges.reject {|e| e["name"].blank? }
haml :exchanges
end
helpers do
include Sinatra::Partials
def class_for_queue(queue)
@message_threshold = queue_config[@server.id][queue["name"]].to_i rescue 0
current_amount = queue["messages"].to_i
if @message_threshold == 0 || @message_threshold >= current_amount
nil
else
"critical_queue"
end
end
def queue_config
File.open(options.queue_threshold_file, "w") unless File.exists?(options.queue_threshold_file)
@queue_config ||= (YAML.load_file(options.queue_threshold_file) || {})
end
end
not_found do
haml "#failbunny\n%img(src='img/bunny.jpg')"
end
def set_server(server = nil)
@servers = Server.configurations.keys
session["server"] = server_name = server || params["server"] || session["server"] || @servers.first
begin
@server = Server.new(server_name)
rescue Server::ConfigurationException
set_server(@servers.first)
end
end
end
end