diff --git a/Gemfile b/Gemfile index 022c817b..1d941b34 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ +source 'https://code.stripe.com' source 'https://rubygems.org' gem 'sinatra' @@ -15,11 +16,10 @@ gem 'mail' gem 'google-api-client', require: 'google/api_client' gem 'tilt' gem 'erubis' -gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby' +gem 'stripe' gem 'screencap' gem 'cocaine' gem 'zipruby' -gem 'always_verify_ssl_certificates' gem 'sass', require: nil gem 'dav4rack' diff --git a/Gemfile.lock b/Gemfile.lock index 042aaf46..837b1390 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,13 +1,5 @@ -GIT - remote: https://github.com/stripe/stripe-ruby - revision: 48f76057f425ab5c3bb147f3d71c3d36d951159f - specs: - stripe (1.11.0) - json (~> 1.8.1) - mime-types (~> 1.25) - rest-client (~> 1.4) - GEM + remote: https://code.stripe.com/ remote: https://rubygems.org/ specs: activesupport (4.1.4) @@ -18,7 +10,6 @@ GEM tzinfo (~> 1.1) addressable (2.3.6) ago (0.1.5) - always_verify_ssl_certificates (0.3.0) ansi (1.4.3) autoparse (0.3.3) addressable (>= 2.3.1) @@ -105,6 +96,7 @@ GEM metaclass (~> 0.0.1) multi_json (1.10.1) multipart-post (2.0.0) + netrc (0.7.7) nokogiri (1.6.3.1) mini_portile (= 0.6.0) pg (0.17.1) @@ -146,8 +138,9 @@ GEM redis (3.0.7) redis-namespace (1.4.1) redis (~> 3.0.4) - rest-client (1.6.7) - mime-types (>= 1.16) + rest-client (1.7.2) + mime-types (>= 1.16, < 3.0) + netrc (~> 0.7) retriable (1.4.1) rmagick (2.13.2) safe_yaml (1.0.1) @@ -185,6 +178,10 @@ GEM sinatra-xsendfile (0.4.2) sinatra (>= 0.9.1) slop (3.5.0) + stripe (1.15.0) + json (~> 1.8.1) + mime-types (>= 1.25, < 3.0) + rest-client (~> 1.4) thread_safe (0.3.4) tilt (1.4.1) timers (1.1.0) @@ -211,7 +208,6 @@ PLATFORMS DEPENDENCIES ago - always_verify_ssl_certificates bcrypt capybara_minitest_spec cocaine @@ -253,7 +249,7 @@ DEPENDENCIES sinatra sinatra-flash sinatra-xsendfile - stripe! + stripe tilt webmock zipruby diff --git a/app.rb b/app.rb index eb495777..a8d61b87 100644 --- a/app.rb +++ b/app.rb @@ -149,8 +149,67 @@ get '/tips_mockup' do erb :'tips_mockup' end -get '/stats_mockup' do - erb :'stats_mockup' +get '/stats/?' do + require_admin + + @stats = { + total_sites: Site.count, + total_unbanned_sites: Site.where(is_banned: false).count, + total_banned_sites: Site.where(is_banned: true).count, + total_nsfw_sites: Site.where(is_nsfw: true).count, + total_unbanned_nsfw_sites: Site.where(is_banned: false, is_nsfw: true).count, + total_banned_nsfw_sites: Site.where(is_banned: true, is_nsfw: true).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 = Time.now + + until runner.year == now.year && runner.month == 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 + + customers = Stripe::Customer.all + + @stats[:total_recurring_revenue] = 0.0 + + subscriptions = [] + cancelled_subscriptions = 0 + + customers.each do |customer| + sub = {created_at: Time.at(customer.created)} + if customer[:subscriptions] + if customer[:subscriptions][:data].empty? + sub[:status] = 'cancelled' + else + sub[:status] = 'active' + sub[:amount] = (customer[:subscriptions][:data].first[:plan][:amount] / 100.0).round(2) + @stats[:total_recurring_revenue] += sub[:amount] + end + end + subscriptions.push sub + end + + @stats[:subscriptions] = subscriptions + erb :'stats' end get '/?' do diff --git a/views/stats.erb b/views/stats.erb new file mode 100644 index 00000000..5655e024 --- /dev/null +++ b/views/stats.erb @@ -0,0 +1,91 @@ +
+
+

Neocities Site Statistics

+
+
+ +
+

+
+

General Stats

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Total Sites<%= @stats[:total_sites] %>
Total Unbanned Sites<%= @stats[:total_unbanned_sites] %>
Total Banned Sites<%= @stats[:total_banned_sites] %>
Total NSFW Sites<%= @stats[:total_nsfw_sites] %>
Total Unbanned NSFW Sites<%= @stats[:total_unbanned_nsfw_sites] %>
Total Banned NSFW Sites<%= @stats[:total_banned_nsfw_sites] %>
+ +

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] %>
+ +

Subscriptions

+

Current Recurring Revenue: <%= format("$%.2f", @stats[:total_recurring_revenue]) %> +
Active Subscriptions: <%= @stats[:subscriptions].select {|s| s[:status] == 'active' }.length %>

+ + + + + + + <% @stats[:subscriptions].each do |sub| %> + + + + + + <% end %> +
StatusAmountDate Subscribed
<%= sub[:status] %><%= sub[:amount].nil? ? '$0.00' : format("$%.2f", sub[:amount]) %><%= sub[:created_at].strftime('%Y %B') %>
+

+

+
+
\ No newline at end of file