Skip to content

Commit

Permalink
fix the object analytics fetching after the new year change (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Jan 1, 2024
1 parent b1f6039 commit 2b1210b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GIT

GIT
remote: https://github.com/ontoportal-lirmm/ontologies_linked_data.git
revision: e98b884999e5ce917a8be5fdc37f7b4797a1559e
revision: c6d681dcb05e3d88c6c17baff42c5581871e39ac
branch: master
specs:
ontologies_linked_data (0.0.1)
Expand Down Expand Up @@ -282,4 +282,4 @@ DEPENDENCIES
test-unit-minitest

BUNDLED WITH
2.3.23
2.3.15
12 changes: 8 additions & 4 deletions bin/import_google_ua_analytics_data
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ module NcboCron
(start_year..Date.today.year).each do |y|
aggregated_results[acronym] = Hash.new if aggregated_results[acronym].nil?
aggregated_results[acronym][y.to_s] = Hash.new unless aggregated_results[acronym].has_key?(y.to_s)
# fill up non existent months with zeros
last_month = y.eql?(Date.today.year) ? Date.today.month : 12
(1..last_month).each { |n| aggregated_results[acronym][y.to_s][n.to_s] = 0 if aggregated_results[acronym][y.to_s].is_a?(Hash) && !aggregated_results[acronym][y.to_s].has_key?(n.to_s)}
end
# fill up non existent months with zeros
(1..12).each { |n| aggregated_results[acronym].values.each { |v| v[n.to_s] = 0 unless v.has_key?(n.to_s) } }
break
end
end
Expand Down Expand Up @@ -82,9 +83,12 @@ module NcboCron
(start_year..Date.today.year).each do |y|
aggregated_results = Hash.new if aggregated_results.nil?
aggregated_results[y.to_s] = Hash.new unless aggregated_results.has_key?(y.to_s)

# fill up non existent months with zeros
last_month = y.eql?(Date.today.year) ? Date.today.month.to_i : 12
(1..last_month).each { |n| aggregated_results[y.to_s][n.to_s] = 0 if aggregated_results[y.to_s].is_a?(Hash) && !aggregated_results[y.to_s].has_key?(n.to_s)}
end
# fill up non existent months with zeros
(1..12).each { |n| aggregated_results.values.each { |v| v[n.to_s] = 0 unless v.has_key?(n.to_s) } }

break
end
end
Expand Down
19 changes: 12 additions & 7 deletions lib/ncbo_cron/analytics/object_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def merge_and_fill_missing_data(new_data, old_data,logger, start_date = @start_d
year = year.to_s
if new_data[acronym].has_key?(year)
if old_data[acronym].has_key?(year)
(1..Date.today.month).each do |month|
last_month = year.eql?(Date.today.year.to_s) ? Date.today.month : 12
(1..last_month).each do |month|
month = month.to_s
old_data[acronym][year][month] ||= 0
unless old_data[acronym][year][month].eql?(new_data[acronym][year][month])
Expand All @@ -144,9 +145,7 @@ def merge_and_fill_missing_data(new_data, old_data,logger, start_date = @start_d
logger.info "Filling in missing years data..."
old_data = fill_missing_data(old_data)
end

# sort_ga_data(old_data)
old_data
sort_ga_data(old_data)
end

def aggregate_results(aggregated_results, results)
Expand Down Expand Up @@ -179,16 +178,22 @@ def fill_missing_data(ga_data)
(start_year..Date.today.year).each do |y|
ga_data[acronym] = Hash.new if ga_data[acronym].nil?
ga_data[acronym][y.to_s] = Hash.new unless ga_data[acronym].has_key?(y.to_s)

# fill up non existent months with zeros
last_month = y.eql?(Date.today.year) ? Date.today.month.to_i : 12
(1..last_month).each { |n| ga_data[acronym][y.to_s][n.to_s] = 0 if ga_data[acronym][y.to_s].is_a?(Hash) && !ga_data[acronym][y.to_s].has_key?(n.to_s) }
end
# fill up non existent months with zeros
(1..12).each { |n| ga_data[acronym].values.each { |v| v[n.to_s] = 0 if v.is_a?(Hash) && !v.has_key?(n.to_s) } }
end
end

def sort_ga_data(ga_data)
ga_data.transform_values { |value|
value.transform_values { |val|
val.sort_by { |key, _| key.to_i }.to_h
if val.is_a?(Hash)
val.sort_by { |key, _| key.to_i }.to_h
else
val
end
}.sort_by { |k, _| k.to_i }.to_h
}.sort.to_h
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ncbo_cron/analytics/page_visits_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
module NcboCron
module Models
class PageVisitsAnalytics < ObjectAnalytics
def initialize(start_date: Date.today.prev_month, old_data: {})
super(redis_field: 'pages_analytics', start_date: Date.today.prev_month, old_data: { })
def initialize(start_date: Date.today.prev_month.to_s, old_data: {})
super(redis_field: 'pages_analytics', start_date: Date.today.prev_month.to_s, old_data: { })
end

private
Expand Down

0 comments on commit 2b1210b

Please sign in to comment.