Cleanup trailing whitespace
[rails.git] / script / statistics
1 #!/usr/bin/env ruby
2
3 require File.dirname(__FILE__) + '/../config/environment'
4
5 start_time = Time.now
6
7 puts "<html>"
8 puts "<head>"
9 puts "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
10 puts "<title>OpenStreetMap Statistics</title>"
11 puts "<style>th { text-align: left }</style>"
12 puts "</head>"
13 puts "<body>"
14 puts "<h2>OpenStreetMap stats report run at #{start_time.to_s}</h2>"
15
16 begin
17   ActiveRecord::Base.transaction do
18     user_count = User.where(:status => ["active", "confirmed", "suspended"]).count
19     tracepoint_count = Tracepoint.count
20     node_count = Node.where(:visible => true).count
21     way_count = Way.where(:visible => true).count
22     relation_count = Relation.where(:visible => true).count
23
24     puts "<table>"
25     puts "<tr><td>Number of users</td><td>#{user_count}</td></tr>"
26     puts "<tr><td>Number of uploaded GPS points</td><td>#{tracepoint_count}</td></tr>"
27     puts "<tr><td>Number of nodes</td><td>#{node_count}</td></tr>"
28     puts "<tr><td>Number of ways</td><td>#{way_count}</td></tr>"
29     puts "<tr><td>Number of relations</td><td>#{relation_count}</td></tr>"
30     puts "</table>"
31
32     puts "<h2>Top 50 users for uploads of GPS data</h2>"
33     puts "<table>"
34     puts "<tr><th>User</th><th>Number of Points</th></tr>"
35
36     Trace.where(:inserted => true).group(:user_id).order("sum_size DESC").limit(50).sum(:size).each do |user, count|
37       display_name = User.find(user).display_name.gsub('@', ' at ').gsub('.', ' dot ')
38       puts "<tr><td><a href=\"/user/#{display_name}\">#{display_name}</a></td><td>#{count}</td></tr>"
39     end
40
41     puts "</table>"
42
43     puts "<h2>Number of users editing over the past...</h2>"
44     puts "<table>"
45     puts "<tr><th>Data Type</th><th>Day</th><th>Week</th><th>Month</th></tr>"
46
47     day_count = Trace.where("timestamp > NOW() - '1 DAY'::INTERVAL").distinct.count(:user_id)
48     week_count = Trace.where("timestamp > NOW() - '7 DAYS'::INTERVAL").distinct.count(:user_id)
49     month_count = Trace.where("timestamp > NOW() - '28 DAYS'::INTERVAL").distinct.count(:user_id)
50
51     puts "<tr><th>GPX Files</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
52
53     day_count = OldNode.where("timestamp > NOW() - '1 DAY'::INTERVAL").joins(:changeset).distinct.count(:user_id)
54     week_count = OldNode.where("timestamp > NOW() - '7 DAYS'::INTERVAL").joins(:changeset).distinct.count(:user_id)
55     month_count = OldNode.where("timestamp > NOW() - '28 DAYS'::INTERVAL").joins(:changeset).distinct.count(:user_id)
56
57     puts "<tr><th>Nodes</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
58
59     puts "</table>"
60
61     puts "<h2>Top users editing over the past...</h2>"
62     puts "<table>"
63     puts "<tr><th>Day</th><th>Week</th><th>Month</th></tr>"
64
65     day_users = OldNode.where("timestamp > NOW() - '1 DAY'::INTERVAL").joins(:changeset).order("count_all DESC").group(:user_id).count
66     week_users = OldNode.where("timestamp > NOW() - '7 DAYS'::INTERVAL").joins(:changeset).order("count_all DESC").limit(60).group(:user_id).count
67     month_users = OldNode.where("timestamp > NOW() - '28 DAYS'::INTERVAL").joins(:changeset).order("count_all DESC").limit(60).group(:user_id).count
68
69     day_users.zip(week_users, month_users).each do |row|
70       puts "<tr>"
71       row.each do |column|
72         if column.nil?
73           puts "<td></td>"
74         else
75           display_name = User.find(column[0]).display_name.gsub('@', ' at ').gsub('.', ' dot ')
76           count = column[1]
77           puts "<td>#{count} <a href=\"/user/#{display_name}\">#{display_name}</a></td>"
78         end
79       end
80       puts "</tr>"
81     end
82
83     puts "</table>"
84   end
85 rescue Exception => e
86   puts "<p><em>Exception: #{e.to_s}</em><br />#{e.backtrace.join('<br />')}</p>"
87 end
88
89 puts "<p>Report took #{(Time.new - start_time).to_s} seconds to run</p>"
90 puts "</body>"
91 puts "</html>"
92
93 exit 0