Skip to content

Commit 078e1f5

Browse files
phuang26cllde8
authored andcommitted
bagit upload download enhancement
1 parent f38b0ed commit 078e1f5

File tree

6 files changed

+57
-26
lines changed

6 files changed

+57
-26
lines changed

lib/labimotion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def self.logger
88
end
99

1010
def self.log_exception(exception, current_user = nil)
11-
Labimotion.logger.error("version: #{Labimotion::VERSION}; #{Labimotion::IS_RAILS5}, (#{current_user&.id}) \n Exception: #{exception.message}")
11+
Labimotion.logger.error("version: #{Labimotion::VERSION}; (#{current_user&.id}) \n Exception: #{exception.message}")
1212
Labimotion.logger.error(exception.backtrace.join("\n"))
1313
end
1414

lib/labimotion/apis/labimotion_hub_api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LabimotionHubAPI < Grape::API
1919
get do
2020
list = "Labimotion::#{params[:klass]}".constantize.where(is_active: true).where.not(released_at: nil)
2121
list = list.where(is_generic: true) if params[:klass] == 'ElementKlass'
22-
entities = Labimotion::GenericPublicEntity.represent(list, displayed: params[:with_props])
22+
entities = Labimotion::GenericPublicEntity.represent(list, displayed: params[:with_props], root: 'list')
2323
rescue StandardError => e
2424
Labimotion.log_exception(e, current_user)
2525
[]

lib/labimotion/libs/converter.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ def self.logger
2020

2121
def self.process_ds(id, current_user = {})
2222
att = Attachment.find_by(id: id, con_state: Labimotion::ConState::CONVERTED)
23-
return if att.nil? || att.attachable_id.nil? || att.attachable_type != 'Container'
23+
return if att.nil? || att.attachable_id.nil? || att.attachable_type != 'Container' || att.filename.split('.')&.last != 'zip'
24+
25+
eln_ds = Container.find_by(id: att.attachable_id, container_type: 'dataset')
26+
return if eln_ds.nil? || eln_ds.parent.nil? || eln_ds.parent&.container_type != 'analysis'
2427

2528
dsr = []
2629
ols = nil

lib/labimotion/libs/export_dataset.rb

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def description(ds, id)
4040
header_style = sheet.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
4141
sheet.add_row(['File name', res_name(id)])
4242
sheet.add_row(['Time', Time.now.strftime("%Y-%m-%d %H:%M:%S %Z")] )
43+
sheet.add_row(['(This file is automatically generated by the system.)'])
44+
sheet.add_row([''])
4345
sheet.add_row([''])
4446
sheet.add_row(['Fields description of sheet:' + ds.dataset_klass.label])
4547
sheet.add_row(['Fields', 'Field description'], style: header_style)
@@ -49,13 +51,11 @@ def description(ds, id)
4951
sheet.add_row(['Unit', 'The unit of the field'])
5052
sheet.add_row(['Name', 'The key of the field, can be used to identify the field'])
5153
sheet.add_row(['Type', 'The type of the field'])
52-
sheet.add_row(['From device?', '[Yes] if the field is from the device, [No] if the field is manually entered by the user, [SYS] if the field is automatically generated by the system'])
53-
sheet.add_row(['Device source', 'The source tag of the device file, available only if the ontology term is 1H NMR or 13C NMR'])
54-
sheet.add_row(['Device data', 'The original device data, can not be changed after data uploaded'])
55-
sheet.add_row([''])
54+
sheet.add_row(['Source?', '[Device] from device, [Chemotion] from Chemotion'])
55+
sheet.add_row(['Source identifier', 'The source identifier'])
56+
sheet.add_row(['Source data', 'The data from Device or Chemotion, cannot be modified once a generic dataset is created'])
5657
sheet.add_row([''])
5758
sheet.add_row([''])
58-
sheet.add_row(['', '(This file is automatically generated by the system.)'])
5959
end
6060

6161
def export(id)
@@ -84,29 +84,41 @@ def export(id)
8484
next if field['type'] == 'dummy'
8585

8686
type = field['type']
87-
from_device = field['device'].present? ? 'Yes' : 'No'
88-
from_device = field['system'].present? ? 'SYS' : from_device
87+
from_device = field['device'].present? ? 'Device' : ''
88+
from_device = field['system'].present? ? 'Chemotion' : from_device
8989
type = "#{field['type']}-#{field['option_layers']}" if field['type'] == 'select' || field['type'] == 'system-defined'
9090

9191
show_value = field['value'] =~ /\A\d+,\d+\z/ ? field['value']&.gsub(',', '.') : field['value']
92-
sheet.add_row([' ', field['label'], show_value, field['value_system'], field['field'], type, from_device, field['dkey'], field['system'] || field['device']].freeze)
92+
sheet.add_row([' ', field['label'], nil, field['value_system'], field['field'], type, from_device, field['dkey'], nil].freeze)
93+
94+
if %w[system-defined integer].include? field['type']
95+
sheet.rows.last.cells[2].type = :integer
96+
sheet.rows.last.cells[8].type = :integer
97+
else
98+
sheet.rows.last.cells[2].type = :string
99+
sheet.rows.last.cells[8].type = :string
100+
end
101+
sheet.rows.last.cells[2].value = show_value
102+
sheet.rows.last.cells[8].value = field['system'] || field['device']
103+
104+
93105
end
94106
# sheet.column_widths nil, nil, nil, nil, 0, 0, 0, 0, 0
95107
end
96108
end
97109

98110
def spectra(id)
111+
name_mapping = []
99112
wb = @xfile.workbook
100113
gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
101114
cds = Container.find(id)
102-
cds_csv = cds.attachments.where(aasm_state: 'csv')
115+
cds_csv = cds.attachments.where(aasm_state: 'csv').order(:filename)
103116
csv_length = cds_csv.length
104117
return if csv_length.zero?
105118
cds_csv.each_with_index do |att, idx|
106-
name = File.basename(att.filename, '.csv')
107-
name = name.slice(0, (25 - csv_length.to_s.length - 1))
108-
sheet_name = "#{name}_#{idx}"
119+
sheet_name = "Sheet#{idx+1}"
109120
sheet = @xfile.workbook.add_worksheet(name: sheet_name)
121+
name_mapping.push([sheet_name, att.filename])
110122

111123
if Labimotion::IS_RAILS5 == true
112124
File.open(att.store.path) do |fi|
@@ -122,10 +134,22 @@ def spectra(id)
122134
end
123135
end
124136
end
137+
138+
if name_mapping.length > 1
139+
first_sheet = @xfile.workbook.worksheets&.first
140+
header_style = first_sheet&.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
141+
first_sheet&.add_row(['Sheet name', 'File name'], style: header_style)
142+
name_mapping&.each do |mapping|
143+
next if mapping.length < 2
144+
145+
@xfile.workbook.worksheets&.first&.add_row([mapping[0].to_s, mapping[1].to_s])
146+
end
147+
end
148+
125149
end
126150

127151
def header
128-
['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'from device?', 'Device source', 'Device data'].freeze
152+
['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'Source?', 'Source identifier', 'Source data'].freeze
129153
end
130154

131155
def read

lib/labimotion/libs/nmr_mapper.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ def self.process_ds(id, current_user = {})
1010
att = Attachment.find_by(id: id, con_state: Labimotion::ConState::NMR)
1111
return if att.nil?
1212

13-
content = is_brucker_binary(id)
14-
if content.nil?
13+
result = is_brucker_binary(id)
14+
if result[:is_bagit] == true
15+
att.update_column(:con_state, Labimotion::ConState::CONVERTED)
16+
Labimotion::Converter.metadata(id)
17+
Labimotion::ConState::COMPLETED
18+
elsif result[:metadata] == nil
1519
Labimotion::ConState::NONE
1620
else
1721
data = process(att, id, content)
@@ -29,7 +33,9 @@ def self.is_brucker_binary(id)
2933
zip_file.each do |entry|
3034
if entry.name.include?('/pdata/') && entry.name.include?('parm.txt')
3135
metadata = entry.get_input_stream.read.force_encoding('UTF-8')
32-
return metadata
36+
return { is_bagit: false, metadata: metadata }
37+
elsif entry.name.include?('metadata/') && entry.name.include?('converter.json')
38+
return { is_bagit: true, metadata: nil }
3339
end
3440
end
3541
end
@@ -39,13 +45,15 @@ def self.is_brucker_binary(id)
3945
zip_file.each do |entry|
4046
if entry.name.include?('/pdata/') && entry.name.include?('parm.txt')
4147
metadata = entry.get_input_stream.read.force_encoding('UTF-8')
42-
return metadata
48+
return { is_bagit: false, metadata: metadata }
49+
elsif entry.name.include?('metadata/') && entry.name.include?('converter.json')
50+
return { is_bagit: true, metadata: nil }
4351
end
4452
end
4553
end
4654
end
4755
end
48-
nil
56+
{ is_bagit: false, metadata: nil }
4957
end
5058

5159
def self.process(att, id, content)

lib/labimotion/version.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@
33
## Labimotion Version
44
module Labimotion
55
IS_RAILS5 = false
6-
VERSION_ELN = '1.0.19'
7-
VERSION_REPO = '0.3.1'
8-
9-
VERSION = Labimotion::VERSION_REPO if Labimotion::IS_RAILS5 == true
10-
VERSION = Labimotion::VERSION_ELN if Labimotion::IS_RAILS5 == false
6+
VERSION = '1.1.0'
117
end

0 commit comments

Comments
 (0)