diff --git a/Gemfile b/Gemfile index adaeb43a..3cdb377e 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ gem 'sinatra-contrib', '~> 1.0' gem 'request_store' gem 'parallel' gem 'json-ld' +gem 'google-apis-core', '0.15.0' # Rack middleware diff --git a/Gemfile.lock b/Gemfile.lock index d44a5d5c..beeb83fd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,7 +57,7 @@ GIT GIT remote: https://github.com/ontoportal-lirmm/ontologies_linked_data.git - revision: c4dbcc14a3eb75343e597679ea4532ba53785b8f + revision: ca79d5a84a3b6e961118b7e1062f082e7f7b99fc branch: development specs: ontologies_linked_data (0.0.1) @@ -114,6 +114,7 @@ GEM base64 (0.2.0) bcrypt (3.1.20) bcrypt_pbkdf (1.1.1) + bcrypt_pbkdf (1.1.1-x86_64-darwin) bigdecimal (1.4.2) builder (3.3.0) capistrano (3.19.1) @@ -197,7 +198,7 @@ GEM google-cloud-env (2.1.1) faraday (>= 1.0, < 3.a) google-cloud-errors (1.4.0) - google-protobuf (3.25.3-x86_64-linux) + google-protobuf (3.25.4) googleapis-common-protos (1.6.0) google-protobuf (>= 3.18, < 5.a) googleapis-common-protos-types (~> 1.7) @@ -211,13 +212,10 @@ GEM multi_json (~> 1.11) os (>= 0.9, < 2.0) signet (>= 0.16, < 2.a) - grpc (1.65.1) + grpc (1.65.2-x86_64-darwin) google-protobuf (>= 3.25, < 5.0) googleapis-common-protos-types (~> 1.0) - grpc (1.65.1-x86_64-darwin) - google-protobuf (>= 3.25, < 5.0) - googleapis-common-protos-types (~> 1.0) - grpc (1.65.1-x86_64-linux) + grpc (1.65.2-x86_64-linux) google-protobuf (>= 3.25, < 5.0) googleapis-common-protos-types (~> 1.0) haml (5.2.2) @@ -346,7 +344,7 @@ GEM mime-types (>= 1.16, < 4.0) netrc (~> 0.8) retriable (3.1.2) - rexml (3.3.2) + rexml (3.3.4) strscan rsolr (2.6.0) builder (>= 2.1.2) @@ -411,6 +409,7 @@ GEM hashdiff (>= 0.4.0, < 2.0.0) PLATFORMS + x86_64-darwin-23 x86_64-linux DEPENDENCIES @@ -426,6 +425,7 @@ DEPENDENCIES faraday (~> 1.9) ffi goo! + google-apis-core (= 0.15.0) haml (~> 5.2.2) json-ld json-schema (~> 2.0) @@ -468,4 +468,4 @@ DEPENDENCIES webmock (~> 3.19.1) BUNDLED WITH - 2.3.15 + 2.3.23 diff --git a/controllers/agents_controller.rb b/controllers/agents_controller.rb index 6b69fbc5..0b47c0c2 100644 --- a/controllers/agents_controller.rb +++ b/controllers/agents_controller.rb @@ -22,147 +22,150 @@ class AgentsController < ApplicationController reply agents end - namespace "/agents" do - get do - check_last_modified_collection(LinkedData::Models::Agent) - query = LinkedData::Models::Agent.where - query = apply_filters(LinkedData::Models::Agent, query) - query = query.include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)) - if page? - page, size = page_params - agents = query.page(page, size).all - else - agents = query.to_a - end + %w[agents Agents].each do |namespace| + namespace "/#{namespace}" do + get do + check_last_modified_collection(LinkedData::Models::Agent) + query = LinkedData::Models::Agent.where + query = apply_filters(LinkedData::Models::Agent, query) + query = query.include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)) + if page? + page, size = page_params + agents = query.page(page, size).all + else + agents = query.to_a + end + + if includes_param.include?(:all) || includes_param.include?(:usages) + LinkedData::Models::Agent.load_agents_usages(agents) + end - if includes_param.include?(:all) || includes_param.include?(:usages) - LinkedData::Models::Agent.load_agents_usages(agents) + reply agents end - reply agents - end + # Display a single agent + get '/:id' do + check_last_modified_collection(LinkedData::Models::Agent) + id = params["id"] + agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first + error 404, "Agent #{id} not found" if agent.nil? + reply 200, agent + end - # Display a single agent - get '/:id' do - check_last_modified_collection(LinkedData::Models::Agent) - id = params["id"] - agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first - error 404, "Agent #{id} not found" if agent.nil? - reply 200, agent - end + # Create a agent with the given acronym + post do + reply 201, create_new_agent + end - # Create a agent with the given acronym - post do - reply 201, create_new_agent - end + # Create a agent with the given acronym + put '/:acronym' do + reply 201, create_new_agent + end - # Create a agent with the given acronym - put '/:acronym' do - reply 201, create_new_agent - end + # Update an existing submission of a agent + patch '/:id' do + acronym = params["id"] + agent = LinkedData::Models::Agent.find(acronym).include(LinkedData::Models::Agent.attributes).first - # Update an existing submission of a agent - patch '/:id' do - acronym = params["id"] - agent = LinkedData::Models::Agent.find(acronym).include(LinkedData::Models::Agent.attributes).first + if agent.nil? + error 400, "Agent does not exist, please create using HTTP PUT before modifying" + else + agent = update_agent(agent, params) - if agent.nil? - error 400, "Agent does not exist, please create using HTTP PUT before modifying" - else - agent = update_agent(agent, params) + error 400, agent.errors unless agent.errors.empty? + end + halt 204 + end - error 400, agent.errors unless agent.errors.empty? + # Delete a agent + delete '/:id' do + agent = LinkedData::Models::Agent.find(params["id"]).first + agent.delete + halt 204 end - halt 204 - end - # Delete a agent - delete '/:id' do - agent = LinkedData::Models::Agent.find(params["id"]).first - agent.delete - halt 204 - end + private - private + def update_identifiers(identifiers) + Array(identifiers).map do |i| + next nil if i.empty? - def update_identifiers(identifiers) - Array(identifiers).map do |i| - next nil if i.empty? + id = i["id"] || LinkedData::Models::AgentIdentifier.generate_identifier(i['notation'], i['schemaAgency']) + identifier = LinkedData::Models::AgentIdentifier.find(RDF::URI.new(id)).first - id = i["id"] || LinkedData::Models::AgentIdentifier.generate_identifier(i['notation'], i['schemaAgency']) - identifier = LinkedData::Models::AgentIdentifier.find(RDF::URI.new(id)).first + if identifier + identifier.bring_remaining + else + identifier = LinkedData::Models::AgentIdentifier.new + end - if identifier - identifier.bring_remaining - else - identifier = LinkedData::Models::AgentIdentifier.new - end + i.delete "id" - i.delete "id" + next identifier if i.keys.size.zero? - next identifier if i.keys.size.zero? + populate_from_params(identifier, i) - populate_from_params(identifier, i) + if identifier.valid? + identifier.save + else + error 400, identifier.errors + end + identifier + end.compact + end - if identifier.valid? - identifier.save - else - error 400, identifier.errors - end - identifier - end.compact - end + def update_affiliations(affiliations) + Array(affiliations).map do |aff| + affiliation = aff["id"] ? LinkedData::Models::Agent.find(RDF::URI.new(aff["id"])).first : nil - def update_affiliations(affiliations) - Array(affiliations).map do |aff| - affiliation = aff["id"] ? LinkedData::Models::Agent.find(RDF::URI.new(aff["id"])).first : nil + if affiliation + affiliation.bring_remaining + affiliation.identifiers.each{|i| i.bring_remaining} + end - if affiliation - affiliation.bring_remaining - affiliation.identifiers.each{|i| i.bring_remaining} - end + next affiliation if aff.keys.size.eql?(1) && aff["id"] - next affiliation if aff.keys.size.eql?(1) && aff["id"] + if affiliation + affiliation = update_agent(affiliation, aff) + else + affiliation = create_new_agent(aff["id"], aff) + end - if affiliation - affiliation = update_agent(affiliation, aff) - else - affiliation = create_new_agent(aff["id"], aff) + error 400, affiliation.errors unless affiliation.errors.empty? + + affiliation end + end - error 400, affiliation.errors unless affiliation.errors.empty? + def create_new_agent (id = @params['id'], params = @params) + agent = nil + agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first if id - affiliation + if agent.nil? + agent = update_agent(LinkedData::Models::Agent.new, params) + error 400, agent.errors unless agent.errors.empty? + + return agent + else + error 400, "Agent exists, please use HTTP PATCH to update" + end end - end - def create_new_agent (id = @params['id'], params = @params) - agent = nil - agent = LinkedData::Models::Agent.find(id).include(LinkedData::Models::Agent.goo_attrs_to_load(includes_param)).first if id + def update_agent(agent, params) + return agent unless agent - if agent.nil? - agent = update_agent(LinkedData::Models::Agent.new, params) - error 400, agent.errors unless agent.errors.empty? + identifiers = params.delete "identifiers" + affiliations = params.delete "affiliations" + params.delete "id" + populate_from_params(agent, params) + agent.identifiers = update_identifiers(identifiers) + agent.affiliations = update_affiliations(affiliations) + agent.save if agent.valid? return agent - else - error 400, "Agent exists, please use HTTP PATCH to update" end - end - def update_agent(agent, params) - return agent unless agent - - identifiers = params.delete "identifiers" - affiliations = params.delete "affiliations" - params.delete "id" - populate_from_params(agent, params) - agent.identifiers = update_identifiers(identifiers) - agent.affiliations = update_affiliations(affiliations) - - agent.save if agent.valid? - return agent end - end + end diff --git a/controllers/home_controller.rb b/controllers/home_controller.rb index c2a67fb4..a44fd22e 100644 --- a/controllers/home_controller.rb +++ b/controllers/home_controller.rb @@ -13,11 +13,15 @@ class HomeController < ApplicationController expires 3600, :public last_modified @@root_last_modified ||= Time.now.httpdate routes = routes_list + #TODO: delete when ccv will be on production routes.delete("/ccv") if LinkedData.settings.enable_resource_index == false routes.delete("/resource_index") end + + routes.delete('/Agents') + routes_hash = {} context = {} routes.each do |route| diff --git a/controllers/properties_controller.rb b/controllers/properties_controller.rb index 45267fe1..d32180d5 100644 --- a/controllers/properties_controller.rb +++ b/controllers/properties_controller.rb @@ -24,9 +24,7 @@ class PropertiesController < ApplicationController get '/:property' do prop = params[:property] ont, submission = get_ontology_and_submission - bring_unmapped = bring_unmapped?(includes_param) - - p = ont.property(prop, submission, display_all_attributes: bring_unmapped) + p = ont.property(prop, submission, display_all_attributes: false) error 404, "Property #{prop} not found in ontology #{ont.id.to_s}" if p.nil? reply 200, p end @@ -53,7 +51,7 @@ class PropertiesController < ApplicationController get '/:property/tree' do prop = params[:property] ont, submission = get_ontology_and_submission - p = ont.property(prop, submission) + p = ont.property(prop, submission, display_all_attributes: false) error 404, "Property #{prop} not found in ontology #{ont.id.to_s}" if p.nil? root_tree = p.tree @@ -81,7 +79,7 @@ class PropertiesController < ApplicationController get '/:property/ancestors' do prop = params[:property] ont, submission = get_ontology_and_submission - p = ont.property(prop, submission) + p = ont.property(prop, submission, display_all_attributes: false) error 404, "Property #{prop} not found in ontology #{ont.id.to_s}" if p.nil? ancestors = p.ancestors p.class.in(submission).models(ancestors).include(:label, :definition).all @@ -93,7 +91,7 @@ class PropertiesController < ApplicationController get '/:property/descendants' do prop = params[:property] ont, submission = get_ontology_and_submission - p = ont.property(prop, submission) + p = ont.property(prop, submission, display_all_attributes: false) error 404, "Property #{prop} not found in ontology #{ont.id.to_s}" if p.nil? descendants = p.descendants p.class.in(submission).models(descendants).include(:label, :definition).all @@ -105,7 +103,7 @@ class PropertiesController < ApplicationController get '/:property/parents' do prop = params[:property] ont, submission = get_ontology_and_submission - p = ont.property(prop, submission) + p = ont.property(prop, submission, display_all_attributes: false) error 404, "Property #{prop} not found in ontology #{ont.id.to_s}" if p.nil? p.bring(:parents) @@ -122,7 +120,7 @@ class PropertiesController < ApplicationController get '/:property/children' do prop = params[:property] ont, submission = get_ontology_and_submission - p = ont.property(prop, submission) + p = ont.property(prop, submission, display_all_attributes: false) error 404, "Property #{prop} not found in ontology #{ont.id.to_s}" if p.nil? p.bring(:children) diff --git a/controllers/search_controller.rb b/controllers/search_controller.rb index cf2d76c6..ce34d51d 100644 --- a/controllers/search_controller.rb +++ b/controllers/search_controller.rb @@ -63,7 +63,7 @@ class SearchController < ApplicationController page_size: page_size, sort: sort }) - + total_found = page_data.aggregate ontology_rank = LinkedData::Models::Ontology.rank docs = {} @@ -153,11 +153,17 @@ class SearchController < ApplicationController fq = "agentType_t:#{type}" if type - qf = [ - "acronymSuggestEdge^25 nameSuggestEdge^15 emailSuggestEdge^15 identifiersSuggestEdge^10 ", # start of the word first - "identifiers_texts^20 acronym_text^15 name_text^10 email_text^10 ", # full word match - "acronymSuggestNgram^2 nameSuggestNgram^1.5 email_text^1" # substring match last - ].join(' ') + if params[:qf] + qf = params[:qf] + else + qf = [ + "acronymSuggestEdge^25 nameSuggestEdge^15 emailSuggestEdge^15 identifiersSuggestEdge^10 ", # start of the word first + "identifiers_texts^20 acronym_text^15 name_text^10 email_text^10 ", # full word match + "acronymSuggestNgram^2 nameSuggestNgram^1.5 email_text^1" # substring match last + ].join(' ') + end + + if params[:sort] sort = "#{params[:sort]} asc, score desc" diff --git a/docker-compose.yml b/docker-compose.yml index a75136d7..07b0cda1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,7 @@ services: environment: <<: *env BUNDLE_APP_CONFIG: /srv/ontoportal/ontologies_api/.bundle + profiles: - 4store depends_on: @@ -48,7 +49,7 @@ services: - "9393:9393" volumes: # bundle volume for hosting gems installed by bundle; it speeds up gem install in local development - - app_api:/srv/ontoportal/ontologies_api + - .:/srv/ontoportal/ontologies_api - repository:/srv/ontoportal/data/repository ncbo_cron: