2021-01-28 19:57:04 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ExperimentsOverviewService
|
|
|
|
def initialize(project, user, params)
|
|
|
|
@project = project
|
|
|
|
@user = user
|
|
|
|
@params = params
|
|
|
|
@view_state = @project.current_view_state(@user)
|
|
|
|
@view_mode = if @project.archived?
|
|
|
|
'archived'
|
|
|
|
else
|
|
|
|
params[:view_mode] || 'active'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update sort if changed
|
|
|
|
@sort = @view_state.state.dig('experiments', @view_mode, 'sort') || 'atoz'
|
|
|
|
if @params[:sort] && @sort != @params[:sort] &&
|
|
|
|
%w(new old atoz ztoa archived_old archived_new).include?(@params[:sort])
|
|
|
|
@view_state.state['experiments'].merge!(Hash[@view_mode, { 'sort': @params[:sort] }.stringify_keys])
|
|
|
|
@view_state.save!
|
|
|
|
@sort = @view_state.state.dig('experiments', @view_mode, 'sort')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def experiments
|
|
|
|
sort_records(
|
|
|
|
filter_records(
|
|
|
|
fetch_records
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_records
|
2021-02-17 23:50:09 +08:00
|
|
|
@project.experiments
|
|
|
|
.joins(:project)
|
|
|
|
.joins('LEFT OUTER JOIN my_modules AS active_tasks ON active_tasks.experiment_id = experiments.id ' \
|
|
|
|
'AND active_tasks.archived = FALSE')
|
|
|
|
.joins('LEFT OUTER JOIN my_modules AS active_completed_tasks ON active_completed_tasks.experiment_id '\
|
|
|
|
'= experiments.id AND active_completed_tasks.archived = FALSE AND active_completed_tasks.state = 1')
|
2021-02-10 21:50:42 +08:00
|
|
|
.select('experiments.*')
|
2021-02-17 23:50:09 +08:00
|
|
|
.select('COUNT(DISTINCT active_tasks.id) AS task_count')
|
|
|
|
.select('COUNT(DISTINCT active_completed_tasks.id) AS completed_task_count')
|
|
|
|
.group('experiments.id')
|
2021-01-28 19:57:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_records(records)
|
|
|
|
records = records.archived if @view_mode == 'archived' && @project.active?
|
|
|
|
records = records.active if @view_mode == 'active'
|
|
|
|
if @params[:search].present?
|
2021-06-18 20:38:07 +08:00
|
|
|
records = records.where_attributes_like(
|
|
|
|
["experiments.name", "experiments.description", "('EX' || experiments.id)"],
|
|
|
|
@params[:search]
|
|
|
|
)
|
2021-01-28 19:57:04 +08:00
|
|
|
end
|
|
|
|
if @params[:created_on_from].present?
|
|
|
|
records = records.where('experiments.created_at > ?', @params[:created_on_from])
|
|
|
|
end
|
|
|
|
records = records.where('experiments.created_at < ?', @params[:created_on_to]) if @params[:created_on_to].present?
|
2021-02-02 18:17:29 +08:00
|
|
|
if @params[:updated_on_from].present?
|
|
|
|
records = records.where('experiments.updated_at > ?', @params[:updated_on_from])
|
|
|
|
end
|
|
|
|
records = records.where('experiments.updated_at < ?', @params[:updated_on_to]) if @params[:updated_on_to].present?
|
|
|
|
|
2021-01-28 19:57:04 +08:00
|
|
|
if @params[:archived_on_from].present?
|
|
|
|
records = records.where('COALESCE(experiments.archived_on, projects.archived_on) > ?', @params[:archived_on_from])
|
|
|
|
end
|
|
|
|
if @params[:archived_on_to].present?
|
|
|
|
records = records.where('COALESCE(experiments.archived_on, projects.archived_on) < ?', @params[:archived_on_to])
|
|
|
|
end
|
|
|
|
records
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_records(records)
|
|
|
|
case @sort
|
|
|
|
when 'new'
|
|
|
|
records.order(created_at: :desc)
|
|
|
|
when 'old'
|
|
|
|
records.order(:created_at)
|
|
|
|
when 'atoz'
|
|
|
|
records.order(:name)
|
|
|
|
when 'ztoa'
|
|
|
|
records.order(name: :desc)
|
|
|
|
when 'archived_old'
|
2021-02-18 16:42:17 +08:00
|
|
|
records.group('projects.archived_on')
|
|
|
|
.order(Arel.sql('COALESCE(experiments.archived_on, projects.archived_on) ASC'))
|
2021-01-28 19:57:04 +08:00
|
|
|
when 'archived_new'
|
2021-02-18 16:42:17 +08:00
|
|
|
records.group('projects.archived_on')
|
|
|
|
.order(Arel.sql('COALESCE(experiments.archived_on, projects.archived_on) DESC'))
|
2021-01-28 19:57:04 +08:00
|
|
|
else
|
|
|
|
records
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|