2019-09-12 15:02:45 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
module ReportsHelper
|
2018-09-20 13:05:52 +08:00
|
|
|
include StringUtility
|
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
def render_new_element(hide)
|
|
|
|
render partial: 'reports/elements/new_element.html.erb',
|
|
|
|
locals: { hide: hide }
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
def render_report_element(element, provided_locals = nil)
|
2018-10-12 04:35:03 +08:00
|
|
|
# Determine partial
|
|
|
|
file_name = element.type_of
|
|
|
|
if element.type_of.in? ReportExtends::MY_MODULE_CHILDREN_ELEMENTS
|
|
|
|
file_name = "my_module_#{element.type_of.singularize}"
|
|
|
|
end
|
|
|
|
view = "reports/elements/#{file_name}_element.html.erb"
|
|
|
|
|
|
|
|
# Set locals
|
|
|
|
|
|
|
|
locals = provided_locals.nil? ? {} : provided_locals.clone
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2018-10-12 04:35:03 +08:00
|
|
|
children_html = ''.html_safe
|
2017-03-08 21:14:03 +08:00
|
|
|
# First, recursively render element's children
|
|
|
|
if element.comments? || element.project_header?
|
|
|
|
# Render no children
|
|
|
|
elsif element.result?
|
|
|
|
# Special handling for result comments
|
|
|
|
if element.has_children?
|
|
|
|
children_html.safe_concat render_new_element(true)
|
|
|
|
element.children.each do |child|
|
2017-03-08 21:41:42 +08:00
|
|
|
children_html
|
|
|
|
.safe_concat render_report_element(child, provided_locals)
|
2017-03-08 21:14:03 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
children_html.safe_concat render_new_element(false)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
2017-03-08 21:14:03 +08:00
|
|
|
if element.has_children?
|
|
|
|
element.children.each do |child|
|
|
|
|
children_html.safe_concat render_new_element(false)
|
2017-03-08 21:41:42 +08:00
|
|
|
children_html
|
|
|
|
.safe_concat render_report_element(child, provided_locals)
|
2017-03-08 21:14:03 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2017-03-08 21:14:03 +08:00
|
|
|
children_html.safe_concat render_new_element(false)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2017-03-08 21:14:03 +08:00
|
|
|
locals[:children] = children_html
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2018-09-18 11:40:43 +08:00
|
|
|
if provided_locals[:export_all]
|
2018-10-12 04:35:03 +08:00
|
|
|
# Set path and filename locals for files and tables in export all ZIP
|
2018-09-12 02:18:33 +08:00
|
|
|
|
2018-09-18 11:40:43 +08:00
|
|
|
if element['type_of'] == 'my_module_repository'
|
2018-10-15 11:54:19 +08:00
|
|
|
obj_id = element[:repository_id]
|
2018-09-18 11:40:43 +08:00
|
|
|
elsif element['type_of'].in? %w(step_asset step_table result_asset
|
2018-09-20 13:05:52 +08:00
|
|
|
result_table)
|
2018-08-29 12:56:47 +08:00
|
|
|
|
2018-09-18 11:40:43 +08:00
|
|
|
parent_el = ReportElement.find(element['parent_id'])
|
|
|
|
parent_type = parent_el[:type_of]
|
|
|
|
parent = parent_type.singularize.classify.constantize
|
|
|
|
.find(parent_el["#{parent_type}_id"])
|
|
|
|
|
|
|
|
if parent.class == Step
|
2018-10-15 11:54:19 +08:00
|
|
|
obj_id = if element['type_of'] == 'step_asset'
|
|
|
|
element[:asset_id]
|
|
|
|
elsif element['type_of'] == 'step_table'
|
|
|
|
element[:table_id]
|
|
|
|
end
|
2018-10-12 04:35:03 +08:00
|
|
|
elsif parent.class == MyModule
|
2018-10-15 11:54:19 +08:00
|
|
|
result = Result.find(element[:result_id])
|
|
|
|
obj_id = if element['type_of'] == 'result_asset'
|
|
|
|
result.asset.id
|
|
|
|
elsif element['type_of'] == 'result_table'
|
|
|
|
result.table.id
|
|
|
|
end
|
2018-09-18 11:40:43 +08:00
|
|
|
end
|
2018-10-15 11:54:19 +08:00
|
|
|
end
|
2018-09-18 11:40:43 +08:00
|
|
|
|
2018-10-15 11:54:19 +08:00
|
|
|
if obj_id
|
|
|
|
locals[:path] =
|
|
|
|
provided_locals[:obj_filenames][element['type_of'].to_sym][obj_id]
|
2018-10-15 12:52:39 +08:00
|
|
|
.sub(%r{/usr/src/app/tmp/temp-zip-\d+/}, '')
|
2018-10-15 11:54:19 +08:00
|
|
|
locals[:filename] = locals[:path].split('/').last
|
2018-09-18 11:40:43 +08:00
|
|
|
end
|
2018-08-29 12:56:47 +08:00
|
|
|
end
|
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
# ReportExtends is located in config/initializers/extends/report_extends.rb
|
|
|
|
ReportElement.type_ofs.keys.each do |type|
|
2017-03-24 23:25:50 +08:00
|
|
|
next unless element.public_send("#{type}?")
|
2019-09-12 15:02:45 +08:00
|
|
|
|
2017-06-05 18:24:06 +08:00
|
|
|
element.element_references.each do |el_ref|
|
|
|
|
locals[el_ref.class.name.underscore.to_sym] = el_ref
|
|
|
|
end
|
2019-09-12 15:02:45 +08:00
|
|
|
locals[:order] = element.sort_order if type.in? ReportExtends::SORTED_ELEMENTS
|
2017-03-08 21:14:03 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
(render partial: view, locals: locals).html_safe
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-03-08 21:14:03 +08:00
|
|
|
# "Hack" to omit file preview URL because of WKHTML issues
|
2019-10-17 19:30:14 +08:00
|
|
|
def report_image_asset_url(asset)
|
|
|
|
image_tag(asset.medium_preview
|
|
|
|
.processed
|
|
|
|
.service_url(expires_in: Constants::URL_LONG_EXPIRE_TIME))
|
2017-03-08 21:14:03 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-03-08 21:41:42 +08:00
|
|
|
# "Hack" to load Glyphicons css directly from the CDN
|
|
|
|
# site so they work in report
|
2017-03-08 21:14:03 +08:00
|
|
|
def bootstrap_cdn_link_tag
|
2017-03-08 21:41:42 +08:00
|
|
|
specs = Gem.loaded_specs['bootstrap-sass']
|
|
|
|
return '' unless specs.present?
|
2019-09-12 15:02:45 +08:00
|
|
|
|
2017-03-08 21:41:42 +08:00
|
|
|
stylesheet_link_tag("http://netdna.bootstrapcdn.com/bootstrap/" \
|
|
|
|
"#{specs.version.version}/css/bootstrap.min.css",
|
|
|
|
media: 'all')
|
2017-03-08 21:14:03 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2020-07-14 15:44:03 +08:00
|
|
|
def font_awesome_cdn_link_tag
|
|
|
|
stylesheet_link_tag(
|
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/fontawesome.min.css',
|
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/regular.min.css',
|
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/solid.min.css'
|
|
|
|
)
|
2017-03-08 21:14:03 +08:00
|
|
|
end
|
2017-05-05 15:45:18 +08:00
|
|
|
|
2020-05-27 18:15:19 +08:00
|
|
|
def assigned_repository_or_snapshot(my_module, element_id, snapshot, repository)
|
|
|
|
if element_id
|
|
|
|
repository = Repository.accessible_by_teams(my_module.experiment.project.team).find_by(id: element_id)
|
2020-06-03 16:45:53 +08:00
|
|
|
# Check for default set snapshots when repository still exists
|
|
|
|
if repository
|
|
|
|
selected_snapshot = repository.repository_snapshots.where(my_module: my_module).find_by(selected: true)
|
|
|
|
repository = selected_snapshot if selected_snapshot
|
|
|
|
end
|
2020-05-29 22:30:27 +08:00
|
|
|
repository ||= RepositorySnapshot.joins(my_module: { experiment: :project })
|
|
|
|
.where(my_module: { experiments: { project: my_module.experiment.project } })
|
|
|
|
.find_by(id: element_id)
|
2020-05-27 18:15:19 +08:00
|
|
|
end
|
|
|
|
repository || snapshot
|
2020-05-22 01:25:28 +08:00
|
|
|
end
|
|
|
|
|
2020-06-04 04:57:38 +08:00
|
|
|
def assigned_repositories_in_project_list(project)
|
2020-06-29 17:43:23 +08:00
|
|
|
live_repositories = Repository.assigned_to_project(project)
|
2020-07-09 20:50:01 +08:00
|
|
|
snapshots = RepositorySnapshot.of_unassigned_from_project(project)
|
2020-06-04 04:57:38 +08:00
|
|
|
|
|
|
|
snapshots.each { |snapshot| snapshot.name = "#{snapshot.name} #{t('projects.reports.index.deleted')}" }
|
|
|
|
(live_repositories + snapshots).sort_by { |r| r.name.downcase }
|
|
|
|
end
|
|
|
|
|
2017-05-05 15:45:18 +08:00
|
|
|
def step_status_label(step)
|
|
|
|
if step.completed
|
|
|
|
style = 'success'
|
|
|
|
text = t('protocols.steps.completed')
|
|
|
|
else
|
|
|
|
style = 'default'
|
|
|
|
text = t('protocols.steps.uncompleted')
|
|
|
|
end
|
2020-07-23 20:24:30 +08:00
|
|
|
"<span class=\"label step-label-#{style}\">[#{text}]</span>".html_safe
|
2017-05-05 15:45:18 +08:00
|
|
|
end
|
2017-06-22 22:40:50 +08:00
|
|
|
|
2017-06-27 17:26:32 +08:00
|
|
|
# Fixes issues with avatar images in reports
|
|
|
|
def fix_smart_annotation_image(html)
|
|
|
|
html_doc = Nokogiri::HTML(html)
|
|
|
|
html_doc.search('.atwho-user-popover').each do |el|
|
|
|
|
text = el.content
|
|
|
|
el.replace("<a href='#' style='margin-left: 5px'>#{text}</a>")
|
|
|
|
end
|
|
|
|
html_doc.search('[ref="missing-img"]').each do |el|
|
|
|
|
tag = wicked_pdf_image_tag('icon_small/missing.png')
|
|
|
|
el.replace(tag)
|
|
|
|
end
|
|
|
|
html_doc.to_s
|
2017-06-22 22:40:50 +08:00
|
|
|
end
|
2018-10-12 04:35:03 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-10-15 07:12:25 +08:00
|
|
|
def obj_name_to_filename(obj, filename_suffix = '')
|
2018-10-12 04:35:03 +08:00
|
|
|
obj_name = if obj.class == Asset
|
2019-07-09 16:28:15 +08:00
|
|
|
obj_name, extension = obj.file_name.split('.')
|
2018-10-15 07:12:25 +08:00
|
|
|
extension&.prepend('.')
|
2018-10-12 04:35:03 +08:00
|
|
|
obj_name
|
|
|
|
elsif obj.class.in? [Table, Result, Repository]
|
2018-10-15 07:12:25 +08:00
|
|
|
extension = '.csv'
|
2018-10-12 04:35:03 +08:00
|
|
|
obj.name.present? ? obj.name : obj.class.name
|
|
|
|
end
|
2018-10-15 07:12:25 +08:00
|
|
|
obj_name = to_filesystem_name(obj_name)
|
|
|
|
obj_name + "#{filename_suffix}#{extension}"
|
2018-10-12 04:35:03 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|