-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpigeonhole.rb
executable file
·155 lines (137 loc) · 4.18 KB
/
pigeonhole.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
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env ruby
# frozen_string_literal: true
$LOAD_PATH.push(File.expand_path(File.join(__FILE__, '..', 'lib')))
require 'sinatra'
require 'influx'
require 'haml'
require 'date'
require 'highcharts'
require 'uri'
require 'pagerduty'
require 'methadone'
require 'version'
include Methadone::CLILogging
influxdb = Influx::Db.new
pagerduty = Pagerduty.new
def today
Time.now.strftime('%Y-%m-%d')
end
get '/' do
@mapper = {
'ack' => 'Acknowleged',
'resolve' => 'Resolved',
'stddev' => 'Std Dev (σ)',
'95_percentile' => '95th Percentile',
'mean' => 'Average (x̄)'
}
@types = %w(ack resolve)
@stats = %w(mean stddev 95_percentile)
@stat_summary = influxdb.generate_stats
@pagerduty_url = pagerduty.pagerduty_url
@acked, @unacked = influxdb.unaddressed_alerts
haml :index
end
get '/categorisation/?' do
redirect "/categorisation/#{today}/#{today}"
end
get '/alert-frequency/?' do
redirect "/alert-frequency/#{today}/#{today}"
end
get '/alert-response/?' do
redirect "/alert-response/#{today}/#{today}"
end
get '/noise-candidates/?' do
redirect "/noise-candidates/#{today}/#{today}"
end
def search_precondition
return '' unless @search
"and incident_key =~ /.*#{@search}.*/i"
end
get '/categorisation/:start_date/:end_date' do
@categories = [
'not set',
'real',
'improved',
'self recovered',
'needs documentation',
'unclear, needs discussion'
]
@start_date = params['start_date']
@end_date = params['end_date']
@search = params['search']
@pagerduty_url = pagerduty.pagerduty_url
@incidents = influxdb.find_incidents(@start_date, @end_date, :conditions => search_precondition)
haml :categorisation
end
get '/alert-frequency/:start_date/:end_date' do
@start_date = params['start_date']
@end_date = params['end_date']
@search = params['search']
@incidents = influxdb.incident_frequency(@start_date, @end_date, search_precondition)
@total = @incidents.map { |x| x['count'] }.inject(:+) || 0
@series = HighCharts.alert_frequency(@incidents)
haml :"alert-frequency"
end
get '/alert-response/:start_date/:end_date' do
@start_date = params['start_date']
@end_date = params['end_date']
@search = params['search']
resp = influxdb.alert_response(@start_date, @end_date, search_precondition)
@series = HighCharts.alert_response(resp)
# Build table data
@incidents = resp[:incidents] || []
@total = @incidents.count
@acked = @incidents.count { |x| (x['time_to_ack']).nonzero? }
@pagerduty_url = pagerduty.pagerduty_url
@incidents.each do |incident|
incident['entity'], incident['check'] = incident['incident_key'].split(':', 2)
incident['ack_by'] = 'N/A' if incident['ack_by'].nil?
incident['time_to_ack'] = 'N/A' if (incident['time_to_ack']).zero?
incident['time_to_resolve'] = 'N/A' if (incident['time_to_resolve']).zero?
end
haml :"alert-response"
end
get '/noise-candidates/:start_date/:end_date' do
@start_date = params['start_date']
@end_date = params['end_date']
@search = params['search']
@incidents = influxdb.noise_candidates(@start_date, @end_date, search_precondition)
@total = @incidents.count
haml :"noise-candidates"
end
post '/categorisation/:start_date/:end_date' do
uri = "#{params['start_date']}/#{params['end_date']}"
uri += "?search=#{params['search']}" if params['search']
opts = {
:start_date => params[:start_date],
:end_date => params[:end_date],
:search => params[:search]
}
params.delete('start_date')
params.delete('end_date')
params.delete('search')
params.delete('splat')
params.delete('captures')
opts[:data] = params
influxdb.save_categories(opts)
redirect "/#{uri}"
end
post '/pagerduty' do
request.body.rewind # in case someone already read it
data = JSON.parse(request.body.read)
begin
incidents = pagerduty.incidents_from_webhook(data)
raise 'No incidents found' if incidents.empty?
incident_ids = incidents.map { |x| x[:id] }
influxdb.insert_incidents(incidents)
status 200
"Inserted incidents: #{incident_ids.join(', ')}"
rescue => e
status 500
{
:data => data,
:error => e.class,
:message => e.message
}.to_json
end
end