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!
|
|
|
|
|
|
|
|
|
|
def index
|
2024-05-23 14:12:23 -04:00
|
|
|
@start_at = Time.zone.at(start_at)
|
|
|
|
|
@end_at = Time.zone.at(end_at)
|
2024-03-23 16:46:18 -04:00
|
|
|
end
|
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
|
|
|
|
|
first_point_timestamp = Point.order(timestamp: :asc)&.first&.timestamp
|
|
|
|
|
|
|
|
|
|
@start_at ||=
|
|
|
|
|
if params[:start_at].nil? && first_point_timestamp.present?
|
|
|
|
|
first_point_timestamp
|
|
|
|
|
elsif params[:start_at].nil?
|
|
|
|
|
1.month.ago.to_i
|
|
|
|
|
else
|
|
|
|
|
Time.zone.parse(params[:start_at]).to_i
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def end_at
|
|
|
|
|
last_point_timestamp = Point.order(timestamp: :desc)&.last&.timestamp
|
|
|
|
|
|
|
|
|
|
@end_at ||=
|
|
|
|
|
if params[:end_at].nil? && last_point_timestamp.present?
|
|
|
|
|
last_point_timestamp
|
|
|
|
|
elsif params[:end_at].nil?
|
|
|
|
|
Time.zone.now.to_i
|
|
|
|
|
else
|
|
|
|
|
Time.zone.parse(params[:end_at]).to_i
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-03-23 16:46:18 -04:00
|
|
|
end
|