diff --git a/models/site.rb b/models/site.rb index bedfccbb..c4e20485 100644 --- a/models/site.rb +++ b/models/site.rb @@ -1932,9 +1932,21 @@ class Site < Sequel::Model end def monthly_bandwidth_used - stat = stats_dataset.order(:created_at.desc).select(:bandwidth).first - return 0 if stat.nil? - stat[:bandwidth] + current_month_start = Date.new(Date.today.year, Date.today.month, 1) + next_month_start = current_month_start.next_month + + result = stats_dataset + .where(created_at: current_month_start...next_month_start) + .sum(:bandwidth) + + return result || 0 + end + + def bandwidth_percentage_used + used = monthly_bandwidth_used + max = maximum_monthly_bandwidth + return 0 if max == 0 || used == 0 + ((used.to_f / max) * 100).round(1) end private diff --git a/views/dashboard/index.erb b/views/dashboard/index.erb index 8cb79860..7772cf30 100644 --- a/views/dashboard/index.erb +++ b/views/dashboard/index.erb @@ -39,7 +39,7 @@
  • Using <%= current_site.space_percentage_used %>% (<%= current_site.total_space_used.to_space_pretty %>) of your <%= current_site.maximum_space.to_space_pretty %> storage.
  • - Used <%= current_site.monthly_bandwidth_used.to_bytes_pretty %> of bandwidth this month. + Used <%= current_site.bandwidth_percentage_used %>% (<%= current_site.monthly_bandwidth_used.to_bytes_pretty %>) of your <%= current_site.maximum_monthly_bandwidth.to_bytes_pretty %> bandwidth this month.
    <% unless current_site.is_education || current_site.supporter? %>Need more space? Become a Supporter!<% end %>