From caa702c964ca4b5314e43d2250592551a970f7c7 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Wed, 2 Sep 2015 02:19:44 -0700 Subject: [PATCH] track bandwidth usage --- app/admin.rb | 31 +++++++++++++++++++++++++++++++ views/admin/usage.erb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 views/admin/usage.erb diff --git a/app/admin.rb b/app/admin.rb index 8e3b094a..d8b4c35d 100644 --- a/app/admin.rb +++ b/app/admin.rb @@ -15,6 +15,37 @@ post '/admin/reports' do end +get '/admin/usage' do + require_admin + today = Date.today + current_month = Date.new today.year, today.month, 1 + + @monthly_stats = [] + + month = current_month + + until month.year == 2015 && month.month == 1 do + stats = DB[ + 'select sum(views) as views, sum(hits) as hits, sum(bandwidth) as bandwidth from stats where created_at >= ? and created_at < ?', + month, + month.next_month].first + + stats.keys.each do |key| + stats[key] ||= 0 + end + + stats.collect {|s| s == 0}.uniq + + unless stats.select {|k,v| v == 0}.length == stats.keys.length + @monthly_stats.push stats.merge(date: month) + end + + month = month.prev_month + end + + erb :'admin/usage' +end + get '/admin/email' do require_admin erb :'admin/email' diff --git a/views/admin/usage.erb b/views/admin/usage.erb new file mode 100644 index 00000000..629a1885 --- /dev/null +++ b/views/admin/usage.erb @@ -0,0 +1,31 @@ +
+
+

Bandwidth Stats

+

John James Cowperthwaite cares that I care about this

+
+
+ +
+
+
+ + + + + + + + + <% @monthly_stats.each do |monthly_stat| %> + + + + + + + <% end %> +
ViewsHitsBandwidthDate
<%= monthly_stat[:views].to_comma_separated %><%= monthly_stat[:hits].to_comma_separated %><%= monthly_stat[:bandwidth].to_bytes_pretty %><%= monthly_stat[:date].strftime('%Y %B') %>
+
+
+ +