dawarich/app/models/point.rb

140 lines
3.9 KiB
Ruby
Raw Normal View History

2024-05-23 14:12:23 -04:00
# frozen_string_literal: true
2024-03-15 18:27:31 -04:00
class Point < ApplicationRecord
include Nearable
include Distanceable
2024-07-12 15:59:03 -04:00
2024-08-22 16:40:27 -04:00
belongs_to :import, optional: true, counter_cache: true
2024-07-21 14:32:29 -04:00
belongs_to :visit, optional: true
2024-07-12 15:59:03 -04:00
belongs_to :user
2025-06-25 16:23:43 -04:00
belongs_to :country, optional: true
belongs_to :track, optional: true
2024-03-15 18:27:31 -04:00
validates :timestamp, :lonlat, presence: true
validates :lonlat, uniqueness: {
scope: %i[timestamp user_id],
message: 'already has a point at this location and time for this user',
index: true
}
2024-09-05 15:01:59 -04:00
enum :battery_status, { unknown: 0, unplugged: 1, charging: 2, full: 3 }, suffix: true
enum :trigger, {
2024-03-15 18:27:31 -04:00
unknown: 0, background_event: 1, circular_region_event: 2, beacon_event: 3,
report_location_message_event: 4, manual_event: 5, timer_based_event: 6,
settings_monitoring_event: 7
2024-09-05 15:01:59 -04:00
}, suffix: true
enum :connection, { mobile: 0, wifi: 1, offline: 2, unknown: 4 }, suffix: true
2024-03-15 18:27:31 -04:00
scope :reverse_geocoded, -> { where.not(reverse_geocoded_at: nil) }
scope :not_reverse_geocoded, -> { where(reverse_geocoded_at: nil) }
scope :visited, -> { where.not(visit_id: nil) }
scope :not_visited, -> { where(visit_id: nil) }
2025-06-29 05:49:44 -04:00
after_create :async_reverse_geocode, if: -> { DawarichSettings.store_geodata? && !reverse_geocoded? }
2025-05-16 12:51:48 -04:00
after_create :set_country
after_create_commit :broadcast_coordinates
2025-07-15 16:26:01 -04:00
after_create_commit :trigger_track_processing, if: -> { import_id.nil? }
2025-07-04 13:49:56 -04:00
after_commit :recalculate_track, on: :update
2024-03-15 18:27:31 -04:00
2024-05-23 14:12:23 -04:00
def self.without_raw_data
select(column_names - ['raw_data'])
end
def recorded_at
2025-03-09 15:07:39 -04:00
@recorded_at ||= Time.zone.at(timestamp)
2024-05-23 14:12:23 -04:00
end
def async_reverse_geocode
return unless DawarichSettings.reverse_geocoding_enabled?
2024-08-05 15:23:08 -04:00
ReverseGeocodingJob.perform_later(self.class.to_s, id)
2024-03-15 18:27:31 -04:00
end
def reverse_geocoded?
reverse_geocoded_at.present?
end
def lon
2025-02-22 17:14:23 -05:00
lonlat.x
end
def lat
2025-02-22 17:14:23 -05:00
lonlat.y
end
2025-05-16 12:51:48 -04:00
def found_in_country
Country.containing_point(lon, lat)
end
private
2025-02-22 17:14:23 -05:00
# rubocop:disable Metrics/MethodLength Metrics/AbcSize
def broadcast_coordinates
PointsChannel.broadcast_to(
user,
[
2025-02-22 17:14:23 -05:00
lat,
lon,
battery.to_s,
altitude.to_s,
timestamp.to_s,
velocity.to_s,
id.to_s,
2025-06-28 06:22:56 -04:00
country_name.to_s
]
)
end
2025-02-22 17:14:23 -05:00
# rubocop:enable Metrics/MethodLength
2025-05-16 12:51:48 -04:00
def set_country
self.country_id = found_in_country&.id
save! if changed?
end
2025-06-28 06:22:56 -04:00
def country_name
2025-07-12 07:43:15 -04:00
# We have a country column in the database,
# but we also have a country_id column.
# TODO: rename country column to country_name
2025-06-28 06:22:56 -04:00
self.country&.name || read_attribute(:country) || ''
end
2025-07-04 13:49:56 -04:00
def recalculate_track
return unless track.present?
track.recalculate_path_and_distance!
end
2025-07-07 15:48:07 -04:00
2025-07-15 16:26:01 -04:00
def trigger_track_processing
# Smart track processing: immediate for track boundaries, batched for continuous tracking
previous_point = user.points.where('timestamp < ?', timestamp)
.order(timestamp: :desc)
.first
if should_trigger_immediate_processing?(previous_point)
# Process immediately for obvious track boundaries
TrackProcessingJob.perform_now(user_id, 'incremental', point_id: id)
else
# Batch processing for continuous tracking (reduces job queue load)
TrackProcessingJob.perform_later(user_id, 'incremental', point_id: id)
end
end
def should_trigger_immediate_processing?(previous_point)
return true if previous_point.nil?
# Immediate processing for obvious track boundaries
time_diff = timestamp - previous_point.timestamp
return true if time_diff > 30.minutes # Long gap = likely new track
# Calculate distance for large jumps
distance_km = Geocoder::Calculations.distance_between(
[previous_point.lat, previous_point.lon],
[lat, lon],
units: :km
)
return true if distance_km > 1.0 # Large jump = likely new track
false
2025-07-07 15:48:07 -04:00
end
2024-03-15 18:27:31 -04:00
end