From f651f9f920a55492629ad08a83dde4ed65278def Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 2 Sep 2024 00:02:51 +0200 Subject: [PATCH] update the special case of analytics to handle federated calls --- lib/ontologies_api_client/analytics.rb | 35 +++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/ontologies_api_client/analytics.rb b/lib/ontologies_api_client/analytics.rb index cbb85ac..b6a78cc 100644 --- a/lib/ontologies_api_client/analytics.rb +++ b/lib/ontologies_api_client/analytics.rb @@ -1,6 +1,9 @@ +require_relative 'request_federation' + module LinkedData::Client class Analytics HTTP = LinkedData::Client::HTTP + include LinkedData::Client::RequestFederation attr_accessor :onts, :date @@ -10,18 +13,32 @@ def self.all(params = {}) def self.last_month data = self.new - data.date = last_month = DateTime.now - 1.month + last_month = DateTime.now.prev_month year_num = last_month.year month_num = last_month.month - analytics = get(:analytics, {year: year_num, month: month_num}).to_h - analytics.delete(:links) - analytics.delete(:context) + params = { year: year_num, month: month_num } + + responses = federated_get(params) do |url| + "#{url}/analytics" + end + + portals = request_portals onts = [] - analytics.keys.each do |ont| - views = analytics[ont][:"#{year_num}"][:"#{month_num}"] - onts << {ont: ont, views: views} + responses.each_with_index do |portal_views, index| + next nil if portal_views&.errors + + portal_views = portal_views.to_h + + url = portals[index].url_prefix.to_s.chomp('/') + portal_views.delete(:links) + portal_views.delete(:context) + portal_views.keys.map do |ont| + views = portal_views[ont][:"#{year_num}"][:"#{month_num}"] + onts << { ont: "#{url}/ontologies/#{ont}", views: views } + end end - data.onts = onts + + data.onts = onts.flatten.compact data end @@ -29,7 +46,7 @@ def self.last_month def self.get(path, params = {}) path = path.to_s - path = "/"+path unless path.start_with?("/") + path = "/" + path unless path.start_with?("/") HTTP.get(path, params) end