Merge remote-tracking branch 'upstream/pull/3426'
[rails.git] / script / statistics
1 #!/usr/bin/env ruby
2
3 require File.join(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 "<h1>OpenStreetMap stats</h1>"
15 puts "<h2>Report run at #{start_time}</h2>"
16
17 begin
18   ActiveRecord::Base.transaction do
19     user_count = User.where(:status => %w[active confirmed suspended]).count
20     tracepoint_count = Tracepoint.count
21     node_count = Node.where(:visible => true).count
22     way_count = Way.where(:visible => true).count
23     relation_count = Relation.where(:visible => true).count
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 id="top-traces">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.where(:inserted => true).group(:user_id).order("sum_size DESC").limit(50).sum(:size).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 id="number-of-editors">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.where("timestamp > NOW() - '1 DAY'::INTERVAL").distinct.count(:user_id)
49     week_count = Trace.where("timestamp > NOW() - '7 DAYS'::INTERVAL").distinct.count(:user_id)
50     month_count = Trace.where("timestamp > NOW() - '28 DAYS'::INTERVAL").distinct.count(:user_id)
51
52     puts "<tr><th>GPX Files</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
53
54     day_count = OldNode.where("timestamp > NOW() - '1 DAY'::INTERVAL").joins(:changeset).distinct.count(:user_id)
55     week_count = OldNode.where("timestamp > NOW() - '7 DAYS'::INTERVAL").joins(:changeset).distinct.count(:user_id)
56     month_count = OldNode.where("timestamp > NOW() - '28 DAYS'::INTERVAL").joins(:changeset).distinct.count(:user_id)
57
58     puts "<tr><th>Nodes</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
59
60     puts "</table>"
61
62     puts '<h2 id="top-editors">Top users editing over the past...</h2>'
63     puts "<table>"
64     puts "<tr><th>Day</th><th>Week</th><th>Month</th></tr>"
65
66     day_users = OldNode.where("timestamp > NOW() - '1 DAY'::INTERVAL").joins(:changeset).order("count_all DESC").group(:user_id).count
67     week_users = OldNode.where("timestamp > NOW() - '7 DAYS'::INTERVAL").joins(:changeset).order("count_all DESC").limit(60).group(:user_id).count
68     month_users = OldNode.where("timestamp > NOW() - '28 DAYS'::INTERVAL").joins(:changeset).order("count_all DESC").limit(60).group(:user_id).count
69
70     day_users.zip(week_users, month_users).each do |row|
71       puts "<tr>"
72       row.each do |column|
73         if column.nil?
74           puts "<td></td>"
75         else
76           display_name = User.find(column[0]).display_name.gsub("@", " at ").gsub(".", " dot ")
77           count = column[1]
78           puts "<td>#{count} <a href=\"/user/#{display_name}\">#{display_name}</a></td>"
79         end
80       end
81       puts "</tr>"
82     end
83
84     puts "</table>"
85   end
86 rescue StandardError => e
87   puts "<p><em>Exception: #{e}</em><br />#{e.backtrace.join('<br />')}</p>"
88 end
89
90 puts "<p>Report took #{Time.new - start_time} seconds to run</p>"
91 puts "</body>"
92 puts "</html>"
93
94 exit 0