2019-01-16 15:37:05 +08:00
|
|
|
# frozen_string_literal: true
|
2019-01-16 15:00:01 +08:00
|
|
|
|
|
|
|
module ModelExporters
|
2019-01-16 15:37:05 +08:00
|
|
|
class ExperimentExporter < ModelExporter
|
|
|
|
def initialize(experiment_id)
|
2019-02-12 20:27:55 +08:00
|
|
|
@include_archived = true
|
2019-02-12 17:56:15 +08:00
|
|
|
super()
|
|
|
|
@experiment = Experiment.find(experiment_id)
|
2019-01-16 15:00:01 +08:00
|
|
|
end
|
|
|
|
|
2019-01-25 17:50:08 +08:00
|
|
|
def export_template_to_dir
|
2019-01-16 15:37:05 +08:00
|
|
|
@asset_counter = 0
|
2019-02-12 20:27:55 +08:00
|
|
|
@include_archived = false
|
2019-02-02 01:06:36 +08:00
|
|
|
@experiment.transaction do
|
2019-01-25 17:50:08 +08:00
|
|
|
@experiment.uuid ||= SecureRandom.uuid
|
2019-01-16 15:37:05 +08:00
|
|
|
@dir_to_export = FileUtils.mkdir_p(
|
|
|
|
File.join("tmp/experiment_#{@experiment.id}" \
|
|
|
|
"_export_#{Time.now.to_i}")
|
|
|
|
).first
|
|
|
|
|
|
|
|
# Writing JSON file with experiment structure
|
|
|
|
File.write(
|
2019-01-25 17:50:08 +08:00
|
|
|
File.join(@dir_to_export, 'experiment.json'),
|
2020-01-07 22:26:21 +08:00
|
|
|
JSON.pretty_generate(experiment[0].as_json)
|
2019-01-16 15:37:05 +08:00
|
|
|
)
|
|
|
|
# Copying assets
|
2019-01-25 17:50:08 +08:00
|
|
|
assets_dir = File.join(@dir_to_export, 'assets')
|
|
|
|
copy_files(@assets_to_copy, :file, assets_dir) do
|
2019-01-16 15:37:05 +08:00
|
|
|
@asset_counter += 1
|
|
|
|
end
|
|
|
|
puts "Exported assets: #{@asset_counter}"
|
|
|
|
puts 'Done!'
|
2019-02-02 01:06:36 +08:00
|
|
|
return @dir_to_export
|
2019-01-16 15:37:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def experiment
|
2019-02-12 20:27:55 +08:00
|
|
|
if @include_archived
|
|
|
|
my_modules = @experiment.my_modules
|
|
|
|
my_module_groups = @experiment.my_module_groups
|
|
|
|
else
|
2019-02-14 17:53:48 +08:00
|
|
|
my_modules = @experiment.my_modules.active
|
2019-02-14 21:54:04 +08:00
|
|
|
my_module_groups = @experiment.my_module_groups.without_archived_modules
|
2019-02-12 20:27:55 +08:00
|
|
|
end
|
2019-01-16 15:00:01 +08:00
|
|
|
return {
|
|
|
|
experiment: @experiment,
|
2019-02-12 20:27:55 +08:00
|
|
|
my_modules: my_modules.map { |m| my_module(m) },
|
|
|
|
my_module_groups: my_module_groups
|
2019-01-16 15:00:01 +08:00
|
|
|
}, @assets_to_copy
|
|
|
|
end
|
|
|
|
|
|
|
|
def my_module(my_module)
|
|
|
|
{
|
|
|
|
my_module: my_module,
|
|
|
|
outputs: my_module.outputs,
|
|
|
|
my_module_tags: my_module.my_module_tags,
|
|
|
|
task_comments: my_module.task_comments,
|
|
|
|
my_module_repository_rows: my_module.my_module_repository_rows,
|
|
|
|
user_my_modules: my_module.user_my_modules,
|
2019-02-12 20:38:59 +08:00
|
|
|
protocols: my_module.protocols.map { |pr| protocol(pr) },
|
2019-01-16 15:00:01 +08:00
|
|
|
results: my_module.results.map { |res| result(res) }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def result(result)
|
|
|
|
@assets_to_copy.push(result.asset) if result.asset.present?
|
|
|
|
{
|
|
|
|
result: result,
|
|
|
|
result_comments: result.result_comments,
|
2019-07-26 18:40:36 +08:00
|
|
|
asset: result_assets_data(result.asset),
|
2019-01-16 15:00:01 +08:00
|
|
|
table: table(result.table),
|
|
|
|
result_text: result.result_text
|
|
|
|
}
|
|
|
|
end
|
2019-07-26 18:40:36 +08:00
|
|
|
|
|
|
|
def result_assets_data(asset)
|
|
|
|
return unless asset&.file&.attached?
|
|
|
|
|
|
|
|
{
|
|
|
|
asset: asset,
|
|
|
|
asset_blob: asset.file.blob
|
|
|
|
}
|
|
|
|
end
|
2019-01-16 15:00:01 +08:00
|
|
|
end
|
|
|
|
end
|