Merge pull request #1451 from jbargu/jg_sci_2937_team_export

Extract the experiments export & import code from team_exporter/team_importer
This commit is contained in:
Alex Kriuchykhin 2019-01-18 14:40:44 +01:00 committed by GitHub
commit 849d5c7509
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 448 additions and 314 deletions

View file

@ -357,7 +357,7 @@ GEM
pry (>= 0.10.4)
public_suffix (3.0.2)
puma (3.11.2)
rack (2.0.5)
rack (2.0.6)
rack-attack (5.4.1)
rack (>= 1.0, < 3)
rack-test (1.1.0)

View file

@ -118,6 +118,7 @@
toggleButtons(true);
setTimeout(function() {
$.initTooltips();
initStepsComments();
initPreviewModal();
SmartAnnotation.preventPropagation('.atwho-user-popover');
@ -613,6 +614,7 @@
setupAssetsLoading();
DragNDropSteps.clearFiles();
initPreviewModal();
$.initTooltips();
},
error: function(xhr) {
if (xhr.responseJSON['assets.file']) {

View file

@ -0,0 +1,103 @@
# frozen_string_literal: true
module ModelExporters
class ExperimentExporter < ModelExporter
def initialize(experiment_id)
@experiment = Experiment.find_by_id(experiment_id)
raise StandardError, 'Can not load experiment' unless @experiment
@assets_to_copy = []
end
def export_to_dir
@asset_counter = 0
@experiment.transaction(isolation: :serializable) do
@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(
File.join(@dir_to_export, 'experiment_export.json'),
experiment[0].to_json
)
# Copying assets
copy_files(@assets_to_copy, :file, File.join(@dir_to_export, 'assets')) do
@asset_counter += 1
end
puts "Exported assets: #{@asset_counter}"
puts 'Done!'
end
end
def experiment
return {
experiment: @experiment,
my_modules: @experiment.my_modules.map { |m| my_module(m) },
my_module_groups: @experiment.my_module_groups
}, @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,
protocols: my_module.protocols.map { |pr| protocol(pr) },
results: my_module.results.map { |res| result(res) }
}
end
def protocol(protocol)
{
protocol: protocol,
protocol_protocol_keywords: protocol.protocol_protocol_keywords,
steps: protocol.steps.map { |s| step(s) }
}
end
def step(step)
@assets_to_copy.push(step.assets.to_a) if step.assets.present?
{
step: step,
checklists: step.checklists.map { |c| checklist(c) },
step_comments: step.step_comments,
step_assets: step.step_assets,
assets: step.assets,
step_tables: step.step_tables,
tables: step.tables.map { |t| table(t) }
}
end
def checklist(checklist)
{
checklist: checklist,
checklist_items: checklist.checklist_items
}
end
def table(table)
return {} if table.nil?
table_json = table.as_json(except: %i(contents data_vector))
table_json['contents'] = Base64.encode64(table.contents)
table_json['data_vector'] = Base64.encode64(table.data_vector)
table_json
end
def result(result)
@assets_to_copy.push(result.asset) if result.asset.present?
{
result: result,
result_comments: result.result_comments,
asset: result.asset,
table: table(result.table),
result_text: result.result_text
}
end
end
end

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
require 'fileutils'
module ModelExporters
class ModelExporter
def copy_files(assets, attachment_name, dir_name)
assets.flatten.each do |a|
next unless a.public_send(attachment_name).present?
unless a.public_send(attachment_name).exists?
raise StandardError,
"File id:#{a.id} of type #{attachment_name} is missing"
end
yield if block_given?
dir = FileUtils.mkdir_p(File.join(dir_name, a.id.to_s)).first
if defined?(S3_BUCKET)
s3_asset =
S3_BUCKET.object(a.public_send(attachment_name).path.remove(%r{^/}))
file_name = a.public_send(attachment_name).original_filename
File.open(File.join(dir, file_name), 'wb') do |f|
s3_asset.get(response_target: f)
end
else
FileUtils.cp(
a.public_send(attachment_name).path,
File.join(dir, a.public_send(attachment_name).original_filename)
)
end
end
end
def export_to_dir
raise NotImplementedError, '#export_to_dir method not implemented.'
end
end
end

View file

@ -0,0 +1,162 @@
# frozen_string_literal: true
module ModelExporters
class TeamExporter < ModelExporter
def initialize(team_id)
@team = Team.includes(:user_teams).find_by_id(team_id)
raise StandardError, 'Can not load team' unless @team
@assets_to_copy = []
@tiny_mce_assets_to_copy = []
end
def export_to_dir
@asset_counter = 0
@team.transaction(isolation: :serializable) do
@dir_to_export = FileUtils.mkdir_p(
File.join("tmp/team_#{@team.id}_export_#{Time.now.to_i}")
).first
# Writing JSON file with team structure
File.write(
File.join(@dir_to_export, 'team_export.json'),
team(@team).to_json
)
# Copying assets
copy_files(@assets_to_copy, :file, File.join(@dir_to_export, 'assets')) do
@asset_counter += 1
end
# Copying tiny_mce_assets
copy_files(@tiny_mce_assets_to_copy,
:image,
File.join(@dir_to_export, 'tiny_mce_assets'))
puts "Exported assets: #{@asset_counter}"
puts "Exported tinyMCE assets: #{@team.tiny_mce_assets.count}"
puts "Exported users: #{@team.users.count}"
puts "Exported repositories: #{@team.repositories.count}"
puts "Exported projects: #{@team.projects.count}"
puts 'Done!'
end
end
private
def team(team)
if team.tiny_mce_assets.present?
@tiny_mce_assets_to_copy.push(team.tiny_mce_assets)
end
{
team: team,
default_admin_id: team.user_teams.where(role: 2).first.user.id,
users: team.users.map { |u| user(u) },
user_teams: team.user_teams,
notifications: Notification
.includes(:user_notifications)
.where('user_notifications.user_id': team.users)
.map { |n| notification(n) },
custom_fields: team.custom_fields,
repositories: team.repositories.map { |r| repository(r) },
tiny_mce_assets: team.tiny_mce_assets,
protocols: team.protocols.where(my_module: nil).map { |pr| protocol(pr) },
protocol_keywords: team.protocol_keywords,
projects: team.projects.map { |p| project(p) }
}
end
def notification(notification)
notification_json = notification.as_json
notification_json['type_of'] = Extends::NOTIFICATIONS_TYPES
.key(notification
.read_attribute('type_of'))
.to_s
notification_json
end
def user(user)
user_json = user.as_json
# Looks like Devise doesn't export some fields to JSON, so add it manually
user_json['encrypted_password'] = user.encrypted_password
user_json['confirmed_at'] = user.confirmed_at
user_json['sign_in_count'] = user.sign_in_count
user_json['last_sign_in_at'] = user.last_sign_in_at
user_json['last_sign_in_ip'] = user.last_sign_in_ip
copy_files([user], :avatar, File.join(@dir_to_export, 'avatars'))
{
user: user_json,
user_notifications: user.user_notifications,
user_identities: user.user_identities,
repository_table_states:
user.repository_table_states.where(repository: @team.repositories)
}
end
def project(project)
experiments = project.experiments.map do |e|
experiment, assets = ExperimentExporter.new(e.id).experiment
@assets_to_copy << assets
experiment
end
{
project: project,
user_projects: project.user_projects,
activities: project.activities,
project_comments: project.project_comments,
reports: project.reports.map { |r| report(r) },
experiments: experiments,
tags: project.tags
}
end
def report(report)
{
report: report,
report_elements: report.report_elements
}
end
def repository(repository)
{
repository: repository,
repository_columns: repository.repository_columns.map do |c|
repository_column(c)
end,
repository_rows: repository.repository_rows.map do |r|
repository_row(r)
end
}
end
def repository_row(repository_row)
{
repository_row: repository_row,
my_module_repository_rows: repository_row.my_module_repository_rows,
repository_cells: repository_row.repository_cells.map do |c|
repository_cell(c)
end
}
end
def repository_cell(cell)
{
repository_cell: cell,
repository_value: cell.value,
repository_value_asset: get_cell_value_asset(cell)
}
end
def repository_column(column)
{
repository_column: column,
repository_list_items: column.repository_list_items
}
end
def get_cell_value_asset(cell)
return unless cell.value_type == 'RepositoryAssetValue'
@assets_to_copy.push(cell.value.asset)
cell.value.asset
end
end
end

View file

@ -1,244 +0,0 @@
require 'fileutils'
class TeamExporter
def initialize(team_id)
@team = Team.includes(:user_teams).find_by_id(team_id)
raise StandardError, 'Can not load team' unless @team
@assets_to_copy = []
@tiny_mce_assets_to_copy = []
end
def export_to_dir
@asset_counter = 0
@team.transaction(isolation: :serializable) do
@dir_to_export = FileUtils.mkdir_p(
File.join("tmp/team_#{@team.id}_export_#{Time.now.to_i}")
).first
# Writing JSON file with team structure
File.write(
File.join(@dir_to_export, 'team_export.json'),
team(@team).to_json
)
# Copying assets
copy_files(@assets_to_copy, :file, File.join(@dir_to_export, 'assets')) do
@asset_counter += 1
end
# Copying tiny_mce_assets
copy_files(@tiny_mce_assets_to_copy,
:image,
File.join(@dir_to_export, 'tiny_mce_assets'))
puts "Exported assets: #{@asset_counter}"
puts "Exported tinyMCE assets: #{@team.tiny_mce_assets.count}"
puts "Exported users: #{@team.users.count}"
puts "Exported repositories: #{@team.repositories.count}"
puts "Exported projects: #{@team.projects.count}"
puts 'Done!'
end
end
private
def copy_files(assets, attachment_name, dir_name)
assets.flatten.each do |a|
next unless a.public_send(attachment_name).present?
unless a.public_send(attachment_name).exists?
raise StandardError,
"File id:#{a.id} of type #{attachment_name} is missing"
end
yield if block_given?
dir = FileUtils.mkdir_p(File.join(dir_name, a.id.to_s)).first
if ENV['S3_BUCKET']
s3_asset =
S3_BUCKET.object(a.public_send(attachment_name).path.remove(%r{^/}))
file_name = a.public_send(attachment_name).original_filename
File.open(File.join(dir, file_name), 'wb') do |f|
s3_asset.get(response_target: f)
end
else
FileUtils.cp(
a.public_send(attachment_name).path,
File.join(dir, a.public_send(attachment_name).original_filename)
)
end
end
end
def team(team)
if team.tiny_mce_assets.present?
@tiny_mce_assets_to_copy.push(team.tiny_mce_assets)
end
{
team: team,
default_admin_id: team.user_teams.where(role: 2).first.user.id,
users: team.users.map { |u| user(u) },
user_teams: team.user_teams,
notifications: Notification
.includes(:user_notifications)
.where('user_notifications.user_id': team.users)
.map { |n| notification(n) },
custom_fields: team.custom_fields,
repositories: team.repositories.map { |r| repository(r) },
tiny_mce_assets: team.tiny_mce_assets,
protocols: team.protocols.where(my_module: nil).map { |pr| protocol(pr) },
protocol_keywords: team.protocol_keywords,
projects: team.projects.map { |p| project(p) }
}
end
def notification(notification)
notification_json = notification.as_json
notification_json['type_of'] = Extends::NOTIFICATIONS_TYPES
.key(notification.read_attribute('type_of'))
.to_s
notification_json
end
def user(user)
user_json = user.as_json
# Looks like Devise doesn't export some fields to JSON, so add it manually
user_json['encrypted_password'] = user.encrypted_password
user_json['confirmed_at'] = user.confirmed_at
user_json['sign_in_count'] = user.sign_in_count
user_json['last_sign_in_at'] = user.last_sign_in_at
user_json['last_sign_in_ip'] = user.last_sign_in_ip
copy_files([user], :avatar, File.join(@dir_to_export, 'avatars'))
{
user: user_json,
user_notifications: user.user_notifications,
user_identities: user.user_identities,
repository_table_states:
user.repository_table_states.where(repository: @team.repositories)
}
end
def project(project)
{
project: project,
user_projects: project.user_projects,
activities: project.activities,
project_comments: project.project_comments,
reports: project.reports.map { |r| report(r) },
experiments: project.experiments.map { |e| experiment(e) },
tags: project.tags
}
end
def report(report)
{
report: report,
report_elements: report.report_elements
}
end
def experiment(experiment)
{
experiment: experiment,
my_modules: experiment.my_modules.map { |m| my_module(m) },
my_module_groups: experiment.my_module_groups
}
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,
protocols: my_module.protocols.map { |pr| protocol(pr) },
results: my_module.results.map { |res| result(res) }
}
end
def protocol(protocol)
{
protocol: protocol,
protocol_protocol_keywords: protocol.protocol_protocol_keywords,
steps: protocol.steps.map { |s| step(s) }
}
end
def step(step)
@assets_to_copy.push(step.assets.to_a) if step.assets.present?
{
step: step,
checklists: step.checklists.map { |c| checklist(c) },
step_comments: step.step_comments,
step_assets: step.step_assets,
assets: step.assets,
step_tables: step.step_tables,
tables: step.tables.map { |t| table(t) }
}
end
def checklist(checklist)
{
checklist: checklist,
checklist_items: checklist.checklist_items
}
end
def table(table)
return {} if table.nil?
table_json = table.as_json(except: %i(contents data_vector))
table_json['contents'] = Base64.encode64(table.contents)
table_json['data_vector'] = Base64.encode64(table.data_vector)
table_json
end
def result(result)
@assets_to_copy.push(result.asset) if result.asset.present?
{
result: result,
result_comments: result.result_comments,
asset: result.asset,
table: table(result.table),
result_text: result.result_text
}
end
def repository(repository)
{
repository: repository,
repository_columns: repository.repository_columns.map do |c|
repository_column(c)
end,
repository_rows: repository.repository_rows.map do |r|
repository_row(r)
end
}
end
def repository_row(repository_row)
{
repository_row: repository_row,
my_module_repository_rows: repository_row.my_module_repository_rows,
repository_cells: repository_row.repository_cells.map do |c|
repository_cell(c)
end
}
end
def repository_cell(cell)
{
repository_cell: cell,
repository_value: cell.value,
repository_value_asset: get_cell_value_asset(cell)
}
end
def repository_column(column)
{
repository_column: column,
repository_list_items: column.repository_list_items
}
end
def get_cell_value_asset(cell)
return unless cell.value_type == 'RepositoryAssetValue'
@assets_to_copy.push(cell.value.asset)
cell.value.asset
end
end

View file

@ -1,3 +1,7 @@
# frozen_string_literal: true
require 'digest'
class TeamImporter
def initialize
@user_mappings = {}
@ -139,6 +143,38 @@ class TeamImporter
end
end
def import_template_experiment_from_dir(import_dir, project_id,
user_id)
# Remove callbacks that can cause problems when importing
MyModule.skip_callback(:create, :before, :create_blank_protocol)
Protocol.skip_callback(:save, :after, :update_linked_children)
@import_dir = import_dir
@is_template = true
# Parse the experiment file and save it to DB
project = Project.find_by_id(project_id)
experiment_json = JSON.parse(File.read("#{@import_dir}/experiment_export.json"))
experiment = create_experiment(experiment_json, project, user_id)
# Create connections for modules
experiment_json['my_modules'].each do |my_module_json|
create_task_connections(my_module_json['outputs'])
end
# Create UUID for the template experiment
# TODO: this requires DB migration first
# experiment.template_uid = Digest::SHA256.new("#{expirement.name + experiment.created_at}")
# experiment.save!
# Reset callbacks
MyModule.set_callback(:create, :before, :create_blank_protocol)
Protocol.set_callback(:save, :after, :update_linked_children)
puts "Imported experiment: #{experiment.id}"
experiment
end
private
def update_smart_annotations(team)
@ -483,32 +519,39 @@ class TeamImporter
def create_experiments(experiments_json, project)
puts('Creating experiments...')
experiments_json.each do |experiment_json|
experiment = Experiment.new(experiment_json['experiment'])
orig_experiment_id = experiment.id
experiment.id = nil
experiment.project = project
experiment.created_by_id = find_user(experiment.created_by_id)
experiment.last_modified_by_id = find_user(experiment.last_modified_by_id)
experiment.archived_by_id = find_user(experiment.archived_by_id)
experiment.restored_by_id = find_user(experiment.restored_by_id)
experiment.save!
@experiment_mappings[orig_experiment_id] = experiment.id
experiment_json['my_module_groups'].each do |my_module_group_json|
my_module_group = MyModuleGroup.new(my_module_group_json)
orig_module_group_id = my_module_group.id
my_module_group.id = nil
my_module_group.experiment = experiment
my_module_group.created_by_id =
find_user(my_module_group.created_by_id)
my_module_group.save!
@my_module_group_mappings[orig_module_group_id] = my_module_group.id
end
experiment.delay.generate_workflow_img
create_my_modules(experiment_json['my_modules'], experiment)
create_experiment(experiment_json, project)
end
end
def create_my_modules(my_modules_json, experiment)
def create_experiment(experiment_json, project, user_id = nil)
experiment = Experiment.new(experiment_json['experiment'])
orig_experiment_id = experiment.id
experiment.id = nil
experiment.project = project
experiment.created_by_id =
user_id || find_user(experiment.created_by_id)
experiment.last_modified_by_id =
user_id || find_user(experiment.last_modified_by_id)
experiment.archived_by_id = find_user(experiment.archived_by_id)
experiment.restored_by_id = find_user(experiment.restored_by_id)
experiment.save!
@experiment_mappings[orig_experiment_id] = experiment.id
experiment_json['my_module_groups'].each do |my_module_group_json|
my_module_group = MyModuleGroup.new(my_module_group_json)
orig_module_group_id = my_module_group.id
my_module_group.id = nil
my_module_group.experiment = experiment
my_module_group.created_by_id =
user_id || find_user(my_module_group.created_by_id)
my_module_group.save!
@my_module_group_mappings[orig_module_group_id] = my_module_group.id
end
experiment.delay.generate_workflow_img
create_my_modules(experiment_json['my_modules'], experiment, user_id)
experiment
end
def create_my_modules(my_modules_json, experiment, user_id = nil)
puts('Creating my_modules...')
my_modules_json.each do |my_module_json|
my_module = MyModule.new(my_module_json['my_module'])
@ -516,9 +559,10 @@ class TeamImporter
my_module.id = nil
my_module.my_module_group_id =
@my_module_group_mappings[my_module.my_module_group_id]
my_module.created_by_id = find_user(my_module.created_by_id)
my_module.created_by_id =
user_id || find_user(my_module.created_by_id)
my_module.last_modified_by_id =
find_user(my_module.last_modified_by_id)
user_id || find_user(my_module.last_modified_by_id)
my_module.archived_by_id = find_user(my_module.archived_by_id)
my_module.restored_by_id = find_user(my_module.restored_by_id)
my_module.experiment = experiment
@ -526,22 +570,24 @@ class TeamImporter
@my_module_mappings[orig_my_module_id] = my_module.id
@my_module_counter += 1
my_module_json['my_module_tags'].each do |my_module_tag_json|
my_module_tag = MyModuleTag.new(my_module_tag_json)
my_module_tag.id = nil
my_module_tag.my_module = my_module
my_module_tag.tag_id = @tag_mappings[my_module_tag.tag_id]
my_module_tag.created_by_id =
find_user(my_module_tag.created_by_id)
my_module_tag.save!
unless @is_template
my_module_json['my_module_tags'].each do |my_module_tag_json|
my_module_tag = MyModuleTag.new(my_module_tag_json)
my_module_tag.id = nil
my_module_tag.my_module = my_module
my_module_tag.tag_id = @tag_mappings[my_module_tag.tag_id]
my_module_tag.created_by_id =
user_id || find_user(my_module_tag.created_by_id)
my_module_tag.save!
end
end
my_module_json['task_comments'].each do |task_comment_json|
task_comment = TaskComment.new(task_comment_json)
task_comment.id = nil
task_comment.user_id = find_user(task_comment.user_id)
task_comment.user_id = user_id || find_user(task_comment.user_id)
task_comment.last_modified_by_id =
find_user(task_comment.last_modified_by_id)
user_id || find_user(task_comment.last_modified_by_id)
task_comment.my_module = my_module
task_comment.save!
end
@ -550,25 +596,27 @@ class TeamImporter
user_module = UserMyModule.new(user_module_json)
user_module.id = nil
user_module.my_module = my_module
user_module.user_id = find_user(user_module.user_id)
user_module.user_id = user_id || find_user(user_module.user_id)
user_module.assigned_by_id =
find_user(user_module.assigned_by_id)
user_id || find_user(user_module.assigned_by_id)
user_module.save!
end
create_protocols(my_module_json['protocols'], my_module)
create_protocols(my_module_json['protocols'],
my_module, nil, user_id)
create_results(my_module_json['results'], my_module)
create_results(my_module_json['results'], my_module, user_id)
end
end
def create_protocols(protocols_json, my_module = nil, team = nil)
def create_protocols(protocols_json, my_module = nil, team = nil,
user_id = nil)
puts 'Creating protocols...'
protocols_json.each do |protocol_json|
protocol = Protocol.new(protocol_json['protocol'])
orig_protocol_id = protocol.id
protocol.id = nil
protocol.added_by_id = find_user(protocol.added_by_id)
protocol.team = team ? team : my_module.experiment.project.team
protocol.team = team || my_module.experiment.project.team
protocol.archived_by_id = find_user(protocol.archived_by_id)
protocol.restored_by_id = find_user(protocol.restored_by_id)
protocol.my_module = my_module unless protocol.my_module_id.nil?
@ -587,18 +635,19 @@ class TeamImporter
@protocol_keyword_mappings[pp_keyword.protocol_keyword_id]
pp_keyword.save!
end
create_steps(protocol_json['steps'], protocol)
create_steps(protocol_json['steps'], protocol, user_id)
end
end
def create_steps(steps_json, protocol)
def create_steps(steps_json, protocol, user_id = nil)
puts('Creating steps...')
steps_json.each do |step_json|
step = Step.new(step_json['step'])
orig_step_id = step.id
step.id = nil
step.user_id = find_user(step.user_id)
step.last_modified_by_id = find_user(step.last_modified_by_id)
step.user_id = user_id || find_user(step.user_id)
step.last_modified_by_id =
user_id || find_user(step.last_modified_by_id)
step.protocol_id = protocol.id
step.save!
@step_mappings[orig_step_id] = step.id
@ -607,9 +656,9 @@ class TeamImporter
step_json['step_comments'].each do |step_comment_json|
step_comment = StepComment.new(step_comment_json)
step_comment.id = nil
step_comment.user_id = find_user(step_comment.user_id)
step_comment.user_id = user_id || find_user(step_comment.user_id)
step_comment.last_modified_by_id =
find_user(step_comment.last_modified_by_id)
user_id || find_user(step_comment.last_modified_by_id)
step_comment.step = step
step_comment.save!
end
@ -618,9 +667,9 @@ class TeamImporter
table = Table.new(table_json)
orig_table_id = table.id
table.id = nil
table.created_by_id = find_user(table.created_by_id)
table.created_by_id = user_id || find_user(table.created_by_id)
table.last_modified_by_id =
find_user(table.last_modified_by_id)
user_id || find_user(table.last_modified_by_id)
table.team = protocol.team
table.contents = Base64.decode64(table.contents)
table.data_vector = Base64.decode64(table.data_vector)
@ -630,23 +679,24 @@ class TeamImporter
end
step_json['assets'].each do |asset_json|
asset = create_asset(asset_json, protocol.team)
asset = create_asset(asset_json, protocol.team, user_id)
StepAsset.create!(step: step, asset: asset)
end
create_step_checklists(step_json['checklists'], step)
create_step_checklists(step_json['checklists'], step, user_id)
end
end
def create_results(results_json, my_module)
def create_results(results_json, my_module, user_id = nil)
puts('Creating results...')
results_json.each do |result_json|
result = Result.new(result_json['result'])
orig_result_id = result.id
result.id = nil
result.my_module = my_module
result.user_id = find_user(result.user_id)
result.last_modified_by_id = find_user(result.last_modified_by_id)
result.user_id = user_id || find_user(result.user_id)
result.last_modified_by_id =
user_id || find_user(result.last_modified_by_id)
result.archived_by_id = find_user(result.archived_by_id)
result.restored_by_id = find_user(result.restored_by_id)
@ -654,9 +704,9 @@ class TeamImporter
table = Table.new(result_json['table'])
orig_table_id = table.id
table.id = nil
table.created_by_id = find_user(table.created_by_id)
table.created_by_id = user_id || find_user(table.created_by_id)
table.last_modified_by_id =
find_user(table.last_modified_by_id)
user_id || find_user(table.last_modified_by_id)
table.team = my_module.experiment.project.team
table.contents = Base64.decode64(table.contents)
table.data_vector = Base64.decode64(table.data_vector)
@ -667,7 +717,8 @@ class TeamImporter
if result_json['asset'].present?
asset = create_asset(result_json['asset'],
my_module.experiment.project.team)
my_module.experiment.project.team,
user_id)
result.asset = asset
end
@ -687,9 +738,10 @@ class TeamImporter
result_json['result_comments'].each do |result_comment_json|
result_comment = ResultComment.new(result_comment_json)
result_comment.id = nil
result_comment.user_id = find_user(result_comment.user_id)
result_comment.user_id =
user_id || find_user(result_comment.user_id)
result_comment.last_modified_by_id =
find_user(result_comment.last_modified_by_id)
user_id || find_user(result_comment.last_modified_by_id)
result_comment.result = result
result_comment.save!
end
@ -697,16 +749,16 @@ class TeamImporter
end
# returns asset object
def create_asset(asset_json, team)
def create_asset(asset_json, team, user_id = nil)
asset = Asset.new(asset_json)
File.open(
"#{@import_dir}/assets/#{asset.id}/#{asset.file_file_name}"
) do |file|
orig_asset_id = asset.id
asset.id = nil
asset.created_by_id = find_user(asset.created_by_id)
asset.created_by_id = user_id || find_user(asset.created_by_id)
asset.last_modified_by_id =
find_user(asset.last_modified_by_id)
user_id || find_user(asset.last_modified_by_id)
asset.team = team
asset.file = file
asset.save!
@ -716,24 +768,26 @@ class TeamImporter
asset
end
def create_step_checklists(step_checklists_json, step)
def create_step_checklists(step_checklists_json, step, user_id = nil)
step_checklists_json.each do |checklist_json|
checklist = Checklist.new(checklist_json['checklist'])
orig_checklist_id = checklist.id
checklist.id = nil
checklist.step = step
checklist.created_by_id = find_user(checklist.created_by_id)
checklist.created_by_id =
user_id || find_user(checklist.created_by_id)
checklist.last_modified_by_id =
find_user(checklist.last_modified_by_id)
user_id || find_user(checklist.last_modified_by_id)
checklist.save!
@checklist_mappings[orig_checklist_id] = checklist.id
checklist_json['checklist_items'].each do |checklist_item_json|
checklist_item = ChecklistItem.new(checklist_item_json)
checklist_item.id = nil
checklist_item.checklist = checklist
checklist_item.created_by_id = find_user(checklist_item.created_by_id)
checklist_item.created_by_id =
user_id || find_user(checklist_item.created_by_id)
checklist_item.last_modified_by_id =
find_user(checklist_item.last_modified_by_id)
user_id || find_user(checklist_item.last_modified_by_id)
checklist_item.save!
end
end

View file

@ -85,8 +85,8 @@ namespace :data do
Rails.logger.info(
"Exporting team with ID:#{args[:team_id]} to directory in tmp"
)
te = TeamExporter.new(args[:team_id])
te.export_to_dir if te
te = ModelExporters::TeamExporter.new(args[:team_id])
te&.export_to_dir
end
desc 'Import team from directory'
@ -104,6 +104,26 @@ namespace :data do
)
team = Team.find_by_id(args[:team_id])
raise StandardError, 'Can not load team' unless team
UserDataDeletion.delete_team_data(team) if team
end
desc 'Export experiment to directory'
task :experiment_export, [:experiment_id] => [:environment] do |_, args|
Rails.logger.info(
"Exporting experiment with ID:#{args[:experiment_id]} to directory in tmp"
)
ee = ModelExporters::ExperimentExporter.new(args[:experiment_id])
ee&.export_to_dir
end
desc 'Import experiment from directory to given project'
task :experiment_import, %i(dir_path project_id user_id) => [:environment] do |_, args|
Rails.logger.info(
"Importing experiment from directory #{args[:dir_path]}"
)
TeamImporter.new.import_template_experiment_from_dir(args[:dir_path],
args[:project_id],
args[:user_id])
end
end