2021-06-12 16:58:26 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class Experiment < ApplicationRecord
|
2021-07-28 21:16:47 +08:00
|
|
|
ID_PREFIX = 'EX'
|
|
|
|
|
|
|
|
include PrefixedIdModel
|
2021-10-14 19:13:29 +08:00
|
|
|
SEARCHABLE_ATTRIBUTES = ['experiments.name', 'experiments.description', PREFIXED_ID_SQL].freeze
|
2021-06-12 16:58:26 +08:00
|
|
|
|
2017-05-12 17:07:11 +08:00
|
|
|
include ArchivableModel
|
|
|
|
include SearchableModel
|
2019-02-26 18:01:15 +08:00
|
|
|
include SearchableByNameModel
|
2022-12-05 17:30:20 +08:00
|
|
|
include ViewableModel
|
2021-02-09 17:45:12 +08:00
|
|
|
include PermissionCheckableModel
|
2021-06-06 15:54:47 +08:00
|
|
|
include Assignable
|
2022-12-01 22:08:59 +08:00
|
|
|
include Cloneable
|
2016-07-22 16:43:55 +08:00
|
|
|
|
2021-05-28 17:31:45 +08:00
|
|
|
before_save -> { report_elements.destroy_all }, if: -> { !new_record? && project_id_changed? }
|
|
|
|
|
2019-07-26 18:40:36 +08:00
|
|
|
belongs_to :project, inverse_of: :experiments, touch: true
|
2022-06-24 16:44:28 +08:00
|
|
|
delegate :team, to: :project
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :created_by,
|
2019-07-26 18:40:36 +08:00
|
|
|
class_name: 'User'
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :last_modified_by,
|
2019-07-26 18:40:36 +08:00
|
|
|
class_name: 'User'
|
2022-03-09 21:12:16 +08:00
|
|
|
belongs_to :archived_by, class_name: 'User', optional: true
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :restored_by,
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2016-07-22 16:43:55 +08:00
|
|
|
|
|
|
|
has_many :my_modules, inverse_of: :experiment, dependent: :destroy
|
|
|
|
has_many :my_module_groups, inverse_of: :experiment, dependent: :destroy
|
2016-08-03 18:49:24 +08:00
|
|
|
has_many :report_elements, inverse_of: :experiment, dependent: :destroy
|
2019-02-25 22:21:13 +08:00
|
|
|
# Associations for old activity type
|
2017-04-20 16:56:25 +08:00
|
|
|
has_many :activities, inverse_of: :experiment
|
2021-02-04 21:08:20 +08:00
|
|
|
has_many :users, through: :user_assignments
|
2016-08-08 22:02:17 +08:00
|
|
|
|
2021-05-08 16:05:22 +08:00
|
|
|
has_one_attached :workflowimg
|
2021-02-09 17:45:12 +08:00
|
|
|
|
2016-09-21 21:35:23 +08:00
|
|
|
auto_strip_attributes :name, :description, nullify: false
|
2020-12-18 23:44:15 +08:00
|
|
|
validates :name, length: { minimum: Constants::NAME_MIN_LENGTH, maximum: Constants::NAME_MAX_LENGTH }
|
2016-10-05 23:45:20 +08:00
|
|
|
validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH }
|
2016-07-22 16:43:55 +08:00
|
|
|
validates :project, presence: true
|
|
|
|
validates :created_by, presence: true
|
2016-07-25 20:47:13 +08:00
|
|
|
validates :last_modified_by, presence: true
|
2019-01-25 17:50:08 +08:00
|
|
|
validates :uuid, uniqueness: { scope: :project },
|
|
|
|
unless: proc { |e| e.uuid.blank? }
|
2016-07-25 17:25:10 +08:00
|
|
|
|
2020-12-17 20:07:30 +08:00
|
|
|
scope :is_archived, lambda { |is_archived|
|
|
|
|
if is_archived
|
|
|
|
joins(:project).where('experiments.archived = TRUE OR projects.archived = TRUE')
|
|
|
|
else
|
|
|
|
joins(:project).where('experiments.archived = FALSE AND projects.archived = FALSE')
|
|
|
|
end
|
|
|
|
}
|
2016-07-25 22:30:59 +08:00
|
|
|
|
2021-06-12 16:27:53 +08:00
|
|
|
scope :experiment_search_scope, lambda { |project_ids, user|
|
|
|
|
joins(:user_assignments).where(
|
|
|
|
project: project_ids,
|
|
|
|
user_assignments: { user: user }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:33:28 +08:00
|
|
|
def self.search(
|
|
|
|
user,
|
|
|
|
include_archived,
|
|
|
|
query = nil,
|
|
|
|
page = 1,
|
2017-05-05 22:41:23 +08:00
|
|
|
current_team = nil,
|
|
|
|
options = {}
|
2017-01-09 18:33:28 +08:00
|
|
|
)
|
2021-10-22 17:43:20 +08:00
|
|
|
viewable_projects = Project.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT, current_team)
|
|
|
|
.pluck(:id)
|
|
|
|
new_query = Experiment.with_granted_permissions(user, ExperimentPermissions::READ)
|
|
|
|
.where(project: viewable_projects)
|
|
|
|
.where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options)
|
2016-07-25 22:30:59 +08:00
|
|
|
|
2021-10-22 17:43:20 +08:00
|
|
|
new_query = new_query.active unless include_archived
|
2016-07-25 22:30:59 +08:00
|
|
|
|
|
|
|
# Show all results if needed
|
2016-10-05 23:45:20 +08:00
|
|
|
if page == Constants::SEARCH_NO_LIMIT
|
2016-07-25 22:30:59 +08:00
|
|
|
new_query
|
|
|
|
else
|
2021-10-22 17:43:20 +08:00
|
|
|
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
|
2016-07-25 22:30:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-26 18:01:15 +08:00
|
|
|
def self.viewable_by_user(user, teams)
|
2021-10-22 17:43:20 +08:00
|
|
|
with_granted_permissions(user, ExperimentPermissions::READ)
|
2021-06-20 17:11:03 +08:00
|
|
|
.where(project: Project.viewable_by_user(user, teams))
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|
|
|
|
|
2022-07-07 18:00:35 +08:00
|
|
|
def self.filter_by_teams(teams = [])
|
|
|
|
return self if teams.blank?
|
|
|
|
|
|
|
|
joins(:project).where(project: { team: teams })
|
|
|
|
end
|
|
|
|
|
2022-12-05 17:30:20 +08:00
|
|
|
def default_view_state
|
|
|
|
{
|
|
|
|
my_modules: {
|
|
|
|
active: { sort: 'atoz' },
|
2023-01-10 18:08:49 +08:00
|
|
|
archived: { sort: 'atoz' },
|
|
|
|
view_type: 'canvas'
|
2022-12-05 17:30:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_view_state(view_state)
|
2023-01-10 18:08:49 +08:00
|
|
|
if %w(canvas table).exclude?(view_state.state.dig('my_modules', 'view_type')) ||
|
|
|
|
%w(atoz ztoa due_first due_last).exclude?(view_state.state.dig('my_modules', 'active', 'sort')) ||
|
2022-12-05 17:30:20 +08:00
|
|
|
%w(atoz ztoa due_first due_last
|
|
|
|
archived_old archived_new).exclude?(view_state.state.dig('my_modules', 'archived', 'sort'))
|
|
|
|
view_state.errors.add(:state, :wrong_state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-07 00:21:57 +08:00
|
|
|
def connections
|
|
|
|
Connection.joins(
|
|
|
|
'LEFT JOIN my_modules AS inputs ON input_id = inputs.id'
|
|
|
|
).joins(
|
|
|
|
'LEFT JOIN my_modules AS outputs ON output_id = outputs.id'
|
|
|
|
).where(
|
|
|
|
'inputs.experiment_id = ? OR outputs.experiment_id = ?', id, id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-12-13 05:46:30 +08:00
|
|
|
def archived_branch?
|
|
|
|
archived? || project.archived?
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
2020-12-13 05:46:30 +08:00
|
|
|
def navigable?
|
|
|
|
!project.archived?
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_canvas(
|
|
|
|
to_archive,
|
|
|
|
to_add,
|
|
|
|
to_rename,
|
2016-08-19 19:53:43 +08:00
|
|
|
to_move,
|
2016-08-22 23:52:55 +08:00
|
|
|
to_move_groups,
|
2016-07-25 17:25:10 +08:00
|
|
|
to_clone,
|
|
|
|
connections,
|
|
|
|
positions,
|
2017-10-05 22:19:25 +08:00
|
|
|
current_user
|
2016-07-25 17:25:10 +08:00
|
|
|
)
|
|
|
|
begin
|
2017-10-05 22:19:25 +08:00
|
|
|
with_lock do
|
2020-06-15 16:20:10 +08:00
|
|
|
# Start with archiving to release positions for new tasks
|
|
|
|
archive_modules(to_archive, current_user) if to_archive.any?
|
|
|
|
|
|
|
|
# Update only existing tasks positions to release positions for new tasks
|
2020-07-24 23:00:42 +08:00
|
|
|
existing_positions = positions.slice(*positions.keys.map { |k| k unless k.to_s.start_with?('n') }.compact)
|
2020-06-15 16:20:10 +08:00
|
|
|
update_module_positions(existing_positions) if existing_positions.any?
|
|
|
|
|
|
|
|
# Move only existing tasks to release positions for new tasks
|
2020-07-24 23:00:42 +08:00
|
|
|
existing_to_move = to_move.slice(*to_move.keys.map { |k| k unless k.to_s.start_with?('n') }.compact)
|
2020-06-15 16:20:10 +08:00
|
|
|
move_modules(existing_to_move, current_user) if existing_to_move.any?
|
|
|
|
|
|
|
|
# add new modules
|
2016-07-25 17:25:10 +08:00
|
|
|
new_ids, cloned_pairs, originals = add_modules(
|
2017-10-05 22:19:25 +08:00
|
|
|
to_add, to_clone, current_user
|
|
|
|
)
|
2016-07-25 17:25:10 +08:00
|
|
|
|
|
|
|
# Rename modules
|
2019-03-15 17:30:51 +08:00
|
|
|
rename_modules(to_rename, current_user)
|
2016-07-25 17:25:10 +08:00
|
|
|
|
|
|
|
# Add activities that modules were created
|
2019-03-18 22:03:14 +08:00
|
|
|
originals.each do |my_module|
|
|
|
|
log_activity(:create_module, current_user, my_module)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add activities that modules were cloned
|
|
|
|
cloned_pairs.each do |mn, mo|
|
2019-03-18 22:03:14 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :clone_module,
|
|
|
|
owner: current_user,
|
|
|
|
team: project.team,
|
|
|
|
project: mn.experiment.project,
|
|
|
|
subject: mn,
|
|
|
|
message_items: { my_module_original: mo.id,
|
|
|
|
my_module_new: mn.id })
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Update connections, positions & module group variables
|
|
|
|
# with actual IDs retrieved from the new modules creation
|
2016-08-23 16:00:34 +08:00
|
|
|
updated_to_move = {}
|
|
|
|
to_move.each do |id, value|
|
|
|
|
updated_to_move[new_ids.fetch(id, id)] = value
|
|
|
|
end
|
2016-08-23 15:00:25 +08:00
|
|
|
updated_to_move_groups = {}
|
|
|
|
to_move_groups.each do |ids, value|
|
|
|
|
mapped = []
|
|
|
|
ids.each do |id|
|
|
|
|
mapped << new_ids.fetch(id, id)
|
|
|
|
end
|
|
|
|
updated_to_move_groups[mapped] = value
|
|
|
|
end
|
2016-07-25 17:25:10 +08:00
|
|
|
updated_connections = []
|
2017-10-05 22:19:25 +08:00
|
|
|
connections.each do |a, b|
|
2016-07-25 17:25:10 +08:00
|
|
|
updated_connections << [new_ids.fetch(a, a), new_ids.fetch(b, b)]
|
|
|
|
end
|
2017-10-05 22:19:25 +08:00
|
|
|
updated_positions = {}
|
2016-07-25 17:25:10 +08:00
|
|
|
positions.each do |id, pos|
|
|
|
|
updated_positions[new_ids.fetch(id, id)] = pos
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update connections
|
|
|
|
update_module_connections(updated_connections)
|
|
|
|
|
|
|
|
# Update module positions (no validation needed here)
|
|
|
|
update_module_positions(updated_positions)
|
|
|
|
|
|
|
|
# Normalize module positions
|
|
|
|
normalize_module_positions
|
|
|
|
|
|
|
|
# Finally, update module groups
|
2017-10-05 22:19:25 +08:00
|
|
|
update_module_groups(current_user)
|
2016-08-19 21:36:12 +08:00
|
|
|
|
2016-08-24 17:42:21 +08:00
|
|
|
# Finally move any modules to another experiment
|
2019-03-12 00:17:59 +08:00
|
|
|
move_modules(updated_to_move, current_user)
|
2016-08-24 17:42:21 +08:00
|
|
|
|
2016-08-22 23:52:55 +08:00
|
|
|
# Everyhing is set, now we can move any module groups
|
2019-03-29 20:51:17 +08:00
|
|
|
move_module_groups(updated_to_move_groups, current_user)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
2017-10-05 22:19:25 +08:00
|
|
|
rescue ActiveRecord::ActiveRecordError,
|
|
|
|
ArgumentError,
|
2022-03-09 21:12:16 +08:00
|
|
|
ActiveRecord::RecordNotSaved => e
|
|
|
|
logger.error e.message
|
2016-07-25 17:25:10 +08:00
|
|
|
return false
|
|
|
|
end
|
2017-10-05 22:19:25 +08:00
|
|
|
true
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
2019-07-04 15:33:12 +08:00
|
|
|
def workflowimg_exists?
|
2020-10-16 20:21:25 +08:00
|
|
|
workflowimg.attached? && workflowimg.service.exist?(workflowimg.blob.key)
|
2019-07-04 15:33:12 +08:00
|
|
|
end
|
|
|
|
|
2016-08-16 14:47:04 +08:00
|
|
|
# Projects to which this experiment can be moved (inside the same
|
2017-01-24 23:34:21 +08:00
|
|
|
# team and not archived), all users assigned on experiment.project has
|
2016-08-16 14:47:04 +08:00
|
|
|
# to be assigned on such project
|
2021-09-30 17:32:11 +08:00
|
|
|
def movable_projects(current_user)
|
2021-11-12 19:59:49 +08:00
|
|
|
project.team.projects.active
|
|
|
|
.where.not(id: project_id)
|
|
|
|
.where(archived: false)
|
|
|
|
.with_granted_permissions(current_user, ProjectPermissions::EXPERIMENTS_CREATE)
|
2016-08-16 14:47:04 +08:00
|
|
|
end
|
|
|
|
|
2022-12-01 22:08:59 +08:00
|
|
|
def parent
|
|
|
|
project
|
|
|
|
end
|
|
|
|
|
2021-05-08 16:05:22 +08:00
|
|
|
def permission_parent
|
|
|
|
project
|
|
|
|
end
|
|
|
|
|
2017-10-05 22:19:25 +08:00
|
|
|
# Archive all modules. Receives an array of module integer IDs
|
|
|
|
# and current user.
|
2016-07-25 17:25:10 +08:00
|
|
|
def archive_modules(module_ids, current_user)
|
2022-03-09 21:12:16 +08:00
|
|
|
my_modules.where(id: module_ids).find_each do |my_module|
|
2019-03-18 22:03:14 +08:00
|
|
|
my_module.archive!(current_user)
|
2019-03-29 22:41:34 +08:00
|
|
|
log_activity(:archive_module, current_user, my_module)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
my_modules.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add modules, and returns a map of "virtual" IDs with
|
|
|
|
# actual IDs of saved modules.
|
|
|
|
# to_add is an array of hashes, each containing 'name',
|
|
|
|
# 'x', 'y' and 'id'.
|
|
|
|
# to_clone is a hash, storing new cloned modules as keys,
|
|
|
|
# and original modules as values.
|
|
|
|
def add_modules(to_add, to_clone, current_user)
|
|
|
|
originals = []
|
|
|
|
cloned_pairs = {}
|
2017-10-05 22:19:25 +08:00
|
|
|
ids_map = {}
|
2016-07-25 17:25:10 +08:00
|
|
|
to_add.each do |m|
|
2021-09-30 17:32:11 +08:00
|
|
|
original = MyModule.find_by(id: to_clone.fetch(m[:id], nil)) if to_clone.present?
|
2017-10-05 22:19:25 +08:00
|
|
|
if original.present?
|
2016-07-25 17:25:10 +08:00
|
|
|
my_module = original.deep_clone(current_user)
|
|
|
|
cloned_pairs[my_module] = original
|
|
|
|
else
|
2017-10-05 22:19:25 +08:00
|
|
|
my_module = MyModule.new(experiment: self)
|
2016-07-25 17:25:10 +08:00
|
|
|
originals << my_module
|
|
|
|
end
|
|
|
|
|
|
|
|
my_module.name = m[:name]
|
|
|
|
my_module.x = m[:x]
|
|
|
|
my_module.y = m[:y]
|
|
|
|
my_module.created_by = current_user
|
|
|
|
my_module.last_modified_by = current_user
|
|
|
|
my_module.save!
|
|
|
|
|
2020-03-30 21:16:14 +08:00
|
|
|
my_module.assign_user(current_user)
|
|
|
|
|
2016-07-25 17:25:10 +08:00
|
|
|
ids_map[m[:id]] = my_module.id.to_s
|
|
|
|
end
|
|
|
|
my_modules.reload
|
|
|
|
return ids_map, cloned_pairs, originals
|
|
|
|
end
|
|
|
|
|
|
|
|
# Rename modules; this method accepts a map where keys
|
|
|
|
# represent IDs of modules, and values new names for
|
|
|
|
# such modules. If a module with given ID doesn't exist,
|
|
|
|
# it's obviously not updated.
|
2019-03-12 00:17:59 +08:00
|
|
|
def rename_modules(to_rename, current_user)
|
2016-07-25 17:25:10 +08:00
|
|
|
to_rename.each do |id, new_name|
|
2022-03-09 21:12:16 +08:00
|
|
|
my_module = MyModule.find_by(id: id)
|
|
|
|
next if my_module.blank?
|
|
|
|
|
|
|
|
my_module.name = new_name
|
|
|
|
my_module.save!
|
|
|
|
log_activity(:rename_task, current_user, my_module)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-19 19:53:43 +08:00
|
|
|
# Move modules; this method accepts a map where keys
|
|
|
|
# represent IDs of modules, and values represent experiment
|
|
|
|
# IDs of new names to which the given modules should be moved.
|
|
|
|
# If a module with given ID doesn't exist (or experiment ID)
|
|
|
|
# it's obviously not updated. Any connection on module is destroyed.
|
2019-03-12 00:17:59 +08:00
|
|
|
def move_modules(to_move, current_user)
|
2016-08-19 19:53:43 +08:00
|
|
|
to_move.each do |id, experiment_id|
|
2022-03-09 21:12:16 +08:00
|
|
|
my_module = my_modules.find_by(id: id)
|
|
|
|
experiment = project.experiments.find_by(id: experiment_id)
|
2016-08-23 17:08:10 +08:00
|
|
|
next unless my_module.present? && experiment.present?
|
2016-08-19 19:53:43 +08:00
|
|
|
|
2020-06-15 16:20:10 +08:00
|
|
|
experiment_original = my_module.experiment
|
2016-08-23 17:08:10 +08:00
|
|
|
my_module.experiment = experiment
|
2016-08-19 19:53:43 +08:00
|
|
|
|
2016-08-23 17:08:10 +08:00
|
|
|
# Calculate new module position
|
|
|
|
new_pos = my_module.get_new_position
|
|
|
|
my_module.x = new_pos[:x]
|
|
|
|
my_module.y = new_pos[:y]
|
2016-08-19 19:53:43 +08:00
|
|
|
|
2022-03-09 21:12:16 +08:00
|
|
|
raise ActiveRecord::ActiveRecordError unless my_module.outputs.destroy_all && my_module.inputs.destroy_all
|
2016-08-23 17:08:10 +08:00
|
|
|
|
2019-03-12 00:17:59 +08:00
|
|
|
next unless my_module.save
|
2022-03-09 21:12:16 +08:00
|
|
|
|
2021-06-13 00:51:55 +08:00
|
|
|
# regenerate user assignments
|
|
|
|
my_module.user_assignments.destroy_all
|
|
|
|
UserAssignments::GenerateUserAssignmentsJob.perform_later(my_module, current_user)
|
2019-03-12 00:17:59 +08:00
|
|
|
|
|
|
|
Activities::CreateActivityService.call(activity_type: :move_task,
|
|
|
|
owner: current_user,
|
|
|
|
subject: my_module,
|
|
|
|
project: project,
|
|
|
|
team: project.team,
|
|
|
|
message_items: {
|
|
|
|
my_module: my_module.id,
|
|
|
|
experiment_original:
|
2020-06-15 16:20:10 +08:00
|
|
|
experiment_original.id,
|
2019-03-12 00:17:59 +08:00
|
|
|
experiment_new: experiment.id
|
|
|
|
})
|
2016-09-13 22:32:33 +08:00
|
|
|
end
|
2016-08-19 19:53:43 +08:00
|
|
|
end
|
|
|
|
|
2016-08-22 23:52:55 +08:00
|
|
|
# Move module groups; this method accepts a map where keys
|
|
|
|
# represent IDs of modules which are in module group,
|
|
|
|
# and values represent experiment
|
|
|
|
# IDs of new names to which the given module group should be moved.
|
|
|
|
# If a module with given ID doesn't exist (or experiment ID)
|
|
|
|
# it's obviously not updated. Position for entire module group is updated
|
|
|
|
# to bottom left corner.
|
2019-03-29 20:51:17 +08:00
|
|
|
def move_module_groups(to_move, current_user)
|
2021-11-16 18:10:15 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
to_move.each do |ids, experiment_id|
|
|
|
|
modules = my_modules.find(ids)
|
|
|
|
groups = Set.new(modules.map(&:my_module_group))
|
2022-03-09 21:12:16 +08:00
|
|
|
experiment = project.experiments.find_by(id: experiment_id)
|
2021-11-16 18:10:15 +08:00
|
|
|
|
|
|
|
groups.each do |group|
|
|
|
|
next unless group && experiment.present?
|
|
|
|
|
|
|
|
# Find the lowest point for current modules(max_y) and the leftmost
|
|
|
|
# module(min_x)
|
|
|
|
active_modules = experiment.my_modules.active
|
|
|
|
if active_modules.blank?
|
|
|
|
max_y = 0
|
|
|
|
min_x = 0
|
|
|
|
else
|
|
|
|
max_y = active_modules.maximum(:y) + MyModule::HEIGHT
|
|
|
|
min_x = active_modules.minimum(:x)
|
|
|
|
end
|
2016-08-22 23:52:55 +08:00
|
|
|
|
2021-11-16 18:10:15 +08:00
|
|
|
# Set new positions
|
|
|
|
curr_min_x = modules.min_by(&:x).x
|
|
|
|
curr_min_y = modules.min_by(&:y).y
|
|
|
|
modules.each { |m| m.x += -curr_min_x + min_x }
|
|
|
|
modules.each { |m| m.y += -curr_min_y + max_y }
|
|
|
|
|
|
|
|
modules.each do |m|
|
|
|
|
experiment_org = m.experiment
|
|
|
|
m.experiment = experiment
|
|
|
|
m.save!
|
|
|
|
|
|
|
|
# regenerate user assignments
|
|
|
|
m.user_assignments.destroy_all
|
|
|
|
UserAssignments::GenerateUserAssignmentsJob.new(m, current_user).perform_now
|
|
|
|
|
|
|
|
# Add activity
|
|
|
|
Activities::CreateActivityService.call(
|
|
|
|
activity_type: :move_task,
|
|
|
|
owner: current_user,
|
|
|
|
subject: m,
|
|
|
|
project: experiment.project,
|
|
|
|
team: experiment.project.team,
|
|
|
|
message_items: {
|
|
|
|
my_module: m.id,
|
|
|
|
experiment_original: experiment_org.id,
|
|
|
|
experiment_new: experiment.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2016-08-22 23:52:55 +08:00
|
|
|
|
2021-11-16 18:10:15 +08:00
|
|
|
group.experiment = experiment
|
|
|
|
group.save!
|
2016-08-22 23:52:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-25 17:25:10 +08:00
|
|
|
# Update connections for all modules in this project.
|
|
|
|
# Input is an array of arrays, where first element represents
|
|
|
|
# source node, and second element represents target node.
|
|
|
|
# Example input: [ [1, 2], [2, 3], [4, 5], [2, 5] ]
|
|
|
|
def update_module_connections(connections)
|
|
|
|
require 'rgl/base'
|
|
|
|
require 'rgl/adjacency'
|
|
|
|
require 'rgl/topsort'
|
|
|
|
dg = RGL::DirectedAdjacencyGraph.new
|
2017-10-05 22:19:25 +08:00
|
|
|
connections.each do |a, b|
|
2016-07-25 17:25:10 +08:00
|
|
|
# Check if both vertices exist
|
2022-03-09 21:12:16 +08:00
|
|
|
dg.add_edge(a, b) if (my_modules.find_all { |m| [a.to_i, b.to_i].include? m.id }).count == 2
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check if cycles exist!
|
|
|
|
topsort = dg.topsort_iterator.to_a
|
2022-03-09 21:12:16 +08:00
|
|
|
raise ArgumentError, 'Cycles exist.' if topsort.length.zero? && dg.edges.size > 1
|
2016-07-25 17:25:10 +08:00
|
|
|
|
|
|
|
# First, delete existing connections
|
|
|
|
# but keep a copy of previous state
|
|
|
|
previous_sources = {}
|
|
|
|
previous_sources.default = []
|
2017-10-05 22:19:25 +08:00
|
|
|
|
2022-03-09 21:12:16 +08:00
|
|
|
my_modules.includes(inputs: { from: [:inputs, outputs: :to] }).find_each do |m|
|
2016-07-25 17:25:10 +08:00
|
|
|
previous_sources[m.id] = []
|
|
|
|
m.inputs.each do |c|
|
|
|
|
previous_sources[m.id] << c.from
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-05 22:19:25 +08:00
|
|
|
# There are no callbacks in Connection, so delete_all should be safe
|
2017-12-20 18:44:11 +08:00
|
|
|
Connection.where(output_id: my_modules).delete_all
|
2016-07-25 17:25:10 +08:00
|
|
|
|
|
|
|
# Add new connections
|
|
|
|
filtered_edges = dg.edges.collect { |e| [e.source, e.target] }
|
|
|
|
filtered_edges.each do |a, b|
|
2017-10-05 22:19:25 +08:00
|
|
|
Connection.create!(input_id: b, output_id: a)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Save topological order of modules (for modules without workflow,
|
|
|
|
# leave them unordered)
|
2022-03-09 21:12:16 +08:00
|
|
|
my_modules.includes(:my_module_group).find_each do |m|
|
2017-10-05 22:19:25 +08:00
|
|
|
m.workflow_order =
|
|
|
|
if topsort.include? m.id.to_s
|
|
|
|
topsort.find_index(m.id.to_s)
|
|
|
|
else
|
|
|
|
-1
|
|
|
|
end
|
2016-07-25 17:25:10 +08:00
|
|
|
m.save!
|
|
|
|
end
|
|
|
|
|
2017-10-05 22:19:25 +08:00
|
|
|
# Make sure to reload my modules, which now have updated connections
|
|
|
|
my_modules.reload
|
2016-07-25 17:25:10 +08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Updates positions of modules.
|
|
|
|
# Input is a map where keys are module IDs, and values are
|
|
|
|
# hashes like { x: <x>, y: <y> }.
|
|
|
|
def update_module_positions(positions)
|
2017-10-05 22:19:25 +08:00
|
|
|
modules = my_modules.where(id: positions.keys)
|
|
|
|
modules.each do |m|
|
2020-10-16 20:21:25 +08:00
|
|
|
m.update(x: positions[m.id.to_s][:x], y: positions[m.id.to_s][:y])
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
2017-10-05 22:19:25 +08:00
|
|
|
my_modules.reload
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Normalize module positions in this project.
|
|
|
|
def normalize_module_positions
|
|
|
|
# This method normalizes module positions so x-s and y-s
|
|
|
|
# are all positive
|
2021-02-03 21:58:04 +08:00
|
|
|
x_diff = my_modules.active.pluck(:x).min
|
|
|
|
y_diff = my_modules.active.pluck(:y).min
|
2016-07-25 17:25:10 +08:00
|
|
|
|
2021-02-26 23:29:56 +08:00
|
|
|
return unless x_diff && y_diff
|
|
|
|
|
2021-02-19 21:41:57 +08:00
|
|
|
moving_direction = {
|
|
|
|
x: x_diff.positive? ? :asc : :desc,
|
|
|
|
y: y_diff.positive? ? :asc : :desc
|
|
|
|
}
|
|
|
|
my_modules.active.order(moving_direction).each do |m|
|
2021-02-03 21:58:04 +08:00
|
|
|
m.update!(x: m.x - x_diff, y: m.y - y_diff)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
2021-02-03 21:58:04 +08:00
|
|
|
my_modules.reload
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Recalculate module groups in this project. Input is
|
|
|
|
# a hash of module ids and their corresponding module names.
|
2017-10-05 22:19:25 +08:00
|
|
|
def update_module_groups(current_user)
|
2016-07-25 17:25:10 +08:00
|
|
|
require 'rgl/base'
|
|
|
|
require 'rgl/adjacency'
|
|
|
|
require 'rgl/connected_components'
|
|
|
|
|
|
|
|
dg = RGL::DirectedAdjacencyGraph[]
|
|
|
|
group_ids = Set.new
|
2022-03-09 21:12:16 +08:00
|
|
|
my_modules.active.includes(:my_module_group, outputs: :to).find_each do |m|
|
|
|
|
group_ids << m.my_module_group.id if m.my_module_group.present?
|
2017-10-05 22:19:25 +08:00
|
|
|
dg.add_vertex m.id unless dg.has_vertex? m.id
|
2016-07-25 17:25:10 +08:00
|
|
|
m.outputs.each do |o|
|
|
|
|
dg.add_edge m.id, o.to.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
workflows = []
|
2017-10-05 22:19:25 +08:00
|
|
|
dg.to_undirected.each_connected_component do |w|
|
|
|
|
workflows << my_modules.find(w)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Remove any existing module groups from modules
|
2022-03-09 21:12:16 +08:00
|
|
|
raise ActiveRecord::ActiveRecordError unless MyModuleGroup.where(id: group_ids.to_a).destroy_all
|
2016-07-25 17:25:10 +08:00
|
|
|
|
|
|
|
# Second, create new groups
|
2017-10-05 22:19:25 +08:00
|
|
|
workflows.each do |modules|
|
2016-07-25 17:25:10 +08:00
|
|
|
# Single modules are not considered part of any workflow
|
2017-10-05 22:19:25 +08:00
|
|
|
next unless modules.length > 1
|
2022-03-09 21:12:16 +08:00
|
|
|
|
2017-10-05 22:19:25 +08:00
|
|
|
MyModuleGroup.create!(experiment: self,
|
|
|
|
my_modules: modules,
|
|
|
|
created_by: current_user)
|
2016-07-25 17:25:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
my_module_groups.reload
|
|
|
|
true
|
|
|
|
end
|
2016-08-11 15:19:12 +08:00
|
|
|
|
2022-11-23 21:42:02 +08:00
|
|
|
private
|
|
|
|
|
2019-03-18 22:03:14 +08:00
|
|
|
def log_activity(type_of, current_user, my_module)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
team: my_module.experiment.project.team,
|
|
|
|
project: my_module.experiment.project,
|
|
|
|
subject: my_module,
|
|
|
|
message_items: { my_module: my_module.id })
|
|
|
|
end
|
2016-07-22 16:43:55 +08:00
|
|
|
end
|