From 8d71c04f888dc7293913e78c24a34362e478aa87 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Thu, 30 Jun 2016 14:44:47 -0700 Subject: [PATCH] Move stats to admin until performance issues are resolved --- app/admin.rb | 98 +++++++++++++++++++++++++++++++++++++++++++ views/admin/stats.erb | 60 ++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 views/admin/stats.erb diff --git a/app/admin.rb b/app/admin.rb index 94923046..dcfdb0ba 100644 --- a/app/admin.rb +++ b/app/admin.rb @@ -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 diff --git a/views/admin/stats.erb b/views/admin/stats.erb new file mode 100644 index 00000000..a53502c3 --- /dev/null +++ b/views/admin/stats.erb @@ -0,0 +1,60 @@ +
+
+

Neocities Site Statistics

+
+
+ +
+
+ + + + + + + + + + + + + + + + + +
Hits (sites hosted on Neocities)<%= @stats[:total_hosted_site_hits].to_comma_separated %>
Unique Views (sites hosted on Neocities)<%= @stats[:total_hosted_site_views].to_comma_separated %>
Sites<%= @stats[:total_sites].to_comma_separated %>
Site Updates<%= @stats[:total_site_changes].to_comma_separated %>
+ +

Monthly Statistics

+ + + + + + + + + + <% @stats[:monthly_stats].each_with_index do |stat,i| %> + + + + + + + + + + <% end %> +
YearMonthSites CreatedChangeTotalSupporters
<%= stat[:date].year %> + <%= stat[:date].strftime('%B') %> + <% if Time.now.month == stat[:date].month && Time.now.year == stat[:date].year %> + current + <% end %><%= stat[:sites_created] %> + <% 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 %> + + <%= stat[:total_from_start] %><%= stat[:supporters] %>
+
+