]> git.openstreetmap.org Git - rails.git/blob - script/statistics
Show a helpful message when there are no traces to display
[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   ActiveRecord::Base.transaction do
19     user_count = User.count(:conditions => { :status => ["active", "confirmed", "suspended"] })
20     tracepoint_count = Tracepoint.count()
21     node_count = Node.count(:conditions => "visible = true")
22     way_count = Way.count(:conditions => "visible = true")
23     relation_count = Relation.count(:conditions => "visible = true")
24
25     puts "<table>"
26     puts "<tr><td>Number of users</td><td>#{user_count}</td></tr>"
27     puts "<tr><td>Number of uploaded GPS points</td><td>#{tracepoint_count}</td></tr>"
28     puts "<tr><td>Number of nodes</td><td>#{node_count}</td></tr>"
29     puts "<tr><td>Number of ways</td><td>#{way_count}</td></tr>"
30     puts "<tr><td>Number of relations</td><td>#{relation_count}</td></tr>"
31     puts "</table>"
32
33     puts "<h2>Top 50 users for uploads of GPS data</h2>"
34     puts "<table>"
35     puts "<tr><th>User</th><th>Number of Points</th></tr>"
36
37     Trace.sum(:size, :conditions => { :inserted => true }, :group => :user_id, :order => "sum_size DESC", :limit => 50).each do |user, count|
38       display_name = User.find(user).display_name.gsub('@', ' at ').gsub('.', ' dot ')
39       puts "<tr><td><a href=\"/user/#{display_name}\">#{display_name}</a></td><td>#{count}</td></tr>"
40     end
41
42     puts "</table>"
43
44     puts "<h2>Number of users editing over the past...</h2>"
45     puts "<table>"
46     puts "<tr><th>Data Type</th><th>Day</th><th>Week</th><th>Month</th></tr>"
47
48     day_count = Trace.count(:user_id, :distinct => true, 
49                             :conditions => "timestamp > NOW() - '1 DAY'::INTERVAL")
50     week_count = Trace.count(:user_id, :distinct => true, 
51                              :conditions => "timestamp > NOW() - '7 DAYS'::INTERVAL")
52     month_count = Trace.count(:user_id, :distinct => true, 
53                               :conditions => "timestamp > NOW() - '28 DAYS'::INTERVAL")
54
55     puts "<tr><th>GPX Files</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
56
57     day_count = OldNode.count(:user_id, :distinct => true, :joins => :changeset,
58                               :conditions => "timestamp > NOW() - '1 DAY'::INTERVAL")
59     week_count = OldNode.count(:user_id, :distinct => true,  :joins => :changeset,
60                                :conditions => "timestamp > NOW() - '7 DAYS'::INTERVAL")
61     month_count = OldNode.count(:user_id, :distinct => true, :joins => :changeset,
62                                 :conditions => "timestamp > NOW() - '28 DAYS'::INTERVAL")
63
64     puts "<tr><th>Nodes</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
65
66     puts "</table>"
67   
68     puts "<h2>Top users editing over the past...</h2>"
69     puts "<table>"
70     puts "<tr><th>Day</th><th>Week</th><th>Month</th></tr>"
71
72     day_users = OldNode.count(:conditions => "timestamp > NOW() - '1 DAY'::INTERVAL",
73                               :joins => :changeset, :group => :user_id,
74                               :order => "count_all DESC")
75     week_users = OldNode.count(:conditions => "timestamp > NOW() - '7 DAYS'::INTERVAL",
76                                :joins => :changeset, :group => :user_id,
77                                :order => "count_all DESC", :limit => 60)
78     month_users = OldNode.count(:conditions => "timestamp > NOW() - '28 DAYS'::INTERVAL",
79                                 :joins => :changeset, :group => :user_id,
80                                 :order => "count_all DESC", :limit => 60)
81
82     SyncEnumerator.new(day_users, week_users, month_users).each do |row|
83       puts "<tr>"
84       row.each do |column|
85         if column.nil?
86           puts "<td></td>"
87         else
88           display_name = User.find(column[0]).display_name.gsub('@', ' at ').gsub('.', ' dot ')
89           count = column[1]
90           puts "<td>#{count} <a href=\"/user/#{display_name}\">#{display_name}</a></td>"
91         end
92       end
93       puts "</tr>"
94     end
95
96     puts "</table>"
97   end
98 rescue Exception => e
99   puts "<p><em>Exception: #{e.to_s}</em><br />#{e.backtrace.join('<br />')}</p>"
100 end
101
102 puts "<p>Report took #{(Time.new - start_time).to_s} seconds to run</p>"
103 puts "</body>"
104 puts "</html>"
105
106 exit 0