2020-08-06 16:35:45 +02:00
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: exports
|
|
|
|
|
#
|
2022-04-05 18:11:12 +02:00
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
|
# format :string not null
|
2022-07-26 17:53:24 +02:00
|
|
|
|
# job_status :string default("pending"), not null
|
2022-04-05 18:11:12 +02:00
|
|
|
|
# key :text not null
|
|
|
|
|
# procedure_presentation_snapshot :jsonb
|
|
|
|
|
# statut :string default("tous")
|
|
|
|
|
# time_span_type :string default("everything"), not null
|
|
|
|
|
# created_at :datetime not null
|
|
|
|
|
# updated_at :datetime not null
|
|
|
|
|
# procedure_presentation_id :bigint
|
2020-08-06 16:35:45 +02:00
|
|
|
|
#
|
2019-12-03 18:36:50 +01:00
|
|
|
|
class Export < ApplicationRecord
|
2022-07-07 18:15:48 +02:00
|
|
|
|
include TransientModelsWithPurgeableJobConcern
|
|
|
|
|
|
2022-10-17 15:05:14 +02:00
|
|
|
|
MAX_DUREE_CONSERVATION_EXPORT = 32.hours
|
|
|
|
|
MAX_DUREE_GENERATION = 16.hours
|
2019-12-03 18:36:50 +01:00
|
|
|
|
|
|
|
|
|
enum format: {
|
|
|
|
|
csv: 'csv',
|
|
|
|
|
ods: 'ods',
|
2022-04-08 17:12:53 +02:00
|
|
|
|
xlsx: 'xlsx',
|
2022-11-16 11:50:19 +01:00
|
|
|
|
zip: 'zip',
|
|
|
|
|
json: 'json'
|
2022-04-08 17:12:53 +02:00
|
|
|
|
}, _prefix: true
|
2019-12-03 18:36:50 +01:00
|
|
|
|
|
2021-06-16 11:46:25 +02:00
|
|
|
|
enum time_span_type: {
|
|
|
|
|
everything: 'everything',
|
|
|
|
|
monthly: 'monthly'
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 15:53:15 +02:00
|
|
|
|
enum statut: {
|
|
|
|
|
'a-suivre': 'a-suivre',
|
|
|
|
|
suivis: 'suivis',
|
|
|
|
|
traites: 'traites',
|
|
|
|
|
tous: 'tous',
|
|
|
|
|
supprimes_recemment: 'supprimes_recemment',
|
|
|
|
|
archives: 'archives',
|
|
|
|
|
expirant: 'expirant'
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 18:36:50 +01:00
|
|
|
|
has_and_belongs_to_many :groupe_instructeurs
|
2022-04-05 15:53:15 +02:00
|
|
|
|
belongs_to :procedure_presentation, optional: true
|
2019-12-03 18:36:50 +01:00
|
|
|
|
|
|
|
|
|
has_one_attached :file
|
|
|
|
|
|
2021-03-31 18:19:28 +02:00
|
|
|
|
validates :format, :groupe_instructeurs, :key, presence: true
|
2019-12-03 18:36:50 +01:00
|
|
|
|
|
2020-09-29 14:00:31 +02:00
|
|
|
|
after_create_commit :compute_async
|
2019-12-03 18:36:50 +01:00
|
|
|
|
|
2022-04-05 15:57:19 +02:00
|
|
|
|
FORMATS_WITH_TIME_SPAN = [:xlsx, :ods, :csv].flat_map do |format|
|
2022-05-17 17:54:41 +02:00
|
|
|
|
[{ format: format, time_span_type: 'everything' }]
|
2021-06-16 11:46:25 +02:00
|
|
|
|
end
|
2022-11-16 11:50:19 +01:00
|
|
|
|
FORMATS = [:xlsx, :ods, :csv, :zip, :json].map do |format|
|
2022-04-08 17:05:02 +02:00
|
|
|
|
{ format: format }
|
2022-04-05 15:57:19 +02:00
|
|
|
|
end
|
2021-06-16 11:46:25 +02:00
|
|
|
|
|
2019-12-03 18:36:50 +01:00
|
|
|
|
def compute_async
|
|
|
|
|
ExportJob.perform_later(self)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def compute
|
2022-04-05 18:11:12 +02:00
|
|
|
|
load_snapshot!
|
|
|
|
|
|
2022-04-08 17:12:34 +02:00
|
|
|
|
file.attach(blob)
|
2019-12-03 18:36:50 +01:00
|
|
|
|
end
|
|
|
|
|
|
2021-06-16 11:46:25 +02:00
|
|
|
|
def since
|
|
|
|
|
time_span_type == Export.time_span_types.fetch(:monthly) ? 30.days.ago : nil
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-08 12:47:29 +02:00
|
|
|
|
def old?
|
2022-11-15 18:52:40 +01:00
|
|
|
|
updated_at < 10.minutes.ago || filters_changed?
|
2022-04-05 18:11:12 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def filters_changed?
|
|
|
|
|
procedure_presentation&.snapshot != procedure_presentation_snapshot
|
2021-04-08 12:47:29 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-05 15:57:19 +02:00
|
|
|
|
def filtered?
|
|
|
|
|
procedure_presentation_id.present?
|
|
|
|
|
end
|
|
|
|
|
|
2022-11-15 18:53:23 +01:00
|
|
|
|
def flash_message
|
|
|
|
|
if available?
|
|
|
|
|
"L’export au format \"#{format}\" est prêt. Vous pouvez le <a href=\"#{file.service_url}\">télécharger</a>"
|
|
|
|
|
else
|
|
|
|
|
"Nous générons cet export. Veuillez revenir dans quelques minutes pour le télécharger."
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.find_or_create_export(format, groupe_instructeurs, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil, force: false)
|
|
|
|
|
export = create_or_find_export(format, groupe_instructeurs, time_span_type: time_span_type, statut: statut, procedure_presentation: procedure_presentation)
|
|
|
|
|
|
|
|
|
|
if export.available? && export.old? && force
|
|
|
|
|
export.destroy
|
|
|
|
|
create_or_find_export(format, groupe_instructeurs, time_span_type: time_span_type, statut: statut, procedure_presentation: procedure_presentation)
|
|
|
|
|
else
|
|
|
|
|
export
|
|
|
|
|
end
|
2019-12-03 18:36:50 +01:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-05 15:57:19 +02:00
|
|
|
|
def self.find_for_groupe_instructeurs(groupe_instructeurs_ids, procedure_presentation)
|
|
|
|
|
exports = if procedure_presentation.present?
|
2022-10-21 16:55:42 +02:00
|
|
|
|
where(key: generate_cache_key(groupe_instructeurs_ids, procedure_presentation))
|
2022-10-31 14:12:55 +01:00
|
|
|
|
.or(where(key: generate_cache_key(groupe_instructeurs_ids)))
|
2022-04-05 15:57:19 +02:00
|
|
|
|
else
|
|
|
|
|
where(key: generate_cache_key(groupe_instructeurs_ids))
|
|
|
|
|
end
|
|
|
|
|
filtered, not_filtered = exports.partition(&:filtered?)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
xlsx: {
|
2022-04-12 11:59:24 +02:00
|
|
|
|
time_span_type: not_filtered.filter(&:format_xlsx?).index_by(&:time_span_type),
|
|
|
|
|
statut: filtered.filter(&:format_xlsx?).index_by(&:statut)
|
2022-04-05 15:57:19 +02:00
|
|
|
|
},
|
|
|
|
|
ods: {
|
2022-04-12 11:59:24 +02:00
|
|
|
|
time_span_type: not_filtered.filter(&:format_ods?).index_by(&:time_span_type),
|
|
|
|
|
statut: filtered.filter(&:format_ods?).index_by(&:statut)
|
2022-04-05 15:57:19 +02:00
|
|
|
|
},
|
|
|
|
|
csv: {
|
2022-04-12 11:59:24 +02:00
|
|
|
|
time_span_type: not_filtered.filter(&:format_csv?).index_by(&:time_span_type),
|
|
|
|
|
statut: filtered.filter(&:format_csv?).index_by(&:statut)
|
2022-04-08 17:12:53 +02:00
|
|
|
|
},
|
|
|
|
|
zip: {
|
|
|
|
|
time_span_type: {},
|
2022-04-12 11:59:24 +02:00
|
|
|
|
statut: filtered.filter(&:format_zip?).index_by(&:statut)
|
2022-11-16 11:50:19 +01:00
|
|
|
|
},
|
|
|
|
|
json: {
|
|
|
|
|
time_span_type: {},
|
|
|
|
|
statut: filtered.filter(&:format_json?).index_by(&:statut)
|
2022-04-05 15:57:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
2021-04-07 09:43:55 +02:00
|
|
|
|
|
2022-11-15 18:53:23 +01:00
|
|
|
|
def self.create_or_find_export(format, groupe_instructeurs, time_span_type:, statut:, procedure_presentation:)
|
|
|
|
|
create_with(groupe_instructeurs: groupe_instructeurs, procedure_presentation: procedure_presentation, procedure_presentation_snapshot: procedure_presentation&.snapshot)
|
|
|
|
|
.includes(:procedure_presentation)
|
|
|
|
|
.create_or_find_by(format: format,
|
|
|
|
|
time_span_type: time_span_type,
|
|
|
|
|
statut: statut,
|
|
|
|
|
key: generate_cache_key(groupe_instructeurs.map(&:id), procedure_presentation))
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-21 16:55:42 +02:00
|
|
|
|
def self.generate_cache_key(groupe_instructeurs_ids, procedure_presentation = nil)
|
|
|
|
|
if procedure_presentation.present?
|
|
|
|
|
[
|
|
|
|
|
groupe_instructeurs_ids.sort.join('-'),
|
|
|
|
|
procedure_presentation.id,
|
2022-10-21 17:24:16 +02:00
|
|
|
|
Digest::MD5.hexdigest(procedure_presentation.snapshot.slice(:filters, :sort).to_s)
|
2022-10-21 16:55:42 +02:00
|
|
|
|
].join('--')
|
2022-04-05 15:57:19 +02:00
|
|
|
|
else
|
|
|
|
|
groupe_instructeurs_ids.sort.join('-')
|
|
|
|
|
end
|
2021-03-31 18:19:28 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-05 15:57:19 +02:00
|
|
|
|
def count
|
|
|
|
|
if procedure_presentation_id.present?
|
|
|
|
|
dossiers_for_export.size
|
|
|
|
|
end
|
2019-12-03 18:36:50 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-02-08 17:32:40 +01:00
|
|
|
|
def procedure
|
|
|
|
|
groupe_instructeurs.first.procedure
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-03 18:36:50 +01:00
|
|
|
|
private
|
|
|
|
|
|
2022-04-05 18:11:12 +02:00
|
|
|
|
def load_snapshot!
|
|
|
|
|
if procedure_presentation_snapshot.present?
|
|
|
|
|
procedure_presentation.attributes = procedure_presentation_snapshot
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-05 15:57:19 +02:00
|
|
|
|
def dossiers_for_export
|
|
|
|
|
@dossiers_for_export ||= begin
|
|
|
|
|
dossiers = Dossier.where(groupe_instructeur: groupe_instructeurs)
|
|
|
|
|
|
|
|
|
|
if since.present?
|
|
|
|
|
dossiers.visible_by_administration.where('dossiers.depose_at > ?', since)
|
|
|
|
|
elsif procedure_presentation.present?
|
|
|
|
|
filtered_sorted_ids = procedure_presentation
|
2022-04-06 09:12:15 +02:00
|
|
|
|
.filtered_sorted_ids(dossiers, statut)
|
2022-04-05 15:57:19 +02:00
|
|
|
|
|
|
|
|
|
dossiers.where(id: filtered_sorted_ids)
|
|
|
|
|
else
|
2022-05-10 14:38:04 +02:00
|
|
|
|
dossiers.visible_by_administration
|
2022-04-05 15:57:19 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-08 17:12:34 +02:00
|
|
|
|
def blob
|
2022-04-05 15:57:19 +02:00
|
|
|
|
service = ProcedureExportService.new(procedure, dossiers_for_export)
|
2019-12-03 18:36:50 +01:00
|
|
|
|
|
|
|
|
|
case format.to_sym
|
|
|
|
|
when :csv
|
2022-04-08 17:12:34 +02:00
|
|
|
|
service.to_csv
|
2019-12-03 18:36:50 +01:00
|
|
|
|
when :xlsx
|
2022-04-08 17:12:34 +02:00
|
|
|
|
service.to_xlsx
|
2019-12-03 18:36:50 +01:00
|
|
|
|
when :ods
|
2022-04-08 17:12:34 +02:00
|
|
|
|
service.to_ods
|
2022-04-08 17:12:53 +02:00
|
|
|
|
when :zip
|
|
|
|
|
service.to_zip
|
2022-11-16 11:50:19 +01:00
|
|
|
|
when :json
|
|
|
|
|
service.to_geo_json
|
2019-12-03 18:36:50 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|