diff --git a/Gemfile b/Gemfile index 99ab1cbe..60a997ef 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,7 @@ gem 'dav4rack' gem 'filesize' gem 'thread' gem 'scrypt' +gem 'rack-cache' platform :mri do gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic diff --git a/Gemfile.lock b/Gemfile.lock index 358b5caf..94876208 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -122,6 +122,8 @@ GEM puma (2.8.1) rack (>= 1.1, < 2.0) rack (1.5.2) + rack-cache (1.2) + rack (>= 0.4) rack-protection (1.5.2) rack rack-recaptcha (0.6.6) @@ -238,6 +240,7 @@ DEPENDENCIES pry pry-byebug puma + rack-cache rack-recaptcha rack-test rack_session_access diff --git a/app/stats.rb b/app/stats.rb index 0eb0d8cf..f29a7c9b 100644 --- a/app/stats.rb +++ b/app/stats.rb @@ -1,5 +1,5 @@ get '/stats/?' do - require_admin + expires 14400, :public, :must_revalidate # 4 hours @stats = { total_sites: Site.count, @@ -41,19 +41,29 @@ get '/stats/?' do @stats[:total_recurring_revenue] = 0.0 subscriptions = [] - cancelled_subscriptions = 0 + @stats[: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 + + 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] = (plan[:amount] / 100.0).round(2) + + if(plan[:interval] == 'year') + sub[:amount] = (sub[:amount] / 12).round(2) + end + + @stats[:total_recurring_revenue] += sub[:amount] + subscriptions.push sub end diff --git a/config.ru b/config.ru index bf394699..9b9aaf79 100644 --- a/config.ru +++ b/config.ru @@ -7,7 +7,14 @@ if defined?(Unicorn) use Unicorn::PrereadInput end -map('/') { run Sinatra::Application } +map('/') do + use(Rack::Cache, + verbose: false, + metastore: 'file:/tmp/neocitiesrackcache/meta', + entitystore: 'file:/tmp/neocitiesrackcache/body' + ) + run Sinatra::Application +end map '/webdav' do use Rack::Auth::Basic do |username, password| diff --git a/views/stats.erb b/views/stats.erb index d707392d..0d2421f1 100644 --- a/views/stats.erb +++ b/views/stats.erb @@ -69,7 +69,13 @@

Subscriptions

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

+
+ Active Subscriptions: <%= @stats[:subscriptions].select {|s| s[:status] == 'active' }.length %> +
+ Cancelled Subscriptions: <%= @stats[:cancelled_subscriptions] %> + + +