mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-27 02:04:33 +08:00
Add rake task for creation and syncing structure of BMT inventory [SCI-6010]
This commit is contained in:
parent
ade1385392
commit
3cab5751e7
4 changed files with 69 additions and 13 deletions
|
@ -56,14 +56,16 @@ class BioEddieAssetsController < ApplicationController
|
|||
end
|
||||
|
||||
def bmt_request
|
||||
return render_404 unless ENV['BIOMOLECULE_TOOLKIT_BASE_URL']
|
||||
return render_404 unless Rails.application.config.x.biomolecule_toolkit_base_url
|
||||
|
||||
uri = URI.parse(ENV['BIOMOLECULE_TOOLKIT_BASE_URL'])
|
||||
uri = URI.parse(Rails.application.config.x.biomolecule_toolkit_base_url)
|
||||
uri.path = request.original_fullpath.remove('/biomolecule_toolkit')
|
||||
|
||||
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
||||
api_request = "Net::HTTP::#{request.request_method.capitalize}".constantize.new(uri)
|
||||
api_request['x-api-key'] = ENV['BIOMOLECULE_TOOLKIT_API_KEY'] if ENV['BIOMOLECULE_TOOLKIT_API_KEY']
|
||||
if Rails.application.config.x.biomolecule_toolkit_api_key
|
||||
api_request['x-api-key'] = if Rails.application.config.x.biomolecule_toolkit_api_key
|
||||
end
|
||||
api_request['Content-Type'] = 'application/json'
|
||||
request_body = request.body.read
|
||||
|
||||
|
|
|
@ -2,15 +2,14 @@
|
|||
|
||||
class BiomoleculeToolkitClient
|
||||
MACROMOLECULES_PATH = '/api/macromolecules'
|
||||
MACROMOLECULES_ATTRIBUTES_PATH = '/api/admin/attributes/MACROMOLECULE'
|
||||
|
||||
class BiomoleculeToolkitClientException < StandardError; end
|
||||
|
||||
def initialize
|
||||
@host = Rails.configuration.x.biomolecule_toolkit_host
|
||||
@http = Net::HTTP.new(
|
||||
Rails.configuration.x.biomolecule_toolkit_host,
|
||||
Rails.application.config.x.biomolecule_toolkit_port
|
||||
)
|
||||
uri = URI.parse(Rails.application.config.x.biomolecule_toolkit_base_url)
|
||||
@http = Net::HTTP.new(uri.host, uri.port)
|
||||
@http.use_ssl = (uri.scheme == 'https')
|
||||
end
|
||||
|
||||
def healthy?
|
||||
|
@ -18,13 +17,18 @@ class BiomoleculeToolkitClient
|
|||
process_request(request)&.dig('status') == 'UP'
|
||||
end
|
||||
|
||||
def list_attributes
|
||||
request = Net::HTTP::Get.new(MACROMOLECULES_ATTRIBUTES_PATH)
|
||||
process_request(request)
|
||||
end
|
||||
|
||||
def list
|
||||
request = Net::HTTP::Get.new(MACROMOLECULES_PATH)
|
||||
process_request(request)
|
||||
end
|
||||
|
||||
def create(params:)
|
||||
request = Net::HTTP::Post.new(MACROMOLECULES_PATH, 'Content-Type': 'application/json')
|
||||
request = Net::HTTP::Post.new(MACROMOLECULES_PATH)
|
||||
request.body = params
|
||||
process_request(request)
|
||||
end
|
||||
|
@ -35,7 +39,7 @@ class BiomoleculeToolkitClient
|
|||
end
|
||||
|
||||
def update(cid:, params:)
|
||||
request = Net::HTTP::Put.new("#{MACROMOLECULES_PATH}/#{CGI.escape(cid)}", 'Content-Type': 'application/json')
|
||||
request = Net::HTTP::Put.new("#{MACROMOLECULES_PATH}/#{CGI.escape(cid)}")
|
||||
request.body = params
|
||||
process_request(request)
|
||||
end
|
||||
|
@ -48,9 +52,13 @@ class BiomoleculeToolkitClient
|
|||
private
|
||||
|
||||
def process_request(request)
|
||||
if Rails.application.config.x.biomolecule_toolkit_api_key
|
||||
request['x-api-key'] = Rails.application.config.x.biomolecule_toolkit_api_key
|
||||
end
|
||||
request['Content-Type'] = 'application/json'
|
||||
response = @http.request(request)
|
||||
|
||||
case response.class
|
||||
case response
|
||||
when Net::HTTPOK
|
||||
JSON.parse(response.body)
|
||||
when Net::HTTPNoContent
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
Rails.application.config.x.biomolecule_toolkit_host = ENV['BIOMOLECULE_TOOLKIT_HOST']
|
||||
Rails.application.config.x.biomolecule_toolkit_port = ENV['BIOMOLECULE_TOOLKIT_PORT'] || 80
|
||||
Rails.application.config.x.biomolecule_toolkit_base_url = ENV['BIOMOLECULE_TOOLKIT_BASE_URL']
|
||||
Rails.application.config.x.biomolecule_toolkit_api_key = ENV['BIOMOLECULE_TOOLKIT_API_KEY']
|
||||
|
|
46
lib/tasks/biomolecule_toolkit.rake
Normal file
46
lib/tasks/biomolecule_toolkit.rake
Normal file
|
@ -0,0 +1,46 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
namespace :biomolecule_toolkit do
|
||||
desc 'Creates new BMT inventory and maps BMT macromolecules attributes to its columns'
|
||||
task :init_repository, [:team_id] => :environment do |_, args|
|
||||
raise StandardError, 'BMT repository already exists!' if BmtRepository.any?
|
||||
|
||||
bmt_client = BiomoleculeToolkitClient.new
|
||||
attributes = bmt_client.list_attributes
|
||||
|
||||
team = Team.find(args[:team_id])
|
||||
BmtRepository.transaction do
|
||||
repository = BmtRepository.create!(name: 'Molecules', team: team, created_by: team.created_by)
|
||||
attributes.each do |attribute|
|
||||
repository.repository_columns.create!(name: attribute['name'],
|
||||
data_type: 'RepositoryTextValue',
|
||||
created_by: team.created_by)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Syncs BMT inventory columns with BMT macromolecules attributes'
|
||||
task sync_repository: :environment do
|
||||
raise StandardError, 'BMT repository does not exist!' if BmtRepository.none?
|
||||
|
||||
bmt_client = BiomoleculeToolkitClient.new
|
||||
attributes = bmt_client.list_attributes
|
||||
|
||||
BmtRepository.transaction do
|
||||
repository = BmtRepository.take
|
||||
attributes.each do |attribute|
|
||||
next if repository.repository_columns.find_by(name: attribute['name']).present?
|
||||
|
||||
repository.repository_columns.create!(name: attribute['name'],
|
||||
data_type: 'RepositoryTextValue',
|
||||
created_by: repository.created_by)
|
||||
end
|
||||
|
||||
repository.repository_columns.each do |repository_column|
|
||||
next if attributes.pluck('name').include?(repository_column.name)
|
||||
|
||||
repository_column.destroy!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue