From 2b1210b2a807b333695c1fa4571e605737ef3b41 Mon Sep 17 00:00:00 2001
From: Syphax bouazzouni <gs_bouazzouni@esi.dz>
Date: Mon, 1 Jan 2024 20:27:30 +0100
Subject: [PATCH] fix the object analytics fetching after the new year change
 (#19)

---
 Gemfile.lock                                  |  4 ++--
 bin/import_google_ua_analytics_data           | 12 ++++++++----
 lib/ncbo_cron/analytics/object_analytics.rb   | 19 ++++++++++++-------
 .../analytics/page_visits_analytics.rb        |  4 ++--
 4 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/Gemfile.lock b/Gemfile.lock
index c65a2e6a..30e9e614 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -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)
@@ -282,4 +282,4 @@ DEPENDENCIES
   test-unit-minitest
 
 BUNDLED WITH
-   2.3.23
+   2.3.15
diff --git a/bin/import_google_ua_analytics_data b/bin/import_google_ua_analytics_data
index eadfd936..efc216d1 100755
--- a/bin/import_google_ua_analytics_data
+++ b/bin/import_google_ua_analytics_data
@@ -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
@@ -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
diff --git a/lib/ncbo_cron/analytics/object_analytics.rb b/lib/ncbo_cron/analytics/object_analytics.rb
index c4254db7..12765d96 100644
--- a/lib/ncbo_cron/analytics/object_analytics.rb
+++ b/lib/ncbo_cron/analytics/object_analytics.rb
@@ -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])
@@ -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)
@@ -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
diff --git a/lib/ncbo_cron/analytics/page_visits_analytics.rb b/lib/ncbo_cron/analytics/page_visits_analytics.rb
index b2354c00..fd88588a 100644
--- a/lib/ncbo_cron/analytics/page_visits_analytics.rb
+++ b/lib/ncbo_cron/analytics/page_visits_analytics.rb
@@ -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