-
Notifications
You must be signed in to change notification settings - Fork 1
/
pushpin.rb
executable file
·115 lines (92 loc) · 2.78 KB
/
pushpin.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
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'mongo'
require 'thor'
require 'yaml'
require 'active_support/all'
require 'aws/s3'
S3_ACCESS_KEY = ""
S3_SECRET = ""
S3_BUCKET = ""
include Mongo
class Pushpin < Thor
desc "stats", "Compute edit stats for @pushpinapp"
method_option :s3, :aliases => "-s", :desc => "Store stats json on S3", :required => false
def stats
compute_stats
store_stats_on_s3 if options[:s3]
end
no_tasks do
def mongo_client
@client ||= MongoClient.new("localhost", 27017)
end
def mongo_database
@db ||= mongo_client.db("osm_changesets")
end
def changesets_collection
@changesets ||= mongo_database.collection("changesets")
end
def state_collection
@state_collection ||= mongo_database.collection("state")
end
def stats_collection
@stats_collection ||= mongo_database.collection("pushpin_users")
end
def compute_stats
map = %Q{
function() {
emit({ user: this.user}, { edits: 1 });
}
}
reduce = %Q{
function(key, values) {
var result = { edits: 0 };
values.forEach(function(value) {
result.edits += value.edits;
});
return result;
}
}
options = {
query: {
"tags.created_by" => /Pushpin/
},
out: { :replace => 'pushpin_users' }
}
changesets_collection.map_reduce(map, reduce, options)
json = {
top_users: top_users,
recent_edits: recent_edits,
total_edits: total_edits
}
File.open('stats.json', 'w') {|f| f.write(json.to_json)}
end
def store_stats_on_s3
AWS::S3::Base.establish_connection!(access_key_id: S3_ACCESS_KEY, secret_access_key: S3_SECRET)
default_options = { cache_control: 'no-cache, no-store, max-age=0, must-revalidate',
expires: 'Fri, 01 Jan 1990 00:00:00 GMT',
pragma: 'no-cache' }
AWS::S3::S3Object.store(
'stats.json',
open('stats.json'),
S3_BUCKET,
{ :content_type => 'application/json', :access => :public_read }.merge(default_options)
)
end
def top_users
stats_collection.find.sort([['value.edits', :desc]]).to_a.map do |user|
{ name: user['_id']['user'], edits: user['value']['edits'].to_i }
end
end
def recent_edits
changesets_collection.find({'tags.created_by' => /Pushpin/}).sort([['created_at', :desc]]).limit(500).map do |edit|
{ name: edit['user'], tags: edit['tags'], date: edit['created_at'], id: edit['id']}
end
end
def total_edits
changesets_collection.find({'tags.created_by' => /Pushpin/}).count.to_i
end
end
end
Pushpin.start