-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #699 from Freika/feature/api/points
- Loading branch information
Showing
38 changed files
with
687 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.22.5 | ||
0.23.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class Points::CreateJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(params, user_id) | ||
data = Points::Params.new(params, user_id).call | ||
|
||
data.each_slice(1000) do |location_batch| | ||
Point.upsert_all( | ||
location_batch, | ||
unique_by: %i[latitude longitude timestamp user_id], | ||
returning: false | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
class Points::Params | ||
attr_reader :data, :points, :user_id | ||
|
||
def initialize(json, user_id) | ||
@data = json.with_indifferent_access | ||
@points = @data[:locations] | ||
@user_id = user_id | ||
end | ||
|
||
def call | ||
points.map do |point| | ||
next unless params_valid?(point) | ||
|
||
{ | ||
latitude: point[:geometry][:coordinates][1], | ||
longitude: point[:geometry][:coordinates][0], | ||
battery_status: point[:properties][:battery_state], | ||
battery: battery_level(point[:properties][:battery_level]), | ||
timestamp: DateTime.parse(point[:properties][:timestamp]), | ||
altitude: point[:properties][:altitude], | ||
tracker_id: point[:properties][:device_id], | ||
velocity: point[:properties][:speed], | ||
ssid: point[:properties][:wifi], | ||
accuracy: point[:properties][:horizontal_accuracy], | ||
vertical_accuracy: point[:properties][:vertical_accuracy], | ||
course_accuracy: point[:properties][:course_accuracy], | ||
course: point[:properties][:course], | ||
raw_data: point, | ||
user_id: user_id | ||
} | ||
end.compact | ||
end | ||
|
||
private | ||
|
||
def battery_level(level) | ||
value = (level.to_f * 100).to_i | ||
|
||
value.positive? ? value : nil | ||
end | ||
|
||
def params_valid?(point) | ||
point[:geometry].present? && | ||
point[:geometry][:coordinates].present? && | ||
point.dig(:properties, :timestamp).present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
class RemoveDuplicatePoints < ActiveRecord::Migration[8.0] | ||
def up | ||
# Find duplicate groups using a subquery | ||
duplicate_groups = | ||
Point.select('latitude, longitude, timestamp, user_id, COUNT(*) as count') | ||
.group('latitude, longitude, timestamp, user_id') | ||
.having('COUNT(*) > 1') | ||
|
||
puts "Duplicate groups found: #{duplicate_groups.length}" | ||
|
||
duplicate_groups.each do |group| | ||
points = Point.where( | ||
latitude: group.latitude, | ||
longitude: group.longitude, | ||
timestamp: group.timestamp, | ||
user_id: group.user_id | ||
).order(id: :asc) | ||
|
||
# Keep the latest record and destroy all others | ||
latest = points.last | ||
points.where.not(id: latest.id).destroy_all | ||
end | ||
end | ||
|
||
def down | ||
# This migration cannot be reversed | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
DataMigrate::Data.define(version: 20250104204852) | ||
DataMigrate::Data.define(version: 20250120154554) |
8 changes: 8 additions & 0 deletions
8
db/migrate/20250120152014_add_course_and_course_accuracy_to_points.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddCourseAndCourseAccuracyToPoints < ActiveRecord::Migration[8.0] | ||
def change | ||
add_column :points, :course, :decimal, precision: 8, scale: 5 | ||
add_column :points, :course_accuracy, :decimal, precision: 8, scale: 5 | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20250120152540_add_external_track_id_to_points.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddExternalTrackIdToPoints < ActiveRecord::Migration[8.0] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
add_column :points, :external_track_id, :string | ||
|
||
add_index :points, :external_track_id, algorithm: :concurrently | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddUniqueIndexToPoints < ActiveRecord::Migration[8.0] | ||
disable_ddl_transaction! | ||
|
||
def up | ||
add_index :points, %i[latitude longitude timestamp user_id], | ||
unique: true, | ||
name: 'unique_points_index', | ||
algorithm: :concurrently | ||
end | ||
|
||
def down | ||
remove_index :points, name: 'unique_points_index' | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.