2024-03-15 18:27:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class OwnTracks::ExportParser
|
2024-05-25 07:36:15 -04:00
|
|
|
attr_reader :import, :json, :user_id
|
2024-03-15 18:27:31 -04:00
|
|
|
|
2024-05-25 07:36:15 -04:00
|
|
|
def initialize(import, user_id)
|
2024-03-24 13:05:39 -04:00
|
|
|
@import = import
|
2024-04-25 16:28:34 -04:00
|
|
|
@json = import.raw_data
|
2024-05-25 07:36:15 -04:00
|
|
|
@user_id = user_id
|
2024-03-15 18:27:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
|
|
|
|
points_data = parse_json
|
|
|
|
|
|
2024-03-23 09:20:20 -04:00
|
|
|
points = 0
|
|
|
|
|
|
2024-03-15 18:27:31 -04:00
|
|
|
points_data.each do |point_data|
|
2024-03-21 17:56:55 -04:00
|
|
|
next if Point.exists?(timestamp: point_data[:timestamp], tracker_id: point_data[:tracker_id])
|
|
|
|
|
|
2024-03-15 18:27:31 -04:00
|
|
|
Point.create(
|
|
|
|
|
latitude: point_data[:latitude],
|
|
|
|
|
longitude: point_data[:longitude],
|
|
|
|
|
timestamp: point_data[:timestamp],
|
|
|
|
|
raw_data: point_data[:raw_data],
|
|
|
|
|
topic: point_data[:topic],
|
|
|
|
|
tracker_id: point_data[:tracker_id],
|
2024-05-25 07:36:15 -04:00
|
|
|
import_id: import.id,
|
|
|
|
|
user_id:
|
2024-03-15 18:27:31 -04:00
|
|
|
)
|
2024-03-23 09:20:20 -04:00
|
|
|
|
|
|
|
|
points += 1
|
2024-03-15 18:27:31 -04:00
|
|
|
end
|
2024-03-23 09:20:20 -04:00
|
|
|
|
|
|
|
|
doubles = points_data.size - points
|
2024-03-24 13:05:39 -04:00
|
|
|
processed = points + doubles
|
2024-03-23 09:20:20 -04:00
|
|
|
|
2024-04-25 16:28:34 -04:00
|
|
|
{ raw_points: points_data.size, points:, doubles:, processed: }
|
2024-03-15 18:27:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def parse_json
|
|
|
|
|
points = []
|
|
|
|
|
|
2024-04-25 16:28:34 -04:00
|
|
|
json.each_key do |user|
|
|
|
|
|
json[user].each_key do |devise|
|
2024-03-15 18:27:31 -04:00
|
|
|
json[user][devise].each { |point| points << OwnTracks::Params.new(point).call }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
points
|
|
|
|
|
end
|
|
|
|
|
end
|