Skip to content

Commit

Permalink
Fix: Google analytics new version and add users analytics (#118)
Browse files Browse the repository at this point in the history
* add ontologies and users analytics tests

* extract analytics concern to a file that load and parse data from redis

* add users visits per month and pages visited in the previous month stats
  • Loading branch information
syphax-bouazzouni authored Dec 28, 2023
1 parent e98b884 commit c6d681d
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 21 deletions.
52 changes: 52 additions & 0 deletions lib/ontologies_linked_data/concerns/analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module LinkedData
module Concerns
module Analytics
def self.included base
base.extend ClassMethods
end

module ClassMethods
def load_data(field_name)
@@redis ||= Redis.new(:host => LinkedData.settings.ontology_analytics_redis_host,
:port => LinkedData.settings.ontology_analytics_redis_port,
:timeout => 30)
raw_data = @@redis.get(field_name)
raw_data.nil? ? Hash.new : Marshal.load(raw_data)
end

def analytics_redis_key
raise NotImplementedError # the class that includes it need to implement it
end

def load_analytics_data
self.load_data(analytics_redis_key)
end

def analytics(year = nil, month = nil)
retrieve_analytics(year, month)
end

# A static method for retrieving Analytics for a combination of ontologies, year, month
def retrieve_analytics(year = nil, month = nil)
analytics = self.load_analytics_data

year = year.to_s if year
month = month.to_s if month

unless analytics.empty?
analytics.values.each do |ont_analytics|
ont_analytics.delete_if { |key, _| key != year } unless year.nil?
ont_analytics.each { |_, val| val.delete_if { |key, __| key != month } } unless month.nil?
end
# sort results by the highest traffic values
analytics = Hash[analytics.sort_by { |_, v| v[year][month] }.reverse] if year && month
end
analytics
end
end

end
end
end


26 changes: 5 additions & 21 deletions lib/ontologies_linked_data/models/ontology.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Models
class Ontology < LinkedData::Models::Base
class ParsedSubmissionError < StandardError; end
class OntologyAnalyticsError < StandardError; end
include LinkedData::Concerns::Analytics

ONTOLOGY_ANALYTICS_REDIS_FIELD = "ontology_analytics"
ONTOLOGY_RANK_REDIS_FIELD = "ontology_rank"
Expand Down Expand Up @@ -334,17 +335,8 @@ def rank(weight_analytics=DEFAULT_RANK_WEIGHT_ANALYTICS, weight_umls=DEFAULT_RAN

# A static method for retrieving Analytics for a combination of ontologies, year, month
def self.analytics(year=nil, month=nil, acronyms=nil)
analytics = self.load_analytics_data

unless analytics.empty?
analytics.delete_if { |acronym, _| !acronyms.include? acronym } unless acronyms.nil?
analytics.values.each do |ont_analytics|
ont_analytics.delete_if { |key, _| key != year } unless year.nil?
ont_analytics.each { |_, val| val.delete_if { |key, __| key != month } } unless month.nil?
end
# sort results by the highest traffic values
analytics = Hash[analytics.sort_by {|_, v| v[year][month]}.reverse] if year && month
end
analytics = retrieve_analytics(year, month)
analytics.delete_if { |acronym, _| !acronyms.include? acronym } unless acronyms.nil?
analytics
end

Expand All @@ -361,22 +353,14 @@ def self.rank(weight_analytics=DEFAULT_RANK_WEIGHT_ANALYTICS, weight_umls=DEFAUL
ranking
end

def self.load_analytics_data
self.load_data(ONTOLOGY_ANALYTICS_REDIS_FIELD)
def self.analytics_redis_key
ONTOLOGY_ANALYTICS_REDIS_FIELD
end

def self.load_ranking_data
self.load_data(ONTOLOGY_RANK_REDIS_FIELD)
end

def self.load_data(field_name)
@@redis ||= Redis.new(:host => LinkedData.settings.ontology_analytics_redis_host,
:port => LinkedData.settings.ontology_analytics_redis_port,
:timeout => 30)
raw_data = @@redis.get(field_name)
return raw_data.nil? ? Hash.new : Marshal.load(raw_data)
end

##
# Delete all artifacts of an ontology
def delete(*args)
Expand Down
11 changes: 11 additions & 0 deletions lib/ontologies_linked_data/models/users/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class User < LinkedData::Models::Base
include BCrypt
include LinkedData::Models::Users::Authentication
include LinkedData::Models::Users::OAuthAuthentication
include LinkedData::Concerns::Analytics

ANALYTICS_REDIS_FIELD = "user_analytics"
PAGES_ANALYTICS_REDIS_FIELD = "pages_analytics"

attr_accessor :show_apikey

Expand Down Expand Up @@ -108,6 +112,13 @@ def to_s
self.username.to_s
end
end
def self.analytics_redis_key
ANALYTICS_REDIS_FIELD
end

def self.page_visits_analytics
load_data(PAGES_ANALYTICS_REDIS_FIELD)
end

private

Expand Down
95 changes: 95 additions & 0 deletions test/models/test_analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require_relative "../test_case"

class LinkedData::Models::User
@@user_analytics = {}

def self.update_class_variable(new_value)
@@user_analytics = new_value
end
def self.load_data(field_name)
@@user_analytics
end
end

class LinkedData::Models::Ontology
def self.load_analytics_data
ontologies_analytics = {}
acronyms = %w[E-PHY AGROVOC TEST]
acronyms.each do |acronym|
ontologies_analytics[acronym] = {
"2021" => (1..12).map { |i| [i.to_s, i * 2021] }.to_h,
"2022" => (1..12).map { |i| [i.to_s, i * 2022] }.to_h,
"2023" => (1..12).map { |i| [i.to_s, i * 2023] }.to_h,
}
end
ontologies_analytics
end
end

class TestAnalytics < LinkedData::TestCase

def test_ontologies_analytics
ontologies_analytics = LinkedData::Models::Ontology.load_analytics_data
analytics = LinkedData::Models::Ontology.analytics
assert_equal ontologies_analytics, analytics


month_analytics = LinkedData::Models::Ontology.analytics(2023, 1)
refute_empty month_analytics
month_analytics.each do |_, month_analytic|
exp = { "2023" => { "1" => 2023 } }
assert_equal exp, month_analytic
end

analytics = LinkedData::Models::Ontology.analytics(nil, nil, 'TEST')
exp = { "TEST" => ontologies_analytics["TEST"] }
assert_equal exp, analytics


month_analytics = LinkedData::Models::Ontology.analytics(2021, 2, 'TEST')
refute_empty month_analytics
month_analytics.each do |_, month_analytic|
exp = { "2021" => { "2" => 2 * 2021 } }
assert_equal exp, month_analytic
end
end

def test_user_analytics

user_analytics = { 'all_users' => {
"2021" => (1..12).map { |i| [i.to_s, i * 2021] }.to_h,
"2022" => (1..12).map { |i| [i.to_s, i * 2022] }.to_h,
"2023" => (1..12).map { |i| [i.to_s, i * 2023] }.to_h,
} }
LinkedData::Models::User.update_class_variable(user_analytics)


analytics = LinkedData::Models::User.analytics
assert_equal user_analytics, analytics

month_analytics = LinkedData::Models::User.analytics(2023, 1)
refute_empty month_analytics
month_analytics.each do |_, month_analytic|
exp = { "2023" => { "1" => 2023 } }
assert_equal exp, month_analytic
end
end

def test_page_visits_analytics
user_analytics = { 'all_pages' => { "/annotator" => 229,
"/mappings" => 253,
"/login" => 258,
"/ontologies/CSOPRA" => 273,
"/admin" => 280,
"/search" => 416,
"/" => 4566 }
}

LinkedData::Models::User.update_class_variable(user_analytics)

analytics = LinkedData::Models::User.page_visits_analytics
assert_equal user_analytics, analytics

end

end

0 comments on commit c6d681d

Please sign in to comment.