2 # frozen_string_literal: true
4 require_relative File.join("..", "config", "environment")
6 ActiveRecord::Base.connection.execute("SET statement_timeout = 0 ")
8 start_time = Time.now.utc
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>"
17 puts "<h1>OpenStreetMap stats</h1>"
18 puts "<h2>Report run at #{start_time}</h2>"
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
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>"
36 puts '<h2 id="top-traces">Top 50 users for uploads of GPS data</h2>'
38 puts "<tr><th>User</th><th>Number of Points</th></tr>"
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>"
47 puts '<h2 id="number-of-editors">Number of users editing over the past...</h2>'
49 puts "<tr><th>Data Type</th><th>Day</th><th>Week</th><th>Month</th></tr>"
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)
55 puts "<tr><th>GPX Files</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
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)
61 puts "<tr><th>Nodes</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"
65 puts '<h2 id="top-editors">Top users editing over the past...</h2>'
67 puts "<tr><th>Day</th><th>Week</th><th>Month</th></tr>"
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
73 day_users.zip(week_users, month_users).each do |row|
79 display_name = User.find(column[0]).display_name.gsub("@", " at ").gsub(".", " dot ")
81 puts "<td>#{count} <a href=\"https://www.openstreetmap.org/user/#{display_name}\">#{display_name}</a></td>"
89 rescue StandardError => e
90 puts "<p><em>Exception: #{e}</em><br />#{e.backtrace.join('<br />')}</p>"
93 puts "<p>Report took #{Time.now.utc - start_time} seconds to run</p>"