forked from ontoportal/ontologies_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Federation search endpoint #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Imene-Amirat
wants to merge
6
commits into
development
Choose a base branch
from
federation-search-endpoint
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8db3d5b
Add federation search endpoint
Imene-Amirat 95aefe2
fix fields
Imene-Amirat fe3ae4a
endpoint direct parallel calls to ontoPortal portals
Imene-Amirat a5d1498
refactor direct parallel calls : add redis caching, result deduplicat…
Imene-Amirat 8b93d4b
move logic to helper
Imene-Amirat f50909d
refactor gateway federation search: add NVS
Imene-Amirat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| require 'faraday' | ||
| require 'parallel' | ||
|
|
||
| class FederationController < ApplicationController | ||
|
|
||
| GATEWAY_URL = "https://terminology.services.base4nfdi.de/api-gateway" | ||
| GATEWAY_CONNECTION = Faraday.new(url: GATEWAY_URL) do |conn| | ||
| conn.headers['Accept'] = 'application/json' | ||
| conn.options.timeout = 30 | ||
| conn.options.open_timeout = 10 | ||
| end | ||
|
|
||
| namespace "/api/federation" do | ||
|
|
||
| get '/search' do | ||
| query = params[:query] || params[:q] | ||
|
|
||
| if query.nil? || query.strip.empty? | ||
| error 400, "You must provide a 'query' parameter to execute a search" | ||
| end | ||
|
|
||
| databases = (params[:database] || "ontoportal,nerc").split(',').map(&:strip) | ||
|
|
||
| # Appels parallèles : un par database pour éviter la limitation du Gateway | ||
| gateway_results = Parallel.map(databases, in_threads: databases.size) do |db| | ||
| fetch_gateway(query, db) | ||
| end | ||
|
|
||
| docs = [] | ||
| errors = [] | ||
|
|
||
| gateway_results.each do |result| | ||
| if result[:error] | ||
| errors << result[:error] | ||
| else | ||
| result[:items].each do |item| | ||
| if item["backend_type"] == "nerc" | ||
| docs << map_nvs_item(item) | ||
| else | ||
| docs << map_ontoportal_item(item) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| total_found = docs.size | ||
|
|
||
| page_data = paginate(docs, total_found) | ||
| page_data[:errors] = errors if errors.any? | ||
|
|
||
| content_type 'application/json' | ||
| MultiJson.dump(page_data) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| helpers do | ||
| def fetch_gateway(query, database) | ||
| response = GATEWAY_CONNECTION.get("search", { query: query, database: database }) | ||
|
|
||
| if response.success? | ||
| data = MultiJson.load(response.body) | ||
| items = if data.is_a?(Array) | ||
| data | ||
| elsif data.is_a?(Hash) && data["collection"] | ||
| data["collection"] | ||
| else | ||
| [] | ||
| end | ||
| { items: items } | ||
| else | ||
| { error: "Gateway (#{database}) returned HTTP #{response.status}" } | ||
| end | ||
| rescue => e | ||
| { error: "Gateway (#{database}): #{e.message}" } | ||
| end | ||
|
|
||
| def map_ontoportal_item(item) | ||
| acronym = item["ontology"] | ||
| source_api = item["source"] | ||
| source_name = item["source_name"] | ||
| ontology_iri = item["ontology_iri"] || "#{source_api}/ontologies/#{acronym}" | ||
| concept_id = item["@id"] || item["iri"] | ||
| encoded_id = CGI.escape(concept_id) | ||
|
|
||
| { | ||
| "prefLabel" => item["label"], | ||
| "synonym" => Array(item["synonyms"]), | ||
| "definition" => Array(item["descriptions"]), | ||
| "obsolete" => item["obsolete"] || false, | ||
| "matchType" => nil, | ||
| "ontologyType" => nil, | ||
| "hasChildren" => item["hasChildren"] || false, | ||
| "@id" => concept_id, | ||
| "@type" => item["type"] || "http://www.w3.org/2002/07/owl#Class", | ||
| "links" => { | ||
| "self" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}", | ||
| "ontology" => ontology_iri, | ||
| "children" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/children", | ||
| "parents" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/parents", | ||
| "descendants" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/descendants", | ||
| "ancestors" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/ancestors", | ||
| "instances" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/instances", | ||
| "tree" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/tree", | ||
| "notes" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/notes", | ||
| "mappings" => "#{source_api}/ontologies/#{acronym}/classes/#{encoded_id}/mappings", | ||
| "ui" => item["source_url"] || "#{source_api}/ontologies/#{acronym}?p=classes&conceptid=#{encoded_id}" | ||
| }, | ||
| "source_portal" => source_name | ||
| } | ||
| end | ||
|
|
||
| def map_nvs_item(item) | ||
| concept_id = item["@id"] || item["iri"] | ||
|
|
||
| { | ||
| "prefLabel" => item["label"], | ||
| "synonym" => Array(item["synonyms"]), | ||
| "definition" => Array(item["descriptions"]), | ||
| "obsolete" => item["obsolete"] || false, | ||
| "matchType" => nil, | ||
| "ontologyType" => nil, | ||
| "hasChildren" => item["hasChildren"] || false, | ||
| "@id" => concept_id, | ||
| "@type" => item["type"] || "http://www.w3.org/2004/02/skos/core#Concept", | ||
| "links" => { | ||
| "self" => concept_id, | ||
| "ontology" => item["ontology_iri"] || item["ontology"], | ||
| "children" => Array(item["children"]), | ||
| "parents" => [], | ||
| "descendants" => [], | ||
| "ancestors" => [], | ||
| "instances" => [], | ||
| "tree" => nil, | ||
| "notes" => [], | ||
| "mappings" => [], | ||
| "ui" => concept_id | ||
| }, | ||
| "source_portal" => "nvs" | ||
| } | ||
| end | ||
|
|
||
| def paginate(docs, total_found) | ||
| current_page = (params[:page] || 1).to_i | ||
| pagesize = (params[:pagesize] || 50).to_i | ||
| page_count = (total_found / pagesize.to_f).ceil | ||
| start_index = (current_page - 1) * pagesize | ||
| paged_docs = docs[start_index, pagesize] || [] | ||
|
|
||
| { | ||
| page: current_page, | ||
| pageCount: page_count, | ||
| totalCount: total_found, | ||
| prevPage: current_page > 1 ? current_page - 1 : nil, | ||
| nextPage: current_page < page_count ? current_page + 1 : nil, | ||
| collection: paged_docs | ||
| } | ||
| end | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| class FederationPortalsController < ApplicationController | ||
|
|
||
| namespace "/federation_portals" do | ||
|
|
||
| # search?q=water&portals=agroportal,ecoportal,nvs | ||
| get '/search' do | ||
| query = params[:query] || params[:q] | ||
| error 400, "You must provide a 'query' parameter to execute a search" if query.nil? || query.strip.empty? | ||
|
|
||
| portals = selected_portals(params) | ||
| results = federated_portal_search(portals, query, params) | ||
|
|
||
| current_page = (params[:page] || 1).to_i | ||
| pagesize = (params[:pagesize] || 50).to_i | ||
| total_count = results[:totalCount] | ||
| page_count = (total_count / pagesize.to_f).ceil | ||
|
|
||
| page_data = { | ||
| page: current_page, | ||
| pageCount: page_count, | ||
| totalCount: total_count, | ||
| prevPage: current_page > 1 ? current_page - 1 : nil, | ||
| nextPage: current_page < page_count ? current_page + 1 : nil, | ||
| collection: results[:collection] | ||
| } | ||
| page_data[:errors] = results[:errors] if results[:errors].any? | ||
|
|
||
| content_type 'application/json' | ||
| MultiJson.dump(page_data) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.