Skip to content

Commit

Permalink
update the special case of analytics to handle federated calls
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Sep 1, 2024
1 parent 1161610 commit f651f9f
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/ontologies_api_client/analytics.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -10,26 +13,40 @@ 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

private

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

Expand Down

0 comments on commit f651f9f

Please sign in to comment.