forked from ontoportal/ontologies_linked_data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Google analytics new version and add users analytics (#118)
* 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
1 parent
e98b884
commit c6d681d
Showing
4 changed files
with
163 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |