2020-10-14 18:12:32 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class Report < ApplicationRecord
|
2022-10-28 20:47:43 +08:00
|
|
|
ID_PREFIX = 'RP'
|
2022-12-05 17:17:15 +08:00
|
|
|
include PrefixedIdModel
|
2022-10-28 20:47:43 +08:00
|
|
|
SEARCHABLE_ATTRIBUTES = ['reports.name', 'reports.description', PREFIXED_ID_SQL].freeze
|
|
|
|
|
2021-04-08 23:40:16 +08:00
|
|
|
include SettingsModel
|
2022-05-23 19:32:15 +08:00
|
|
|
include Assignable
|
|
|
|
include PermissionCheckableModel
|
2016-02-12 23:52:43 +08:00
|
|
|
include SearchableModel
|
2019-03-26 00:35:54 +08:00
|
|
|
include SearchableByNameModel
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-05-17 21:07:45 +08:00
|
|
|
enum pdf_file_status: { pdf_empty: 0, pdf_processing: 1, pdf_ready: 2, pdf_error: 3 }
|
|
|
|
enum docx_file_status: { docx_empty: 0, docx_processing: 1, docx_ready: 2, docx_error: 3 }
|
2021-05-11 19:28:28 +08:00
|
|
|
|
2021-03-15 20:30:03 +08:00
|
|
|
# ActiveStorage configuration
|
|
|
|
has_one_attached :pdf_file
|
|
|
|
has_one_attached :docx_file
|
2021-05-25 21:14:50 +08:00
|
|
|
has_one_attached :docx_preview_file
|
2021-03-15 20:30:03 +08:00
|
|
|
|
2016-09-21 21:35:23 +08:00
|
|
|
auto_strip_attributes :name, :description, nullify: false
|
2016-02-12 23:52:43 +08:00
|
|
|
validates :name,
|
2016-10-05 23:45:20 +08:00
|
|
|
length: { minimum: Constants::NAME_MIN_LENGTH,
|
2021-05-12 20:56:22 +08:00
|
|
|
maximum: Constants::NAME_MAX_LENGTH }
|
2016-10-05 23:45:20 +08:00
|
|
|
validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH }
|
2016-02-12 23:52:43 +08:00
|
|
|
validates :project, presence: true
|
|
|
|
validates :user, presence: true
|
|
|
|
|
2019-07-26 18:40:36 +08:00
|
|
|
belongs_to :project, inverse_of: :reports
|
|
|
|
belongs_to :user, inverse_of: :reports
|
2018-04-18 22:47:52 +08:00
|
|
|
belongs_to :team, inverse_of: :reports
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :last_modified_by,
|
|
|
|
foreign_key: 'last_modified_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2022-05-23 19:32:15 +08:00
|
|
|
has_many :users, through: :user_assignments
|
2021-04-06 19:56:24 +08:00
|
|
|
has_many :report_template_values, dependent: :destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Report either has many report elements (if grouped by timestamp),
|
|
|
|
# or many module elements (if grouped by module)
|
2021-05-27 22:31:41 +08:00
|
|
|
has_many :report_elements, inverse_of: :report, dependent: :delete_all
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-04-08 23:40:16 +08:00
|
|
|
DEFAULT_SETTINGS = {
|
|
|
|
all_tasks: true,
|
|
|
|
task: {
|
|
|
|
protocol: {
|
|
|
|
description: true,
|
|
|
|
completed_steps: true,
|
|
|
|
uncompleted_steps: true,
|
2022-06-01 21:53:40 +08:00
|
|
|
step_texts: true,
|
2021-04-08 23:40:16 +08:00
|
|
|
step_checklists: true,
|
|
|
|
step_files: true,
|
|
|
|
step_tables: true,
|
2023-05-10 22:15:14 +08:00
|
|
|
step_comments: true,
|
|
|
|
step_well_plates: true
|
2021-04-08 23:40:16 +08:00
|
|
|
},
|
2021-05-13 21:44:06 +08:00
|
|
|
file_results: true,
|
2021-04-08 23:40:16 +08:00
|
|
|
file_results_previews: false,
|
|
|
|
table_results: true,
|
|
|
|
text_results: true,
|
|
|
|
result_comments: true,
|
2022-12-02 00:13:26 +08:00
|
|
|
result_order: 'new',
|
2022-09-13 15:59:11 +08:00
|
|
|
activities: true,
|
|
|
|
repositories: []
|
2021-04-08 23:40:16 +08:00
|
|
|
}
|
|
|
|
}.freeze
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def self.search(
|
|
|
|
user,
|
|
|
|
include_archived,
|
|
|
|
query = nil,
|
2017-05-05 22:41:23 +08:00
|
|
|
page = 1,
|
|
|
|
_current_team = nil,
|
|
|
|
options = {}
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
|
|
|
|
2021-10-22 17:43:20 +08:00
|
|
|
project_ids = Project.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
|
|
|
.pluck(:id)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-10-22 17:43:20 +08:00
|
|
|
new_query = Report.distinct
|
|
|
|
.where(reports: { project_id: project_ids })
|
2022-10-28 20:47:43 +08:00
|
|
|
.where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Show all results if needed
|
2016-10-05 23:45:20 +08:00
|
|
|
if page == Constants::SEARCH_NO_LIMIT
|
2016-02-12 23:52:43 +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-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-26 00:35:54 +08:00
|
|
|
def self.viewable_by_user(user, teams)
|
2022-10-20 21:35:43 +08:00
|
|
|
with_granted_permissions(user, ReportPermissions::READ)
|
|
|
|
.where(project: Project.viewable_by_user(user, teams))
|
2018-12-13 18:18:09 +08:00
|
|
|
end
|
|
|
|
|
2022-07-07 18:00:35 +08:00
|
|
|
def self.filter_by_teams(teams = [])
|
|
|
|
teams.blank? ? self : where(team: teams)
|
|
|
|
end
|
|
|
|
|
2022-06-07 00:21:57 +08:00
|
|
|
def created_by
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def permission_parent
|
|
|
|
team
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def root_elements
|
2021-05-27 22:31:41 +08:00
|
|
|
report_elements.active.where(parent: nil).order(:position)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-09-17 03:00:09 +08:00
|
|
|
def self.generate_whole_project_report(project, current_user, current_team)
|
2021-05-24 19:16:43 +08:00
|
|
|
content = {
|
2021-05-27 18:44:45 +08:00
|
|
|
'experiments' => [],
|
2021-06-16 22:17:12 +08:00
|
|
|
'repositories' => project.assigned_repositories_and_snapshots.pluck(:id)
|
2021-05-24 19:16:43 +08:00
|
|
|
}
|
|
|
|
project.experiments.includes(:my_modules).each do |experiment|
|
2021-06-22 20:50:47 +08:00
|
|
|
content['experiments'].push(
|
|
|
|
{ id: experiment.id, my_module_ids: experiment.my_module_ids }
|
|
|
|
)
|
2021-05-24 19:16:43 +08:00
|
|
|
end
|
2018-09-17 03:00:09 +08:00
|
|
|
|
2018-10-12 11:59:45 +08:00
|
|
|
report = Report.new
|
|
|
|
report.name = loop do
|
2018-09-17 03:00:09 +08:00
|
|
|
dummy_name = SecureRandom.hex(10)
|
2021-06-08 20:21:34 +08:00
|
|
|
break dummy_name unless Report.exists?(name: dummy_name)
|
2018-09-17 03:00:09 +08:00
|
|
|
end
|
|
|
|
report.project = project
|
|
|
|
report.user = current_user
|
|
|
|
report.team = current_team
|
|
|
|
report.last_modified_by = current_user
|
2021-05-24 19:16:43 +08:00
|
|
|
ReportActions::ReportContent.new(report, content, {}, current_user).save_with_content
|
2018-09-17 03:00:09 +08:00
|
|
|
report
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|