Skip to content

Commit

Permalink
fix: safety sheets are not imported and significantly slow down the i…
Browse files Browse the repository at this point in the history
…mport (#1779)

* define timeout for safety data sheet requests

* modify header for safety data sheet merck request and improve product number extraction

* fix regex for extracting merck product number

* add sleep time between fetch sds merck api requests

* fix: handle empty safety phrases objects in chemicalTab component

* refactor: remove check_if_safety_sheet_already_saved function in chemicalsService class
  • Loading branch information
adambasha0 authored Mar 26, 2024
1 parent a6fea72 commit 2e63c06
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 66 deletions.
26 changes: 15 additions & 11 deletions app/packs/src/components/ChemicalTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,23 @@ export default class ChemicalTab extends React.Component {
// eslint-disable-next-line class-methods-use-this
stylePhrases = (str) => {
const HazardPhrases = [];
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(str.h_statements)) {
// eslint-disable-next-line react/jsx-one-expression-per-line
const st = <p key={key}> {key}:{value} </p>;
HazardPhrases.push(st);
if (str && str.h_statements && str.h_statements.length !== 0) {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(str.h_statements)) {
// eslint-disable-next-line react/jsx-one-expression-per-line
const st = <p key={key}> {key}:{value} </p>;
HazardPhrases.push(st);
}
}

const precautionaryPhrases = [];
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(str?.p_statements || {})) {
// eslint-disable-next-line react/jsx-one-expression-per-line
const st = <p key={key}>{key}:{value}</p>;
precautionaryPhrases.push(st);
if (str && str.p_statements && str?.p_statements?.length !== 0) {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(str?.p_statements || {})) {
// eslint-disable-next-line react/jsx-one-expression-per-line
const st = <p key={key}>{key}:{value}</p>;
precautionaryPhrases.push(st);
}
}

const pictogramsArray = str.pictograms?.map((i) => (
Expand Down Expand Up @@ -582,7 +586,7 @@ export default class ChemicalTab extends React.Component {
vendor_product: vendorProduct
};
ChemicalFetcher.saveSafetySheets(params).then((result) => {
if (result || result === 'file is already saved') {
if (result) {
const value = `/safety_sheets/${productInfo.productNumber}_${productInfo.vendor}.pdf`;
const chemicalData = chemical._chemical_data;
const pathArr = [];
Expand Down
41 changes: 17 additions & 24 deletions lib/chemotion/chemicals_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ module Chemotion
class ChemicalsService
def self.request_options
{ headers: {
'Access-Control-Request-Method' => 'GET',
'Accept' => '*/*',
'User-Agent': 'Google Chrome',
} }
'Access-Control-Request-Method' => 'GET',
'Accept' => '*/*',
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0',
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'keep-alive',
},
timeout: 15,
follow_redirects: true }
end

def self.merck_request(name)
Expand Down Expand Up @@ -50,37 +54,26 @@ def self.alfa(name, language)
'Could not find safety data sheet from Thermofisher'
end

def self.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names)
saved = false
unless safety_sheet_files_names.empty?
safety_sheet_files_names.each do |file|
if file == file_name
saved = true
break
end
end
end
saved
end

def self.write_file(file_path, link)
req_safety_sheet = HTTParty.get(link, request_options)
options = request_options.dup
options[:headers]['Origin'] = 'https://www.sigmaaldrich.com'
req_safety_sheet = HTTParty.get(link, options)
file_name = "public/safety_sheets/#{file_path}"
if req_safety_sheet.headers['Content-Type'] == 'application/pdf'
File.binwrite(file_name, req_safety_sheet)
true
else
'there is no file to save'
end
rescue HTTParty::RedirectionTooDeep => e
"Redirection limit exceeded: #{e}"
rescue HTTParty::TimeoutError => e
"Request timed out: #{e}"
end

def self.create_sds_file(file_path, link)
safety_sheet_files_names = Dir.children('public/safety_sheets')
if check_if_safety_sheet_already_saved(file_path, safety_sheet_files_names) == false
write_file(file_path, link)
else
'file is already saved'
end
write_file(file_path, link)
sleep 1
rescue StandardError
'could not save safety data sheet'
end
Expand Down
6 changes: 4 additions & 2 deletions lib/import/import_chemicals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def self.extract_product_number(url)
match[1]
else
path = url.split('/')
path.last
filter = path.last
# extract digits from the string
filter.match(/.*?(?=[$%&?]|$)/)&.[](0)
end
end

Expand All @@ -115,7 +117,7 @@ def self.create_safety_sheet_path(vendor, value, product_number, chemical)
chemical['chemical_data'][0]['safetySheetPath'] ||= []
sheet_path = { "#{vendor}_link" => "#{SAFETY_SHEET_PATH}#{file_path}" }
is_created = Chemotion::ChemicalsService.create_sds_file(file_path, value)
result = [true, 'file is already saved'].include?(is_created)
result = [true].include?(is_created)
chemical['chemical_data'][0]['safetySheetPath'] << sheet_path if result
chemical
end
Expand Down
29 changes: 0 additions & 29 deletions spec/lib/chemotion/chemicals_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,6 @@

describe Chemotion::ChemicalsService do
describe Chemotion::ChemicalsService do
context 'when check_if_safety_sheet_already_saved is called' do
it 'returns true when file is already saved' do
file_name = 'safety_file.pdf'
safety_sheet_files_names = ['safety_file.pdf', 'other_file.pdf']
expect(described_class.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names)).to be_truthy
end

it 'returns false when file is not already saved' do
file_name = 'safety_file.pdf'
safety_sheet_files_names = ['other_file.pdf', 'another_file.pdf']
expect(described_class.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names)).to be_falsey
end

it 'returns false when safety_sheet_files_names is empty' do
file_name = 'safety_file.pdf'
safety_sheet_files_names = []
expect(described_class.check_if_safety_sheet_already_saved(file_name, safety_sheet_files_names)).to be_falsey
end
end

context 'when write_file is called' do
let(:link) { 'https://www.sigmaaldrich.com/DE/en/sds/sigald/383112' }
let(:file_path) { '252549_Merck.pdf' }
Expand Down Expand Up @@ -55,15 +35,6 @@
result = described_class.create_sds_file(file_path, link)
expect(result).to be_truthy
end

it 'returns file is already saved if already saved' do
file_path = '252549_Merck.pdf'
link = 'https://www.sigmaaldrich.com/US/en/sds/sial/252549'
allow(described_class).to receive(:check_if_safety_sheet_already_saved)
.with(file_path, anything).and_return(true)
result = described_class.create_sds_file(file_path, link)
expect(result).to eq('file is already saved')
end
end

context 'when health_section is called' do
Expand Down

0 comments on commit 2e63c06

Please sign in to comment.