2024-04-26 12:59:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-03-23 16:46:18 -04:00
|
|
|
class ExportController < ApplicationController
|
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
2024-04-04 11:29:11 -04:00
|
|
|
def download
|
2024-05-23 14:12:23 -04:00
|
|
|
export = current_user.export_data(start_at:, end_at:)
|
2024-04-04 11:29:11 -04:00
|
|
|
|
2024-05-23 14:12:23 -04:00
|
|
|
send_data export, filename:, type: 'applocation/json', disposition: 'attachment'
|
2024-04-26 12:59:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def filename
|
2024-05-23 14:12:23 -04:00
|
|
|
first_point_datetime = Time.zone.at(start_at).to_s
|
|
|
|
|
last_point_datetime = Time.zone.at(end_at).to_s
|
2024-04-26 12:59:58 -04:00
|
|
|
|
|
|
|
|
"dawarich-export-#{first_point_datetime}-#{last_point_datetime}.json".gsub(' ', '_')
|
2024-04-04 11:29:11 -04:00
|
|
|
end
|
2024-05-23 14:12:23 -04:00
|
|
|
|
|
|
|
|
def start_at
|
2024-05-25 07:26:56 -04:00
|
|
|
first_point_timestamp = current_user.tracked_points.order(timestamp: :asc)&.first&.timestamp
|
2024-05-23 14:12:23 -04:00
|
|
|
|
2024-05-30 05:50:12 -04:00
|
|
|
@start_at ||= first_point_timestamp || 1.month.ago.to_i
|
2024-05-23 14:12:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def end_at
|
2024-05-30 05:50:12 -04:00
|
|
|
last_point_timestamp = current_user.tracked_points.order(timestamp: :asc)&.last&.timestamp
|
|
|
|
|
|
|
|
|
|
@end_at ||= last_point_timestamp || Time.current.to_i
|
2024-05-23 14:12:23 -04:00
|
|
|
end
|
2024-03-23 16:46:18 -04:00
|
|
|
end
|