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