This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.rb
65 lines (49 loc) · 1.51 KB
/
report.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
require 'pony'
class Report
include MongoMapper::Document
key :status, String, :required => true
key :source, String, :required => true
key :message, String
key :elapsed_time, Float
timestamps!
def self.file(status, source, message, objects = {})
report = Report.new :source => source.to_s, :status => status, :message => message
report.attributes = objects
report.save
puts report.to_s
send_email report if ['FAILURE', 'WARNING'].include?(status.to_s)
report
end
def self.success(source, message, objects = {})
file 'SUCCESS', source, message, objects
end
def self.failure(source, message, objects = {})
file 'FAILURE', source, message, objects
end
def self.warning(source, message, objects = {})
file 'WARNING', source, message, objects
end
def self.latest(model, size = 1)
reports = Report.all :conditions => {:source => model.to_s}, :order => "created_at DESC", :limit => size
size > 1 ? reports : reports.first
end
def self.send_email(report)
if email[:to] and email[:to].any?
Pony.mail email.merge(:subject => report.to_s, :body => report.attributes.inspect)
end
end
def self.email=(details)
@email = details
end
def self.email
@email
end
def to_s
"[#{status}] #{source}#{elapsed_time ? " [#{to_minutes elapsed_time.to_i}]" : ""}\n #{message}"
end
def to_minutes(seconds)
min = seconds / 60
sec = seconds % 60
"#{min > 0 ? "#{min}m," : ""}#{sec}s"
end
end