From a9e695ad3906e5de504d42a410beee6ea81f7aae Mon Sep 17 00:00:00 2001 From: Imad Bourouche Date: Wed, 5 Mar 2025 15:52:58 +0100 Subject: [PATCH] Fix: optimization for artefacts model (#198) * refactor artefact and attribute_fetcher for optimization * fix tests --- .../semantic_artefact/attribute_fetcher.rb | 62 ++++++++++--------- .../models/mod/semantic_artefact.rb | 3 +- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/lib/ontologies_linked_data/concerns/semantic_artefact/attribute_fetcher.rb b/lib/ontologies_linked_data/concerns/semantic_artefact/attribute_fetcher.rb index 6ef9b7a6..0a6d18c4 100644 --- a/lib/ontologies_linked_data/concerns/semantic_artefact/attribute_fetcher.rb +++ b/lib/ontologies_linked_data/concerns/semantic_artefact/attribute_fetcher.rb @@ -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 diff --git a/lib/ontologies_linked_data/models/mod/semantic_artefact.rb b/lib/ontologies_linked_data/models/mod/semantic_artefact.rb index c7c4c415..d0df179d 100644 --- a/lib/ontologies_linked_data/models/mod/semantic_artefact.rb +++ b/lib/ontologies_linked_data/models/mod/semantic_artefact.rb @@ -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