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
/
Rakefile
135 lines (105 loc) · 3.88 KB
/
Rakefile
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
namespace :update do
desc "Run a method on a model (defaults to #update)"
task :manual => :environment do
model = ENV['model'].camelize.constantize
if ENV['method']
model.send ENV['method']
else
model.update
end
end
desc "Run the suite of updates"
task :all => :environment do
Legislator.update
Bill.update
Roll.update
Legislator.update_sponsorships
Legislator.update_contracts
Legislator.update_earmarks
Legislator.update_ratings :limit => 10
Legislator.update_parties
end
end
namespace :development do
desc "Load a fake 'development' api key into the db"
task :api_key => :environment do
key = ENV['key'] || "development"
email = ENV['email'] || "#{key}@example.com"
if ApiKey.where(:key => key).first.nil?
ApiKey.create! :status => "A", :email => email, :key => key
puts "Created '#{key}' API key under email #{email}"
else
puts "'#{key}' API key already exists"
end
end
end
namespace :report do
desc "See reports for a given day (default to last night)"
task :daily => :environment do
# default to today (since we run at past midnight)
day = ENV['day'] || Time.now.midnight.strftime("%Y-%m-%d")
start = Time.parse day
finish = start + 1.day
conditions = {:created_at => {"$gte" => start, "$lt" => finish}}
puts "\nReports for #{day}:\n\n"
Report.all(conditions).each do |report|
puts "#{report}\n\n"
end
end
desc "See latest failed reports (defaults to 5)"
task :failure => :environment do
limit = ENV['n'] || 5
puts "Latest #{limit} failures:\n\n"
Report.all(:order => "created_at DESC", :limit => 5, :status => "FAILURE").each do |report|
puts "(#{report.created_at.strftime "%Y-%m-%d"}) #{report}\n\n"
end
end
end
namespace :api do
desc "Send analytics to the central API analytics department."
task :analytics => :environment do
# default to yesterday
day = ENV['day'] || (Time.now.midnight - 1.day).strftime("%Y-%m-%d")
test = !ENV['test'].nil?
start_time = Time.now
start = Time.parse day
finish = start + 1.day
conditions = {:created_at => {"$gte" => start, "$lt" => finish}}
reports = []
# get down to the driver level for the iteration
hits = MongoMapper.database.collection :hits
keys = hits.distinct :key, conditions
keys.each do |key|
methods = hits.distinct :method, conditions.merge(:key => key)
methods.each do |method|
count = Hit.count conditions.merge(:key => key, :method => method)
reports << {:key => key, :method => method, :count => count}
end
end
sum_count = reports.map {|r| r[:count]}.sum
hit_count = Hit.count conditions
if sum_count == hit_count
api_name = config[:services][:api_name]
shared_secret = config[:services][:shared_secret]
reports.each do |report|
begin
SunlightServices.report(report[:key], report[:method], report[:count], day, api_name, shared_secret) unless test
rescue Exception => exception
Report.failure 'Analytics', "Problem filing a report, error and report data attached", {:error_message => exception.message, :backtrace => exception.backtrace, :report => report, :day => day}
end
end
if test
puts "\nWould report for #{day}:\n\n#{reports.inspect}\n\nTotal hits: #{reports.sum {|r| r[:count]}}\n\n"
else
Report.success 'Analytics', "Filed #{reports.size} report(s) for #{day}.", {:elapsed_time => (Time.now - start_time)}
end
else
Report.failure 'Analytics', "Sanity check failed: error calculating hit reports. Reports attached.", {:reports => reports, :day => day}
end
end
end
task :environment do
require 'rubygems'
require 'bundler/setup'
require 'config/environment'
end