2021-04-20 19:35:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ReportActions
|
|
|
|
class ReportContent
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
2021-06-17 22:23:27 +08:00
|
|
|
include ReportsHelper
|
2021-04-20 19:35:40 +08:00
|
|
|
|
|
|
|
MY_MODULE_ADDONS_ELEMENTS = []
|
|
|
|
|
|
|
|
def initialize(report, content, template_values, user)
|
|
|
|
@content = content
|
|
|
|
@settings = report.settings
|
|
|
|
@user = user
|
|
|
|
@element_position = 0
|
|
|
|
@report = report
|
|
|
|
@template_values = template_values
|
2022-09-13 15:59:11 +08:00
|
|
|
@repositories = @settings.dig(:task, :repositories)
|
2021-04-20 19:35:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def save_with_content
|
|
|
|
Report.transaction do
|
|
|
|
# Save the report itself
|
|
|
|
@report.save!
|
|
|
|
|
|
|
|
# Delete existing report elements
|
|
|
|
@report.report_elements.destroy_all
|
|
|
|
|
|
|
|
# Save new report elements
|
2021-05-06 16:53:40 +08:00
|
|
|
begin
|
|
|
|
generate_content
|
|
|
|
rescue StandardError => e
|
|
|
|
@report.errors.add(:content_error, I18n.t('projects.reports.index.generation.content_generation_error'))
|
|
|
|
Rails.logger.error e.message
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
2021-04-20 19:35:40 +08:00
|
|
|
|
|
|
|
# Delete existing template values
|
|
|
|
@report.report_template_values.destroy_all
|
|
|
|
|
2021-05-03 19:03:26 +08:00
|
|
|
if @template_values.present?
|
|
|
|
formatted_template_values = @template_values.as_json.map { |k, v| v['name'] = k; v }
|
2021-05-31 17:51:04 +08:00
|
|
|
formatted_template_values.each do |template_value|
|
|
|
|
if template_value['view_component'] == 'DateInputComponent'
|
|
|
|
template_value['value'] =
|
|
|
|
Date.strptime(template_value['value'], I18n.backend.date_format.dup.gsub(/%-/, '%')).iso8601
|
|
|
|
end
|
|
|
|
end
|
2021-05-03 19:03:26 +08:00
|
|
|
# Save new template values
|
|
|
|
@report.report_template_values.create!(formatted_template_values)
|
|
|
|
end
|
2021-05-06 16:53:40 +08:00
|
|
|
rescue ActiveRecord::ActiveRecordError, ArgumentError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
raise ActiveRecord::Rollback
|
2021-04-20 19:35:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_content
|
2021-06-14 18:42:18 +08:00
|
|
|
save_element!({ 'project_id' => @report.project_id }, :project_header, nil)
|
|
|
|
|
2021-06-10 16:59:34 +08:00
|
|
|
@content['experiments'].each do |experiment|
|
|
|
|
generate_experiment_content(experiment[:id], experiment[:my_module_ids])
|
2021-04-20 19:35:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-10 16:59:34 +08:00
|
|
|
def generate_experiment_content(experiment_id, my_module_ids)
|
|
|
|
experiment = Experiment.find_by(id: experiment_id)
|
2021-10-01 20:18:40 +08:00
|
|
|
return unless experiment && can_read_experiment?(@user, experiment)
|
2021-04-20 19:35:40 +08:00
|
|
|
|
2021-05-06 16:53:40 +08:00
|
|
|
experiment_element = save_element!({ 'experiment_id' => experiment.id }, :experiment, nil)
|
2021-06-10 16:59:34 +08:00
|
|
|
generate_my_modules_content(experiment, experiment_element, my_module_ids)
|
2021-04-20 19:35:40 +08:00
|
|
|
end
|
|
|
|
|
2021-06-10 16:59:34 +08:00
|
|
|
def generate_my_modules_content(experiment, experiment_element, my_module_ids)
|
2021-04-20 19:35:40 +08:00
|
|
|
my_modules = experiment.my_modules
|
|
|
|
.active
|
2021-06-10 16:59:34 +08:00
|
|
|
.where(id: my_module_ids)
|
|
|
|
my_modules.sort_by { |m| my_module_ids.index m.id }.each do |my_module|
|
2021-10-01 20:18:40 +08:00
|
|
|
next unless can_read_my_module?(@user, my_module)
|
|
|
|
|
2021-05-06 16:53:40 +08:00
|
|
|
my_module_element = save_element!({ 'my_module_id' => my_module.id }, :my_module, experiment_element)
|
2021-04-28 15:54:00 +08:00
|
|
|
|
2022-04-25 19:45:58 +08:00
|
|
|
my_module.live_and_snapshot_repositories_list.each do |repository|
|
2022-09-13 15:59:11 +08:00
|
|
|
next unless @repositories.include?(repository.id)
|
|
|
|
|
2021-05-06 16:53:40 +08:00
|
|
|
save_element!(
|
2021-04-20 19:35:40 +08:00
|
|
|
{ 'my_module_id' => my_module.id, 'repository_id' => repository.id },
|
|
|
|
:my_module_repository,
|
|
|
|
my_module_element
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-10 16:59:34 +08:00
|
|
|
def save_element!(references, type_of, parent)
|
2021-04-20 19:35:40 +08:00
|
|
|
el = ReportElement.new
|
|
|
|
el.position = @element_position
|
|
|
|
el.report = @report
|
|
|
|
el.parent = parent
|
|
|
|
el.type_of = type_of
|
2021-06-10 16:59:34 +08:00
|
|
|
el.assign_attributes(references)
|
2021-04-20 19:35:40 +08:00
|
|
|
el.save!
|
|
|
|
|
|
|
|
@element_position += 1
|
|
|
|
|
|
|
|
el
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|