From 2783cc50143224ffc49fcec3e7de3ee132dc00ae Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Thu, 4 Apr 2024 17:29:11 +0200 Subject: [PATCH] Allow user to download the exported data as a JSON file --- app/controllers/export_controller.rb | 10 ++++ app/models/user.rb | 8 +--- app/serializers/export_serializer.rb | 72 ++++++++++++++++++++++++++++ app/services/own_tracks/params.rb | 38 +++++++-------- app/views/export/index.html.erb | 4 ++ config/routes.rb | 1 + 6 files changed, 107 insertions(+), 26 deletions(-) create mode 100644 app/serializers/export_serializer.rb diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index 461ece1c..949f1148 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index a20d5430..4d3ae41e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/serializers/export_serializer.rb b/app/serializers/export_serializer.rb new file mode 100644 index 00000000..9fd07ef8 --- /dev/null +++ b/app/serializers/export_serializer.rb @@ -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 diff --git a/app/services/own_tracks/params.rb b/app/services/own_tracks/params.rb index 350283f7..b60d6220 100644 --- a/app/services/own_tracks/params.rb +++ b/app/services/own_tracks/params.rb @@ -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 diff --git a/app/views/export/index.html.erb b/app/views/export/index.html.erb index 037e48a6..5223c7b1 100644 --- a/app/views/export/index.html.erb +++ b/app/views/export/index.html.erb @@ -1,4 +1,8 @@
+
+

Export Data

+ <%= link_to 'Download JSON', export_download_path, class: 'btn btn-primary my-5' %> +
<%= current_user.export_data %>
diff --git a/config/routes.rb b/config/routes.rb index 66a9380b..3d95d0df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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