Merge branch 'features/templates' into ok_SCI_3037

This commit is contained in:
Oleksii Kriuchykhin 2019-02-15 10:47:38 +01:00
commit c186498a5e
8 changed files with 44 additions and 37 deletions

View file

@ -100,12 +100,6 @@ class Experiment < ApplicationRecord
end
end
def active_module_groups
my_module_groups.joins(:my_modules)
.where('my_modules.archived = ?', false)
.distinct
end
def active_modules
my_modules.where(archived: false)
end

View file

@ -10,6 +10,10 @@ class MyModuleGroup < ApplicationRecord
optional: true
has_many :my_modules, inverse_of: :my_module_group, dependent: :nullify
scope :without_archived_modules, (lambda do
joins(:my_modules).where('my_modules.archived = ?', false).distinct
end)
def deep_clone_to_experiment(current_user, experiment)
clone = MyModuleGroup.new(
created_by: created_by,

View file

@ -313,16 +313,6 @@ class Team < ApplicationRecord
def generate_template_project
return if without_templates
user = created_by
return unless user
Project.transaction do
tmpl_project = projects.create!(
name: Constants::TEMPLATES_PROJECT_NAME,
visibility: :visible,
template: true
)
tmpl_project.user_projects.create!(user: user, role: 0)
TemplatesService.new.update_project(tmpl_project)
end
TemplatesService.new.update_team(self)
end
end

View file

@ -3,12 +3,14 @@
module ModelExporters
class ExperimentExporter < ModelExporter
def initialize(experiment_id)
@include_archived = true
super()
@experiment = Experiment.find(experiment_id)
end
def export_template_to_dir
@asset_counter = 0
@include_archived = false
@experiment.transaction do
@experiment.uuid ||= SecureRandom.uuid
@dir_to_export = FileUtils.mkdir_p(
@ -33,10 +35,17 @@ module ModelExporters
end
def experiment
if @include_archived
my_modules = @experiment.my_modules
my_module_groups = @experiment.my_module_groups
else
my_modules = @experiment.my_modules.active
my_module_groups = @experiment.my_module_groups.without_archived_modules
end
return {
experiment: @experiment,
my_modules: @experiment.my_modules.map { |m| my_module(m) },
my_module_groups: @experiment.my_module_groups
my_modules: my_modules.map { |m| my_module(m) },
my_module_groups: my_module_groups
}, @assets_to_copy
end

View file

@ -13,33 +13,42 @@ class TemplatesService
end
end
def update_project(project)
return unless project.template
owner = project.user_projects
.where(role: 'owner')
.order(:created_at)
.first
.user
def update_team(team)
tmpl_project = team.projects.where(template: true).take
unless tmpl_project
Project.transaction do
tmpl_project = team.projects.create!(
name: Constants::TEMPLATES_PROJECT_NAME,
visibility: :visible,
template: true
)
tmpl_project.user_projects.create!(user: team.created_by, role: 'owner')
end
end
owner = tmpl_project.user_projects
.where(role: 'owner')
.order(:created_at)
.first&.user
return unless owner.present?
updated = false
exp_tmplt_dir_prefix = "#{@base_dir}/experiment_"
existing = project.experiments.where.not(uuid: nil).pluck(:uuid)
existing = tmpl_project.experiments.where.not(uuid: nil).pluck(:uuid)
@experiment_templates.except(*existing).each_value do |id|
importer_service = TeamImporter.new
importer_service.import_experiment_template_from_dir(
exp_tmplt_dir_prefix + id.to_s, project.id, owner.id
exp_tmplt_dir_prefix + id.to_s, tmpl_project.id, owner.id
)
updated = true
end
updated
end
def update_all_projects
def update_all_templates
processed_counter = 0
updated_counter = 0
Project.where(template: true).find_each do |project|
Team.find_each do |team|
processed_counter += 1
updated_counter += 1 if update_project(project)
updated_counter += 1 if update_team(team)
end
[updated_counter, processed_counter]
end

View file

@ -8,7 +8,7 @@ if ENV['ENABLE_TEMPLATES_SYNC'] && ARGV[0] == 'jobs:work'
# Templates sync periodic task
scheduler.every '1h' do
Rails.logger.info('Templates, syncing all template projects')
updated, total = TemplatesService.new.update_all_projects
updated, total = TemplatesService.new.update_all_templates
Rails.logger.info(
"Templates, total number of updated projects: #{updated} out of #{total}}"
)

View file

@ -133,7 +133,7 @@ namespace :data do
desc 'Update all templates projects'
task update_all_templates: :environment do
Rails.logger.info('Templates, syncing all templates projects')
updated, total = TemplatesService.new.update_all_projects
updated, total = TemplatesService.new.update_all_templates
Rails.logger.info(
"Templates, total number of updated projects: #{updated} out of #{total}}"
)

View file

@ -25,21 +25,22 @@ describe TemplatesService do
FileUtils.remove_dir(tmplts_dir) if Dir.exist?(tmplts_dir)
FileUtils.mkdir(tmplts_dir)
FileUtils.mv(exp_dir, "#{tmplts_dir}/experiment_#{demo_exp.id}")
templates_project = create :project, name: 'Templates', template: true
templates_project =
create :project, name: 'Templates', template: true, team: main_team
create(
:user_project, :owner, project: templates_project, user: admin_user
)
ts = TemplatesService.new(tmplts_dir)
ts.update_project(templates_project)
ts.update_team(main_team)
Delayed::Job.all.each { |job| dj_worker.run(job) }
tmpl_exp = templates_project.experiments.first
expect(tmpl_exp.name).to eq(demo_exp.name)
expect(tmpl_exp.uuid).to_not eq(nil)
expect(tmpl_exp.my_modules.pluck(:name))
.to match_array(demo_exp.my_modules.pluck(:name))
.to match_array(demo_exp.active_my_modules.pluck(:name))
tmpl_tasks = tmpl_exp.my_modules
demo_tasks = demo_exp.my_modules
demo_tasks = demo_exp.active_my_modules
demo_tasks.each do |demo_task|
tmpl_task = tmpl_tasks.find_by_name(demo_task.name)
expect(tmpl_task.name).to eq(demo_task.name)