From d88b121bd02e18a70f91dd10b3d50450b2d0145d Mon Sep 17 00:00:00 2001 From: mdorf Date: Tue, 8 Aug 2023 13:07:43 -0700 Subject: [PATCH] fixed an issue with the GA4 Analytics migration --- controllers/ontology_analytics_controller.rb | 8 ++++++-- helpers/application_helper.rb | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/controllers/ontology_analytics_controller.rb b/controllers/ontology_analytics_controller.rb index 8ecd77d5..829708c1 100644 --- a/controllers/ontology_analytics_controller.rb +++ b/controllers/ontology_analytics_controller.rb @@ -28,7 +28,11 @@ class OntologyAnalyticsController < ApplicationController expires 86400, :public ont = Ontology.find(params["acronym"]).first error 404, "No ontology exists with the acronym: #{params["acronym"]}" if ont.nil? - analytics = ont.analytics + year = year_param(params) + error 400, "The year you supplied is invalid. Valid years start with 2 and contain 4 digits." if params["year"] && !year + month = month_param(params) + error 400, "The month you supplied is invalid. Valid months are 1-12." if params["month"] && !month + analytics = ont.analytics(year, month) if params["format"].to_s.downcase.eql?("csv") tf = Tempfile.new("analytics-#{params['acronym']}") @@ -39,7 +43,7 @@ class OntologyAnalyticsController < ApplicationController years.each do |year| months = analytics[params["acronym"]][year].keys.sort months.each do |month| - next if now.year == year && now.month <= month || (year == 2013 && month < 10) # we don't have good data going back past Oct 2013 + next if year && month && (now.year == year.to_i && now.month <= month.to_i || year.to_i == 2013 && month.to_i < 10) # we don't have good data going back past Oct 2013 visits = analytics[params["acronym"]][year][month] month = DateTime.parse("#{year}/#{month}").strftime("%b %Y") csv << [month, visits] diff --git a/helpers/application_helper.rb b/helpers/application_helper.rb index 18163099..2e811c32 100644 --- a/helpers/application_helper.rb +++ b/helpers/application_helper.rb @@ -261,9 +261,10 @@ def month_param(params=nil) if params["month"] month = params["month"].strip if %r{(?^(0[1-9]|[1-9]|1[0-2])$)}x === month - month.to_i + return month end end + nil end # validates year for starting with 1 or 2 and containing 4 digits @@ -272,9 +273,10 @@ def year_param(params=nil) if params["year"] year = params["year"].strip if %r{(?^([1-2]\d{3})$)}x === year - year.to_i + return year end end + nil end ##