Use attached import file to import own tracks data

This commit is contained in:
Eugene Burmakin 2025-03-23 19:00:04 +01:00
parent a93b49ee80
commit f3b98ac83d
5 changed files with 25 additions and 20 deletions

View file

@ -11,7 +11,7 @@ class Gpx::TrackImporter
end
def call
import.file.open do |file|
import.file.download do |file|
json = Hash.from_xml(file)
tracks = json['gpx']['trk']

View file

@ -3,25 +3,28 @@
class OwnTracks::Importer
include Imports::Broadcaster
attr_reader :import, :data, :user_id
attr_reader :import, :user_id
def initialize(import, user_id)
@import = import
@data = import.raw_data
@user_id = user_id
end
def call
points_data = data.map.with_index(1) do |point, index|
OwnTracks::Params.new(point).call.merge(
import_id: import.id,
user_id: user_id,
created_at: Time.current,
updated_at: Time.current
)
end
import.file.download do |file|
parsed_data = OwnTracks::RecParser.new(file).call
bulk_insert_points(points_data)
points_data = parsed_data.map do |point|
OwnTracks::Params.new(point).call.merge(
import_id: import.id,
user_id: user_id,
created_at: Time.current,
updated_at: Time.current
)
end
bulk_insert_points(points_data)
end
end
private

View file

@ -10,11 +10,8 @@ class OwnTracks::RecParser
def call
file.split("\n").map do |line|
parts = line.split("\t")
if parts.size > 2 && parts[1].strip == '*'
JSON.parse(parts[2])
else
nil
end
Oj.load(parts[2]) if parts.size > 2 && parts[1].strip == '*'
end.compact
end
end

View file

@ -3,8 +3,7 @@
FactoryBot.define do
factory :import do
user
name { 'MARCH_2024.json' }
name { 'owntracks_export.json' }
source { Import.sources[:owntracks] }
raw_data { OwnTracks::RecParser.new(File.read('spec/fixtures/files/owntracks/2024-03.rec')).call }
end
end

View file

@ -7,7 +7,13 @@ RSpec.describe OwnTracks::Importer do
subject(:parser) { described_class.new(import, user.id).call }
let(:user) { create(:user) }
let(:import) { create(:import, user:, name: 'owntracks_export.json') }
let(:import) { create(:import, user:, name: '2024-03.rec') }
let(:file_path) { Rails.root.join('spec/fixtures/files/owntracks/2024-03.rec') }
let(:file) { Rack::Test::UploadedFile.new(file_path, 'text/plain') }
before do
import.file.attach(io: File.open(file_path), filename: '2024-03.rec', content_type: 'text/plain')
end
context 'when file exists' do
it 'creates points' do