Merge 14394:14533 from trunk.
[rails.git] / script / statistics
1 #!/usr/bin/env ruby
2
3 require File.dirname(__FILE__) + '/../config/environment'
4 require 'generator'
5
6 start_time = Time.now
7
8 puts "<html>"
9 puts "<head>"
10 puts "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
11 puts "<title>OpenStreetMap Statistics</title>"
12 puts "<style>th { text-align: left }</style>"
13 puts "</head>"
14 puts "<body>"
15 puts "<h2>OpenStreetMap stats report run at #{start_time.to_s}</h2>"
16
17 begin
18   user_count = User.count(:conditions => "active = true")
19   tracepoint_count = Tracepoint.count()
20   node_count = Node.count(:conditions => "visible = true")
21   way_count = Way.count(:conditions => "visible = true")
22   relation_count = Relation.count(:conditions => "visible = true")
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.sum(:size, :group => :user_id, :order => "sum_size DESC", :limit => 50).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.count(:user_id, :distinct => true, 
48                           :conditions => "timestamp > NOW() - INTERVAL 1 DAY")
49   week_count = Trace.count(:user_id, :distinct => true, 
50                            :conditions => "timestamp > NOW() - INTERVAL 7 DAY")
51   month_count = Trace.count(:user_id, :distinct => true, 
52                             :conditions => "timestamp > NOW() - INTERVAL 28 DAY")
53
54   puts "<tr><th>GPX Files</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
55
56   day_count = OldNode.count(:user_id, :distinct => true,
57                             :include => :changeset,
58                             :conditions => "timestamp > NOW() - INTERVAL 1 DAY")
59   week_count = OldNode.count(:user_id, :distinct => true, 
60                              :include => :changeset,
61                              :conditions => "timestamp > NOW() - INTERVAL 7 DAY")
62   month_count = OldNode.count(:user_id, :distinct => true, 
63                               :include => :changeset,
64                               :conditions => "timestamp > NOW() - INTERVAL 28 DAY")
65
66   puts "<tr><th>Nodes</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
67
68   puts "</table>"
69   
70   puts "<h2>Top users editing over the past...</h2>"
71   puts "<table>"
72   puts "<tr><th>Day</th><th>Week</th><th>Month</th></tr>"
73
74   day_users = OldNode.count(:conditions => "timestamp > NOW() - INTERVAL 1 DAY",
75                             :include => :changeset, :group => :user_id,
76                             :order => "count_all DESC")
77   week_users = OldNode.count(:conditions => "timestamp > NOW() - INTERVAL 7 DAY",
78                              :include => :changeset, :group => :user_id,
79                              :order => "count_all DESC", :limit => 60)
80   month_users = OldNode.count(:conditions => "timestamp > NOW() - INTERVAL 28 DAY",
81                               :include => :changeset, :group => :user_id,
82                               :order => "count_all DESC", :limit => 60)
83
84   SyncEnumerator.new(day_users, week_users, month_users).each do |row|
85     puts "<tr>"
86     row.each do |column|
87       if column.nil?
88         puts "<td></td>"
89       else
90         display_name = User.find(column[0]).display_name.gsub('@', ' at ').gsub('.', ' dot ')
91         count = column[1]
92         puts "<td>#{count} <a href=\"/user/#{display_name}\">#{display_name}</a></td>"
93       end
94     end
95     puts "</tr>"
96   end
97
98   puts "</table>"
99 rescue Exception => e
100   puts "<p><em>Exception: #{e.to_s}</em><br />#{e.backtrace.join('<br />')}</p>"
101 end
102
103 puts "<p>Report took #{(Time.new - start_time).to_s} seconds to run</p>"
104 puts "</body>"
105 puts "</html>"