Skip to content

Commit

Permalink
Fix: optimization for artefacts model (#198)
Browse files Browse the repository at this point in the history
* refactor artefact and attribute_fetcher for optimization

* fix tests
  • Loading branch information
imadbourouche authored Mar 5, 2025
1 parent b321d73 commit a9e695a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,50 @@ module Concerns
module SemanticArtefact
module AttributeFetcher
def bring(*attributes)
attributes = [attributes] unless attributes.is_a?(Array)

attributes.each do |attr|

attributes.flatten!

grouped_attributes = attributes.each_with_object(Hash.new { |h, k| h[k] = {} }) do |attr, hash|
mapping = self.class.attribute_mappings[attr]
next if mapping.nil?

next unless mapping
model = mapping[:model]
mapped_attr = mapping[:attribute]

case model
when :ontology
fetch_from_ontology(attr, mapped_attr)
when :ontology_submission
fetch_from_submission(attr, mapped_attr)
when :metric
fetch_from_metrics(attr, mapped_attr)
end
hash[model][attr] = mapped_attr
end

fetch_from_ontology(grouped_attributes[:ontology]) if grouped_attributes[:ontology].any?
fetch_from_submission(grouped_attributes[:ontology_submission]) if grouped_attributes[:ontology_submission].any?
fetch_from_metrics(grouped_attributes[:metric]) if grouped_attributes[:metric].any?
end

private

def fetch_from_ontology(attr, mapped_attr)
@ontology.bring(*mapped_attr)
self.send("#{attr}=", @ontology.send(mapped_attr)) if @ontology.respond_to?(mapped_attr)
def fetch_from_ontology(attributes)
return if attributes.empty?
@ontology.bring(*attributes.values)
attributes.each do |attr, mapped_attr|
self.send("#{attr}=", @ontology.send(mapped_attr)) if @ontology.respond_to?(mapped_attr)
end
end

def fetch_from_submission(attr, mapped_attr)
latest = defined?(@ontology) ? @ontology.latest_submission(status: :ready) : @submission
return unless latest
latest.bring(*mapped_attr)
self.send("#{attr}=", latest.send(mapped_attr)) if latest.respond_to?(mapped_attr)
def fetch_from_submission(attributes)
return if attributes.empty?
@latest ||= defined?(@ontology) ? @ontology.latest_submission(status: :ready) : @submission
return unless @latest
@latest.bring(*attributes.values)
attributes.each do |attr, mapped_attr|
self.send("#{attr}=", @latest.send(mapped_attr)) if @latest.respond_to?(mapped_attr)
end
end

def fetch_from_metrics(attr, mapped_attr)
latest = defined?(@ontology) ? @ontology.latest_submission(status: :ready) : @submission
return unless latest
latest.bring(metrics: [mapped_attr])
metrics = latest.metrics
metric_value = metrics&.respond_to?(mapped_attr) ? metrics.send(mapped_attr) || 0 : 0
self.send("#{attr}=", metric_value)
def fetch_from_metrics(attributes)
return if attributes.empty?
@latest ||= defined?(@ontology) ? @ontology.latest_submission(status: :ready) : @submission
return unless @latest
@latest.bring(metrics: [attributes.values])
attributes.each do |attr, mapped_attr|
metric_value = @latest.metrics&.respond_to?(mapped_attr) ? @latest.metrics.send(mapped_attr) || 0 : 0
self.send("#{attr}=", metric_value)
end
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/ontologies_linked_data/models/mod/semantic_artefact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,10 @@ def self.find(artefact_id)

def self.all_artefacts(attributes, page, pagesize)
all_count = Ontology.where.count
onts = Ontology.where.include(:acronym, :viewingRestriction, :administeredBy, :acl).page(page, pagesize).page_count_set(all_count).all
onts = Ontology.where.include(:viewingRestriction, :administeredBy, :acl).page(page, pagesize).page_count_set(all_count).all
all_artefacts = onts.map do |o|
new.tap do |sa|
sa.ontology = o
sa.acronym = o.acronym
sa.bring(*attributes) if attributes
end
end
Expand Down

0 comments on commit a9e695a

Please sign in to comment.