From 3cab5751e7fec6be98e3d6d5b5f08d6677337f16 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Tue, 7 Sep 2021 11:39:49 +0200 Subject: [PATCH] Add rake task for creation and syncing structure of BMT inventory [SCI-6010] --- .../bio_eddie_assets_controller.rb | 8 ++-- app/services/biomolecule_toolkit_client.rb | 24 ++++++---- .../biomolecule_toolkit_client.rb | 4 +- lib/tasks/biomolecule_toolkit.rake | 46 +++++++++++++++++++ 4 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 lib/tasks/biomolecule_toolkit.rake diff --git a/app/controllers/bio_eddie_assets_controller.rb b/app/controllers/bio_eddie_assets_controller.rb index 1e2c9b1ed..591e678b3 100644 --- a/app/controllers/bio_eddie_assets_controller.rb +++ b/app/controllers/bio_eddie_assets_controller.rb @@ -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 diff --git a/app/services/biomolecule_toolkit_client.rb b/app/services/biomolecule_toolkit_client.rb index 2909c2cc2..9d859bb82 100644 --- a/app/services/biomolecule_toolkit_client.rb +++ b/app/services/biomolecule_toolkit_client.rb @@ -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 diff --git a/config/initializers/biomolecule_toolkit_client.rb b/config/initializers/biomolecule_toolkit_client.rb index 241d21088..c898ff103 100644 --- a/config/initializers/biomolecule_toolkit_client.rb +++ b/config/initializers/biomolecule_toolkit_client.rb @@ -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'] diff --git a/lib/tasks/biomolecule_toolkit.rake b/lib/tasks/biomolecule_toolkit.rake new file mode 100644 index 000000000..1743c97d6 --- /dev/null +++ b/lib/tasks/biomolecule_toolkit.rake @@ -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