mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 17:21:38 -05:00
Allow user to download the exported data as a JSON file
This commit is contained in:
parent
70cf77f13b
commit
2783cc5014
6 changed files with 107 additions and 26 deletions
|
|
@ -4,4 +4,14 @@ class ExportController < ApplicationController
|
|||
def index
|
||||
@export = current_user.export_data
|
||||
end
|
||||
|
||||
def download
|
||||
first_point_datetime = Time.at(current_user.points.first.timestamp).to_s
|
||||
last_point_datetime = Time.at(current_user.points.last.timestamp).to_s
|
||||
filename = "dawarich-export-#{first_point_datetime}-#{last_point_datetime}.json".gsub(' ', '_')
|
||||
|
||||
export = current_user.export_data
|
||||
|
||||
send_data export, filename: filename
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -9,13 +9,7 @@ class User < ApplicationRecord
|
|||
has_many :stats
|
||||
|
||||
def export_data
|
||||
excepted_attributes = %w[raw_data id created_at updated_at country city import_id]
|
||||
|
||||
{
|
||||
email => {
|
||||
'dawarich-export' => self.points.map { _1.attributes.except(*excepted_attributes) }
|
||||
}
|
||||
}.to_json
|
||||
::ExportSerializer.new(points, self.email).call
|
||||
end
|
||||
|
||||
def total_km
|
||||
|
|
|
|||
72
app/serializers/export_serializer.rb
Normal file
72
app/serializers/export_serializer.rb
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ExportSerializer
|
||||
attr_reader :points, :user_email
|
||||
|
||||
def initialize(points, user_email)
|
||||
@points = points
|
||||
@user_email = user_email
|
||||
end
|
||||
|
||||
def call
|
||||
{ user_email => { 'dawarich-export' => export_points } }.to_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def export_points
|
||||
points.map do |point|
|
||||
{
|
||||
lat: point.latitude,
|
||||
lon: point.longitude,
|
||||
bs: battery_status(point),
|
||||
batt: point.battery,
|
||||
p: point.ping,
|
||||
alt: point.altitude,
|
||||
acc: point.accuracy,
|
||||
vac: point.vertical_accuracy,
|
||||
vel: point.velocity,
|
||||
conn: connection(point),
|
||||
SSID: point.ssid,
|
||||
BSSID: point.bssid,
|
||||
m: trigger(point),
|
||||
tid: point.tracker_id,
|
||||
tst: point.timestamp.to_i,
|
||||
inrids: point.inrids,
|
||||
inregions: point.in_regions,
|
||||
topic: point.topic
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def battery_status(point)
|
||||
case point.battery_status
|
||||
when 'unplugged' then 'u'
|
||||
when 'charging' then 'c'
|
||||
when 'full' then 'f'
|
||||
else 'unknown'
|
||||
end
|
||||
end
|
||||
|
||||
def trigger(point)
|
||||
case point.trigger
|
||||
when 'background_event' then 'p'
|
||||
when 'circular_region_event' then 'c'
|
||||
when 'beacon_event' then 'b'
|
||||
when 'report_location_message_event' then 'r'
|
||||
when 'manual_event' then 'u'
|
||||
when 'timer_based_event' then 't'
|
||||
when 'settings_monitoring_event' then 'v'
|
||||
else 'unknown'
|
||||
end
|
||||
end
|
||||
|
||||
def connection(point)
|
||||
case point.connection
|
||||
when 'mobile' then 'm'
|
||||
when 'wifi' then 'w'
|
||||
when 'offline' then 'o'
|
||||
else 'unknown'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -9,25 +9,25 @@ class OwnTracks::Params
|
|||
|
||||
def call
|
||||
{
|
||||
latitude: params[:lat],
|
||||
longitude: params[:lon],
|
||||
battery_status: battery_status,
|
||||
battery: params[:batt],
|
||||
ping: params[:p],
|
||||
altitude: params[:alt],
|
||||
accuracy: params[:acc],
|
||||
vertical_accuracy: params[:vac],
|
||||
velocity: params[:vel],
|
||||
connection: connection,
|
||||
ssid: params[:SSID],
|
||||
bssid: params[:BSSID],
|
||||
trigger: trigger,
|
||||
tracker_id: params[:tid],
|
||||
timestamp: params[:tst].to_i,
|
||||
inrids: params[:inrids],
|
||||
in_regions: params[:inregions],
|
||||
topic: params[:topic],
|
||||
raw_data: params.deep_stringify_keys
|
||||
latitude: params[:lat],
|
||||
longitude: params[:lon],
|
||||
battery_status: battery_status,
|
||||
battery: params[:batt],
|
||||
ping: params[:p],
|
||||
altitude: params[:alt],
|
||||
accuracy: params[:acc],
|
||||
vertical_accuracy: params[:vac],
|
||||
velocity: params[:vel],
|
||||
connection: connection,
|
||||
ssid: params[:SSID],
|
||||
bssid: params[:BSSID],
|
||||
trigger: trigger,
|
||||
tracker_id: params[:tid],
|
||||
timestamp: params[:tst].to_i,
|
||||
inrids: params[:inrids],
|
||||
in_regions: params[:inregions],
|
||||
topic: params[:topic],
|
||||
raw_data: params.deep_stringify_keys
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
<div class="w-full">
|
||||
<div class='m-5'>
|
||||
<h1 class='text-3xl font-bold'>Export Data</h1>
|
||||
<%= link_to 'Download JSON', export_download_path, class: 'btn btn-primary my-5' %>
|
||||
</div>
|
||||
<div class="mockup-code p-5">
|
||||
<code><%= current_user.export_data %></code>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
Rails.application.routes.draw do
|
||||
get 'export', to: 'export#index'
|
||||
get 'export/download', to: 'export#download'
|
||||
resources :imports
|
||||
resources :stats, only: :index do
|
||||
collection do
|
||||
|
|
|
|||
Loading…
Reference in a new issue