Save number of raw points and doubles in import file

This commit is contained in:
Eugene Burmakin 2024-03-23 14:20:20 +01:00
parent 18ed732c24
commit 5cb8af66e0
7 changed files with 42 additions and 9 deletions

View file

@ -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! }

View file

@ -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

View file

@ -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

View file

@ -11,7 +11,9 @@
<thead>
<tr>
<th>Name</th>
<th>Points</th>
<th>Raw points</th>
<th>Points created</th>
<th>Doubles</th>
<th>Created at</th>
</tr>
</thead>
@ -21,6 +23,8 @@
<td>
<%= link_to import.name, import, class: 'underline hover:no-underline' %> (<%= import.source %>)
</td>
<td><%= import.raw_points %></td>
<td><%= import.doubles %></td>
<td><%= import.points.count %></td>
<td><%= import.created_at.strftime("%d.%m.%Y, %H:%M") %></td>
</tr>

View file

@ -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" %>
<div class="inline-block ml-2">
<%= 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" %>
</div>
<%= link_to "Back to imports", imports_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -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

4
db/schema.rb generated
View file

@ -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