From e341c63c6da3b98f795a2f04440ff74b00099b5b Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Wed, 27 Dec 2023 23:36:07 +0100 Subject: [PATCH] add users and page_visits analytics endpoints --- controllers/analytics_controller.rb | 45 +++++++++++++++++++++++++++++ helpers/application_helper.rb | 4 +-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 controllers/analytics_controller.rb diff --git a/controllers/analytics_controller.rb b/controllers/analytics_controller.rb new file mode 100644 index 00000000..1fe6d561 --- /dev/null +++ b/controllers/analytics_controller.rb @@ -0,0 +1,45 @@ +require 'csv' + +class OntologyAnalyticsController < ApplicationController + + ## + # get all ontology analytics for a given year/month combination + # TODO use a namespace analytics after migration the old OntologyAnalyticsController + namespace "/data/analytics" do + + get 'ontologies' do + expires 86400, :public + 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 + acronyms = restricted_ontologies_to_acronyms(params) + analytics = Ontology.analytics(year, month, acronyms) + + reply analytics + end + + + get 'users' do + expires 86400, :public + 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 = User.analytics(year, month) + reply analytics['all_users'] + end + + get 'page_visits' do + expires 86400, :public + 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 = User.page_visits_analytics + reply analytics['all_pages'] + end + + end + +end diff --git a/helpers/application_helper.rb b/helpers/application_helper.rb index d90630a3..24893eef 100644 --- a/helpers/application_helper.rb +++ b/helpers/application_helper.rb @@ -276,7 +276,7 @@ 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 + month.to_i.to_s end end end @@ -287,7 +287,7 @@ def year_param(params=nil) if params["year"] year = params["year"].strip if %r{(?^([1-2]\d{3})$)}x === year - year.to_i + year.to_i.to_s end end end