Merge pull request #3316 from okriuchykhin/ok_SCI_5702

Add generation error states to reports [SCI-5702]
This commit is contained in:
Miha Mencin 2021-05-17 10:16:06 +02:00 committed by GitHub
commit d952edd389
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 116 additions and 28 deletions

View file

@ -64,9 +64,16 @@
function renderDocxFile(data) {
if (data.error) {
let oldLink = '';
if (data.preview_url) {
oldLink = `<a href="#" class="file-preview-link docx" data-preview-url="${data.preview_url}">
(<i class="fas fa-file-docx"></i>
${I18n.t('projects.reports.index.previous_docx')})
</a>`;
}
return `<span class="processing-error">
<i class="fas fa-exclamation-triangle"></i>
${I18n.t('projects.reports.index.error')}
${I18n.t('projects.reports.index.error')}${oldLink}
</span>`;
}
@ -88,9 +95,16 @@
function renderPdfFile(data) {
if (data.error) {
let oldLink = '';
if (data.preview_url) {
oldLink = `<a href="#" class="file-preview-link pdf" data-preview-url="${data.preview_url}">
(<i class="fas fa-file-pdf"></i>
${I18n.t('projects.reports.index.previous_pdf')})
</a>`;
}
return `<span class="processing-error">
<i class="fas fa-exclamation-triangle"></i>
${I18n.t('projects.reports.index.error')}
${I18n.t('projects.reports.index.error')}${oldLink}
</span>`;
}

View file

@ -80,6 +80,17 @@
padding-top: 5px;
width: 30px;
}
.deliver-error {
background-color: $brand-danger;
border-radius: 50%;
color: $color-concrete;
display: inline-block;
font-size: $font-size-small;
height: 30px;
padding-top: 5px;
width: 30px;
}
}
.btn-more-notifications {

View file

@ -116,6 +116,17 @@
width: 45px;
}
.deliver-error {
background-color: $brand-danger;
border-radius: 50%;
color: $color-concrete;
display: block;
font-size: $font-size-h3;
height: 45px;
padding-top: 5px;
width: 45px;
}
.system-message {
background-color: $brand-success;
border-radius: 50%;

View file

@ -87,7 +87,6 @@ class ReportsController < ApplicationController
@report.project = @project
@report.user = current_user
@report.team = current_team
@report.pdf_file_processing = true
@report.settings = report_params[:settings]
@report.last_modified_by = current_user
@ -99,6 +98,7 @@ class ReportsController < ApplicationController
).save_with_content
if @report.errors.blank?
@report.pdf_processing!
log_activity(:create_report)
flash[:success] = t('projects.reports.index.generation.accepted_message')
@ -125,7 +125,6 @@ class ReportsController < ApplicationController
# Updating existing report from the _save modal of the new page
def update
@report.last_modified_by = current_user
@report.pdf_file_processing = true
@report.assign_attributes(report_params)
ReportActions::ReportContent.new(
@ -136,6 +135,7 @@ class ReportsController < ApplicationController
).save_with_content
if @report.errors.blank?
@report.pdf_processing!
log_activity(:edit_report)
flash[:success] = t('projects.reports.index.generation.accepted_message')
@ -173,14 +173,14 @@ class ReportsController < ApplicationController
format.json do
render json: {
docx: {
processing: @report.docx_file_processing,
processing: @report.docx_processing?,
preview_url: docx,
error: false
error: @report.docx_error?
},
pdf: {
processing: @report.pdf_file_processing,
processing: @report.pdf_processing?,
preview_url: pdf,
error: false
error: @report.pdf_error?
}
}
end
@ -191,7 +191,7 @@ class ReportsController < ApplicationController
def generate_pdf
respond_to do |format|
format.json do
@report.update!(pdf_file_processing: true)
@report.pdf_processing!
log_activity(:generate_pdf_report)
Reports::PdfJob.perform_later(@report.id, current_user)
render json: {
@ -204,7 +204,7 @@ class ReportsController < ApplicationController
def generate_docx
respond_to do |format|
format.json do
@report.update!(docx_file_processing: true)
@report.docx_processing!
log_activity(:generate_docx_report)
Reports::DocxJob.perform_later(@report, current_user, current_team, root_url)
render json: {

View file

@ -35,11 +35,11 @@ class ReportDatatable < CustomDatatable
when 'reports.docx_file'
records.left_joins(:docx_file_attachment)
.order(active_storage_attachments: sort_direction(order_params))
.order(docx_file_processing: sort_direction(order_params) == 'ASC' ? :desc : :asc)
.order(docx_file_status: sort_direction(order_params) == 'ASC' ? :desc : :asc)
when 'reports.pdf_file'
records.left_joins(:pdf_file_attachment)
.order(active_storage_attachments: sort_direction(order_params))
.order(pdf_file_processing: sort_direction(order_params) == 'ASC' ? :desc : :asc)
.order(pdf_file_status: sort_direction(order_params) == 'ASC' ? :desc : :asc)
else
sort_by = "#{sort_column(order_params)} #{sort_direction(order_params)}"
records.order(sort_by)
@ -72,18 +72,18 @@ class ReportDatatable < CustomDatatable
def docx_file(report)
docx = document_preview_report_path(report, report_type: :docx) if report.docx_file.attached?
{
processing: report.docx_file_processing,
processing: report.docx_processing?,
preview_url: docx,
error: false
error: report.docx_error?
}
end
def pdf_file(report)
pdf = document_preview_report_path(report, report_type: :pdf) if report.pdf_file.attached?
{
processing: report.pdf_file_processing,
processing: report.pdf_processing?,
preview_url: pdf,
error: false
error: report.pdf_error?
}
end

View file

@ -2,16 +2,35 @@
module Reports
class DocxJob < ApplicationJob
extend InputSanitizeHelper
include InputSanitizeHelper
queue_as :reports
discard_on StandardError do |job, error|
report = Report.find_by(id: job.arguments.first)
return if report.blank?
ActiveRecord::Base.no_touching do
report&.update(docx_file_processing: false)
report.docx_error!
end
Rails.logger.error("Couldn't generate DOCX for Report with id: #{job.arguments.first}. Error:\n #{error}")
report_path =
if report.docx_file.attached?
Rails.application.routes.url_helpers
.reports_path(team: report.team.id, preview_report_id: report.id, preview_type: :docx)
else
Rails.application.routes.url_helpers.reports_path(team: report.team.id)
end
user = job.arguments.second
notification = Notification.create(
type_of: :deliver_error,
title: I18n.t('projects.reports.index.generation.error_docx_notification_title'),
message: I18n.t('projects.reports.index.generation.error_notification_message',
report_link: "<a href='#{report_path}'>#{sanitize_input(report.name)}</a>",
team_name: sanitize_input(report.team.name))
)
notification.create_user_notification(user)
Rails.logger.error("Couldn't generate DOCX for Report with id: #{report.id}. Error:\n #{error}")
end
def perform(report, user, team, root_url)
@ -21,7 +40,7 @@ module Reports
Reports::Docx.new(report, docx, user: user, team: team, scinote_url: root_url).draw
docx.save
report.docx_file.attach(io: file, filename: 'report.docx')
report.update!(docx_file_processing: false)
report.docx_ready!
report_path = Rails.application.routes.url_helpers
.reports_path(team: report.team.id, preview_report_id: report.id, preview_type: :docx)
notification = Notification.create(

View file

@ -2,6 +2,7 @@
module Reports
class PdfJob < ApplicationJob
extend InputSanitizeHelper
include InputSanitizeHelper
include ReportsHelper
@ -9,10 +10,28 @@ module Reports
discard_on StandardError do |job, error|
report = Report.find_by(id: job.arguments.first)
return if report.blank?
ActiveRecord::Base.no_touching do
report&.update(pdf_file_processing: false)
report.pdf_error!
end
Rails.logger.error("Couldn't generate PDF for Report with id: #{job.arguments.first}. Error:\n #{error}")
report_path =
if report.pdf_file.attached?
Rails.application.routes.url_helpers
.reports_path(team: report.team.id, preview_report_id: report.id, preview_type: :pdf)
else
Rails.application.routes.url_helpers.reports_path(team: report.team.id)
end
user = job.arguments.second
notification = Notification.create(
type_of: :deliver_error,
title: I18n.t('projects.reports.index.generation.error_pdf_notification_title'),
message: I18n.t('projects.reports.index.generation.error_notification_message',
report_link: "<a href='#{report_path}'>#{sanitize_input(report.name)}</a>",
team_name: sanitize_input(report.team.name))
)
notification.create_user_notification(user)
Rails.logger.error("Couldn't generate PDF for Report with id: #{report.id}. Error:\n #{error}")
end
PREVIEW_EXTENSIONS = %w(docx pdf).freeze
@ -51,7 +70,7 @@ module Reports
file = append_result_asset_previews(report, file) if report.settings.dig(:task, :file_results_previews)
report.pdf_file.attach(io: file, filename: 'report.pdf')
report.update!(pdf_file_processing: false)
report.pdf_ready!
report_path = Rails.application.routes.url_helpers
.reports_path(team: report.team.id, preview_report_id: report.id, preview_type: :pdf)

View file

@ -5,6 +5,9 @@ class Report < ApplicationRecord
include SearchableModel
include SearchableByNameModel
enum pdf_file_status: { pdf_processing: 0, pdf_ready: 1, pdf_error: 2 }
enum docx_file_status: { docx_processing: 0, docx_ready: 1, docx_error: 2 }
# ActiveStorage configuration
has_one_attached :pdf_file
has_one_attached :docx_file

View file

@ -559,7 +559,7 @@ class User < ApplicationRecord
end
def enabled_notifications_for?(notification_type, channel)
return true if notification_type == :deliver
return true if %i(deliver deliver_error).include?(notification_type)
case channel
when :web

View file

@ -0,0 +1,5 @@
<div class="text-center">
<span class="deliver-error">
<i class="fas fa-truck"></i>
</span>
</div>

View file

@ -16,7 +16,8 @@ class Extends
NOTIFICATIONS_TYPES = { assignment: 0,
recent_changes: 1,
system_message: 2, # DEPRECATED
deliver: 5 }
deliver: 5,
deliver_error: 7 }
TASKS_STATES = { uncompleted: 0,
completed: 1 }

View file

@ -510,6 +510,8 @@ en:
pdf: "PDF"
docx: "DOCX"
error: "Error"
previous_pdf: "Previous PDF"
previous_docx: "Previous DOCX"
generating: "Generating"
generate: "Generate"
generation:
@ -520,6 +522,9 @@ en:
completed_docx_notification_title: "Your report .DOCX was generated successfully."
completed_pdf_notification_title: "Your report .PDF was generated successfully."
completed_notification_message: "Report: %{report_link} | Team: %{team_name}"
error_docx_notification_title: "Your report .DOCX generation failed. Please try again."
error_pdf_notification_title: "Your report .PDF generation failed. Please try again."
error_notification_message: "Report: %{report_link} | Team: %{team_name}"
content_generation_error: "Failed to generate report content"
modal_delete:
head_title: "Delete report/s"

View file

@ -3,8 +3,8 @@
class AddProcessingFlagsToReport < ActiveRecord::Migration[6.1]
def change
change_table :reports, bulk: true do |t|
t.boolean :pdf_file_processing, default: false
t.boolean :docx_file_processing, default: false
t.integer :pdf_file_status, default: 0
t.integer :docx_file_status, default: 0
end
end
end

View file

@ -1307,8 +1307,8 @@ CREATE TABLE public.reports (
updated_at timestamp without time zone NOT NULL,
last_modified_by_id bigint,
team_id bigint,
pdf_file_processing boolean DEFAULT false,
docx_file_processing boolean DEFAULT false,
pdf_file_status integer DEFAULT 0,
docx_file_status integer DEFAULT 0,
settings jsonb DEFAULT '{}'::jsonb NOT NULL
);