Move stats to admin until performance issues are resolved

This commit is contained in:
Kyle Drake 2016-06-30 14:44:47 -07:00
parent 68e4424725
commit 8d71c04f88
2 changed files with 158 additions and 0 deletions

View file

@ -73,6 +73,104 @@ get '/admin/email' do
erb :'admin/email'
end
get '/admin/stats' do
require_admin
# expires 14400, :public, :must_revalidate if self.class.production? # 4 hours
@stats = {
total_hosted_site_hits: DB['SELECT SUM(hits) FROM sites'].first[:sum],
total_hosted_site_views: DB['SELECT SUM(views) FROM sites'].first[:sum],
total_site_changes: DB['select max(changed_count) from sites'].first[:max],
total_sites: Site.count
}
# Start with the date of the first created site
start = Site.select(:created_at).
exclude(created_at: nil).
order(:created_at).
first[:created_at].to_date
runner = start
monthly_stats = []
now = Date.today
while Date.new(runner.year, runner.month, 1) <= Date.new(now.year, now.month, 1)
monthly_stats.push(
date: runner,
sites_created: Site.where(created_at: runner..runner.next_month).count,
total_from_start: Site.where(created_at: start..runner.next_month).count,
supporters: Site.where(created_at: start..runner.next_month).exclude(stripe_customer_id: nil).count,
)
runner = runner.next_month
end
@stats[:monthly_stats] = monthly_stats
if $stripe_cache && Time.now < $stripe_cache[:time] + 14400
customers = $stripe_cache[:customers]
else
customers = Stripe::Customer.all limit: 100000
$stripe_cache = {
customers: customers,
time: Time.now
}
end
@stats[:monthly_revenue] = 0.0
subscriptions = []
@stats[:cancelled_subscriptions] = 0
customers.each do |customer|
sub = {created_at: Time.at(customer.created)}
if customer[:subscriptions][:data].empty?
@stats[:cancelled_subscriptions] += 1
next
end
next if customer[:subscriptions][:data].first[:plan][:amount] == 0
sub[:status] = 'active'
plan = customer[:subscriptions][:data].first[:plan]
sub[:amount_without_fees] = (plan[:amount] / 100.0).round(2)
sub[:percentage_fee] = (sub[:amount_without_fees]/(100/2.9)).ceil_to(2)
sub[:fixed_fee] = 0.30
sub[:amount] = sub[:amount_without_fees] - sub[:percentage_fee] - sub[:fixed_fee]
if(plan[:interval] == 'year')
sub[:amount] = (sub[:amount] / 12).round(2)
end
@stats[:monthly_revenue] += sub[:amount]
subscriptions.push sub
end
@stats[:subscriptions] = subscriptions
# Hotwired for now
@stats[:expenses] = 300.0 #/mo
@stats[:percent_until_profit] = (
(@stats[:monthly_revenue].to_f / @stats[:expenses]) * 100
)
@stats[:poverty_threshold] = 11_945
@stats[:poverty_threshold_percent] = (@stats[:monthly_revenue].to_f / ((@stats[:poverty_threshold]/12) + @stats[:expenses])) * 100
# http://en.wikipedia.org/wiki/Poverty_threshold
@stats[:average_developer_salary] = 93_280.00 # google "average developer salary"
@stats[:percent_until_developer_salary] = (@stats[:monthly_revenue].to_f / ((@stats[:average_developer_salary]/12) + @stats[:expenses])) * 100
erb :'admin/stats'
end
post '/admin/email' do
require_admin

60
views/admin/stats.erb Normal file
View file

@ -0,0 +1,60 @@
<div class="header-Outro">
<div class="row content single-Col">
<h1>Neocities Site Statistics</h1>
</div>
</div>
<div class="content single-Col misc-page">
<article>
<table class="table">
<tr>
<td><strong>Hits</strong> (sites hosted on Neocities)</td>
<td><strong><%= @stats[:total_hosted_site_hits].to_comma_separated %></strong></td>
</tr>
<tr>
<td><strong>Unique Views</strong> (sites hosted on Neocities)</td>
<td><strong><%= @stats[:total_hosted_site_views].to_comma_separated %></strong></td>
</tr>
<tr>
<td><strong>Sites</strong></td>
<td><strong><%= @stats[:total_sites].to_comma_separated %></strong></td>
</tr>
<tr>
<td><strong>Site Updates</strong></td>
<td><strong><%= @stats[:total_site_changes].to_comma_separated %></strong></td>
</tr>
</table>
<h2>Monthly Statistics</h2>
<table class="table">
<tr>
<th>Year</th>
<th>Month</th>
<th>Sites Created</th>
<th>Change</th>
<th>Total</th>
<th>Supporters</th>
</tr>
<% @stats[:monthly_stats].each_with_index do |stat,i| %>
<tr>
<td><%= stat[:date].year %></td>
<td>
<%= stat[:date].strftime('%B') %>
<% if Time.now.month == stat[:date].month && Time.now.year == stat[:date].year %>
<small>current</small>
<% end %></td>
<td><%= stat[:sites_created] %></td>
<td>
<% if i != 0 && i != @stats[:monthly_stats].length-1 %>
<%= (((stat[:sites_created].to_f - @stats[:monthly_stats][i-1][:sites_created]) / @stats[:monthly_stats][i-1][:sites_created]) * 100).round(2) %>%
<% end %>
</td>
<td>
<%= stat[:total_from_start] %></td>
</td>
<td><%= stat[:supporters] %></td>
</tr>
<% end %>
</table>
</article>
</div>