diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index 20f556f7..351bc31c 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -16,18 +16,23 @@ class ImportsController < ApplicationController def create files = import_params[:files].reject(&:blank?) imports = [] - success = true + report = '' files.each do |file| json = JSON.parse(file.read) import = current_user.imports.create(name: file.original_filename, source: params[:import][:source]) - parser.new(file.path, import.id).call + result = parser.new(file.path, import.id).call - imports << import + if result[:points].zero? + import.destroy! + else + import.update(raw_points: result[:raw_points], doubles: result[:doubles]) + + imports << import + end end - redirect_to imports_url, notice: "#{imports.count} imports was successfully created.", status: :see_other - + redirect_to imports_url, notice: "#{imports.size} import files were imported successfully", status: :see_other rescue StandardError => e imports.each { |import| import&.destroy! } diff --git a/app/services/google_maps/timeline_parser.rb b/app/services/google_maps/timeline_parser.rb index 46d5d45f..7d330704 100644 --- a/app/services/google_maps/timeline_parser.rb +++ b/app/services/google_maps/timeline_parser.rb @@ -16,6 +16,8 @@ class GoogleMaps::TimelineParser def call points_data = parse_json + points = 0 + points_data.each do |point_data| next if Point.exists?(timestamp: point_data[:timestamp]) @@ -28,7 +30,13 @@ class GoogleMaps::TimelineParser tracker_id: 'google-maps-timeline-export', import_id: import_id ) + + points += 1 end + + doubles = points_data.size - points + + { raw_points: points_data.size, points: points, doubles: doubles } end private @@ -43,7 +51,7 @@ class GoogleMaps::TimelineParser { latitude: waypoint['latE7'].to_f / 10**7, longitude: waypoint['lngE7'].to_f / 10**7, - timestamp: DateTime.parse(timeline_object['activitySegment']['duration']['startTimestamp']), + timestamp: DateTime.parse(timeline_object['activitySegment']['duration']['startTimestamp']).to_i, raw_data: timeline_object } end diff --git a/app/services/own_tracks/export_parser.rb b/app/services/own_tracks/export_parser.rb index fd04e9d0..ac7a3f95 100644 --- a/app/services/own_tracks/export_parser.rb +++ b/app/services/own_tracks/export_parser.rb @@ -16,6 +16,8 @@ class OwnTracks::ExportParser def call points_data = parse_json + points = 0 + points_data.each do |point_data| next if Point.exists?(timestamp: point_data[:timestamp], tracker_id: point_data[:tracker_id]) @@ -28,7 +30,13 @@ class OwnTracks::ExportParser tracker_id: point_data[:tracker_id], import_id: import_id ) + + points += 1 end + + doubles = points_data.size - points + + { raw_points: points_data.size, points: points, doubles: doubles } end private diff --git a/app/views/imports/index.html.erb b/app/views/imports/index.html.erb index 40a3d12c..e7967340 100644 --- a/app/views/imports/index.html.erb +++ b/app/views/imports/index.html.erb @@ -11,7 +11,9 @@ Name - Points + Raw points + Points created + Doubles Created at @@ -21,6 +23,8 @@ <%= link_to import.name, import, class: 'underline hover:no-underline' %> (<%= import.source %>) + <%= import.raw_points %> + <%= import.doubles %> <%= import.points.count %> <%= import.created_at.strftime("%d.%m.%Y, %H:%M") %> diff --git a/app/views/imports/show.html.erb b/app/views/imports/show.html.erb index 2f5d28c9..2605a75d 100644 --- a/app/views/imports/show.html.erb +++ b/app/views/imports/show.html.erb @@ -8,7 +8,7 @@ <%= link_to "Edit this import", edit_import_path(@import), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
- <%= link_to "Destroy this import", import_path(@import), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?", turbo_method: :delete }, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> + <%= link_to "Destroy this import", import_path(@import), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure? This action will delete all points imported with this file", turbo_method: :delete }, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
<%= link_to "Back to imports", imports_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> diff --git a/db/migrate/20240323125126_add_raw_points_and_doubles_to_import.rb b/db/migrate/20240323125126_add_raw_points_and_doubles_to_import.rb new file mode 100644 index 00000000..f54897e6 --- /dev/null +++ b/db/migrate/20240323125126_add_raw_points_and_doubles_to_import.rb @@ -0,0 +1,6 @@ +class AddRawPointsAndDoublesToImport < ActiveRecord::Migration[7.1] + def change + add_column :imports, :raw_points, :integer, default: 0 + add_column :imports, :doubles, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index eb181b9a..1cc17c4b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_03_17_171559) do +ActiveRecord::Schema[7.1].define(version: 2024_03_23_125126) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -20,6 +20,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_03_17_171559) do t.integer "source", default: 0 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "raw_points", default: 0 + t.integer "doubles", default: 0 t.index ["source"], name: "index_imports_on_source" t.index ["user_id"], name: "index_imports_on_user_id" end