From d05209d28fac74d6a70427b5510a8df11f3b731b Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 3 Sep 2018 17:35:14 +0200 Subject: [PATCH] Make the stats code more robust to empty databases --- app/controllers/stats_controller.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index c37ec9bad..150989e0a 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -82,7 +82,11 @@ class StatsController < ApplicationController total = procedures.count last_30_days_count = procedures.where(published_at: 1.month.ago..Time.now).count previous_count = procedures.where(published_at: 2.months.ago..1.month.ago).count - evolution = (((last_30_days_count.to_f / previous_count) - 1) * 100).round(0) + if previous_count != 0 + evolution = (((last_30_days_count.to_f / previous_count) - 1) * 100).round(0) + else + evolution = 0 + end formatted_evolution = sprintf("%+d", evolution) { @@ -96,7 +100,11 @@ class StatsController < ApplicationController total = dossiers.count last_30_days_count = dossiers.where(en_construction_at: 1.month.ago..Time.now).count previous_count = dossiers.where(en_construction_at: 2.months.ago..1.month.ago).count - evolution = (((last_30_days_count.to_f / previous_count) - 1) * 100).round(0) + if previous_count != 0 + evolution = (((last_30_days_count.to_f / previous_count) - 1) * 100).round(0) + else + evolution = 0 + end formatted_evolution = sprintf("%+d", evolution) {