2024-03-24 13:55:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-03-23 15:29:55 -04:00
|
|
|
class Stat < ApplicationRecord
|
2025-07-08 12:10:10 -04:00
|
|
|
include DistanceConvertible
|
|
|
|
|
|
2024-03-23 15:29:55 -04:00
|
|
|
validates :year, :month, presence: true
|
|
|
|
|
|
|
|
|
|
belongs_to :user
|
2024-03-24 13:55:35 -04:00
|
|
|
|
2025-09-11 14:41:43 -04:00
|
|
|
before_create :generate_sharing_uuid
|
|
|
|
|
|
2024-03-24 13:55:35 -04:00
|
|
|
def distance_by_day
|
2024-12-06 10:52:36 -05:00
|
|
|
monthly_points = points
|
|
|
|
|
calculate_daily_distances(monthly_points)
|
2024-03-24 13:55:35 -04:00
|
|
|
end
|
2024-03-24 14:46:55 -04:00
|
|
|
|
2024-05-25 07:45:49 -04:00
|
|
|
def self.year_distance(year, user)
|
2024-12-06 10:52:36 -05:00
|
|
|
stats_by_month = where(year:, user:).order(:month).index_by(&:month)
|
2024-03-28 10:11:59 -04:00
|
|
|
|
2024-12-06 10:52:36 -05:00
|
|
|
(1..12).map do |month|
|
2024-03-28 10:11:59 -04:00
|
|
|
month_name = Date::MONTHNAMES[month]
|
2024-12-06 10:52:36 -05:00
|
|
|
distance = stats_by_month[month]&.distance || 0
|
2024-03-28 10:11:59 -04:00
|
|
|
|
|
|
|
|
[month_name, distance]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-06 10:52:36 -05:00
|
|
|
def points
|
2025-08-21 16:32:29 -04:00
|
|
|
user.points
|
2024-12-06 10:52:36 -05:00
|
|
|
.without_raw_data
|
|
|
|
|
.where(timestamp: timespan)
|
|
|
|
|
.order(timestamp: :asc)
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-11 14:41:43 -04:00
|
|
|
def sharing_enabled?
|
|
|
|
|
sharing_settings['enabled'] == true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sharing_expired?
|
2025-09-13 10:41:28 -04:00
|
|
|
expiration = sharing_settings['expiration']
|
|
|
|
|
return false if expiration.blank? || expiration == 'permanent'
|
2025-09-11 14:41:43 -04:00
|
|
|
|
2025-09-13 10:41:28 -04:00
|
|
|
expires_at_value = sharing_settings['expires_at']
|
|
|
|
|
return true if expires_at_value.blank?
|
|
|
|
|
|
|
|
|
|
expires_at = begin
|
|
|
|
|
Time.zone.parse(expires_at_value)
|
|
|
|
|
rescue StandardError
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expires_at.present? ? Time.current > expires_at : true
|
2025-09-11 14:41:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def public_accessible?
|
|
|
|
|
sharing_enabled? && !sharing_expired?
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-18 13:45:53 -04:00
|
|
|
def hexagons_available?
|
|
|
|
|
hexagon_centers.present? &&
|
|
|
|
|
hexagon_centers.is_a?(Array) &&
|
|
|
|
|
hexagon_centers.any?
|
2025-09-13 17:11:42 -04:00
|
|
|
end
|
|
|
|
|
|
2025-09-11 14:41:43 -04:00
|
|
|
def generate_new_sharing_uuid!
|
|
|
|
|
update!(sharing_uuid: SecureRandom.uuid)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def enable_sharing!(expiration: '1h')
|
|
|
|
|
expires_at = case expiration
|
2025-09-13 10:41:28 -04:00
|
|
|
when '1h' then 1.hour.from_now
|
|
|
|
|
when '12h' then 12.hours.from_now
|
|
|
|
|
when '24h' then 24.hours.from_now
|
2025-09-11 14:41:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
update!(
|
|
|
|
|
sharing_settings: {
|
|
|
|
|
'enabled' => true,
|
|
|
|
|
'expiration' => expiration,
|
|
|
|
|
'expires_at' => expires_at&.iso8601
|
|
|
|
|
},
|
|
|
|
|
sharing_uuid: sharing_uuid || SecureRandom.uuid
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def disable_sharing!
|
|
|
|
|
update!(
|
|
|
|
|
sharing_settings: {
|
|
|
|
|
'enabled' => false,
|
|
|
|
|
'expiration' => nil,
|
|
|
|
|
'expires_at' => nil
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-12 15:08:45 -04:00
|
|
|
def calculate_data_bounds
|
|
|
|
|
start_date = Date.new(year, month, 1).beginning_of_day
|
|
|
|
|
end_date = start_date.end_of_month.end_of_day
|
2025-09-13 10:41:28 -04:00
|
|
|
|
2025-09-12 15:08:45 -04:00
|
|
|
points_relation = user.points.where(timestamp: start_date.to_i..end_date.to_i)
|
|
|
|
|
point_count = points_relation.count
|
2025-09-13 10:41:28 -04:00
|
|
|
|
2025-09-12 15:08:45 -04:00
|
|
|
return nil if point_count.zero?
|
|
|
|
|
|
|
|
|
|
bounds_result = ActiveRecord::Base.connection.exec_query(
|
2025-09-13 12:54:02 -04:00
|
|
|
"SELECT MIN(ST_Y(lonlat::geometry)) as min_lat, MAX(ST_Y(lonlat::geometry)) as max_lat,
|
|
|
|
|
MIN(ST_X(lonlat::geometry)) as min_lng, MAX(ST_X(lonlat::geometry)) as max_lng
|
2025-09-12 15:08:45 -04:00
|
|
|
FROM points
|
|
|
|
|
WHERE user_id = $1
|
2025-09-13 12:54:02 -04:00
|
|
|
AND timestamp BETWEEN $2 AND $3
|
|
|
|
|
AND lonlat IS NOT NULL",
|
2025-09-12 15:08:45 -04:00
|
|
|
'data_bounds_query',
|
|
|
|
|
[user.id, start_date.to_i, end_date.to_i]
|
|
|
|
|
).first
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
min_lat: bounds_result['min_lat'].to_f,
|
|
|
|
|
max_lat: bounds_result['max_lat'].to_f,
|
|
|
|
|
min_lng: bounds_result['min_lng'].to_f,
|
|
|
|
|
max_lng: bounds_result['max_lng'].to_f,
|
|
|
|
|
point_count: point_count
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-13 17:23:48 -04:00
|
|
|
def process!
|
|
|
|
|
Stats::CalculatingJob.perform_later(user.id, year, month)
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-02 17:20:25 -04:00
|
|
|
private
|
|
|
|
|
|
2025-09-11 14:41:43 -04:00
|
|
|
def generate_sharing_uuid
|
|
|
|
|
self.sharing_uuid ||= SecureRandom.uuid
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-02 17:20:25 -04:00
|
|
|
def timespan
|
|
|
|
|
DateTime.new(year, month).beginning_of_month..DateTime.new(year, month).end_of_month
|
|
|
|
|
end
|
2024-12-06 10:52:36 -05:00
|
|
|
|
|
|
|
|
def calculate_daily_distances(monthly_points)
|
2025-07-22 17:57:25 -04:00
|
|
|
Stats::DailyDistanceQuery.new(monthly_points, timespan, user_timezone).call
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user_timezone
|
|
|
|
|
# Future: Once user.timezone column exists, uncomment the line below
|
|
|
|
|
# user.timezone.presence || Time.zone.name
|
|
|
|
|
|
|
|
|
|
# For now, use application timezone
|
|
|
|
|
Time.zone.name
|
2024-12-06 10:52:36 -05:00
|
|
|
end
|
2024-03-23 15:29:55 -04:00
|
|
|
end
|