2024-06-12 14:29:38 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Exports::Create
|
2024-09-02 12:28:17 -04:00
|
|
|
def initialize(export:, start_at:, end_at:, format: :json)
|
2024-06-12 14:29:38 -04:00
|
|
|
@export = export
|
|
|
|
|
@user = export.user
|
2024-06-14 12:17:42 -04:00
|
|
|
@start_at = start_at.to_datetime
|
|
|
|
|
@end_at = end_at.to_datetime
|
2024-09-02 12:28:17 -04:00
|
|
|
@format = format
|
2024-06-12 14:29:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
|
|
|
|
export.update!(status: :processing)
|
|
|
|
|
|
2024-08-22 16:40:27 -04:00
|
|
|
Rails.logger.debug "====Exporting data for #{user.email} from #{start_at} to #{end_at}"
|
2024-06-14 13:33:04 -04:00
|
|
|
|
2024-06-14 13:52:05 -04:00
|
|
|
points = time_framed_points
|
2024-06-14 13:33:04 -04:00
|
|
|
|
2024-08-22 16:40:27 -04:00
|
|
|
Rails.logger.debug "====Exporting #{points.size} points"
|
2024-06-14 13:33:04 -04:00
|
|
|
|
2024-06-12 14:29:38 -04:00
|
|
|
data = ::ExportSerializer.new(points, user.email).call
|
2024-09-02 12:28:17 -04:00
|
|
|
file_path = Rails.root.join('public', 'exports', "#{export.name}.#{format}")
|
2024-06-12 14:29:38 -04:00
|
|
|
|
|
|
|
|
File.open(file_path, 'w') { |file| file.write(data) }
|
|
|
|
|
|
|
|
|
|
export.update!(status: :completed, url: "exports/#{export.name}.json")
|
2024-07-04 16:20:12 -04:00
|
|
|
|
2024-08-22 16:40:27 -04:00
|
|
|
create_export_finished_notification
|
2024-06-12 14:29:38 -04:00
|
|
|
rescue StandardError => e
|
|
|
|
|
Rails.logger.error("====Export failed to create: #{e.message}")
|
|
|
|
|
|
2024-08-22 16:40:27 -04:00
|
|
|
create_failed_export_notification(e)
|
2024-07-04 16:20:12 -04:00
|
|
|
|
2024-06-12 14:29:38 -04:00
|
|
|
export.update!(status: :failed)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-09-02 12:28:17 -04:00
|
|
|
attr_reader :user, :export, :start_at, :end_at, :format
|
2024-06-12 14:29:38 -04:00
|
|
|
|
2024-06-14 13:33:04 -04:00
|
|
|
def time_framed_points
|
|
|
|
|
user
|
|
|
|
|
.tracked_points
|
|
|
|
|
.where('timestamp >= ? AND timestamp <= ?', start_at.to_i, end_at.to_i)
|
2024-06-12 14:29:38 -04:00
|
|
|
end
|
2024-08-22 16:40:27 -04:00
|
|
|
|
|
|
|
|
def create_export_finished_notification
|
|
|
|
|
Notifications::Create.new(
|
|
|
|
|
user:,
|
|
|
|
|
kind: :info,
|
|
|
|
|
title: 'Export finished',
|
|
|
|
|
content: "Export \"#{export.name}\" successfully finished."
|
|
|
|
|
).call
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_failed_export_notification(error)
|
|
|
|
|
Notifications::Create.new(
|
|
|
|
|
user:,
|
|
|
|
|
kind: :error,
|
|
|
|
|
title: 'Export failed',
|
|
|
|
|
content: "Export \"#{export.name}\" failed: #{error.message}, stacktrace: #{error.backtrace.join("\n")}"
|
|
|
|
|
).call
|
|
|
|
|
end
|
2024-09-02 12:28:17 -04:00
|
|
|
|
|
|
|
|
def process_json_export(points)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def process_gpx_export(points)
|
|
|
|
|
end
|
2024-06-12 14:29:38 -04:00
|
|
|
end
|