2024-03-24 13:55:35 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-03-23 15:29:55 -04:00
|
|
|
class Stat < ApplicationRecord
|
|
|
|
|
validates :year, :month, presence: true
|
|
|
|
|
|
|
|
|
|
belongs_to :user
|
2024-03-24 13:55:35 -04:00
|
|
|
|
|
|
|
|
def distance_by_day
|
|
|
|
|
timespan.to_a.map.with_index(1) do |day, index|
|
|
|
|
|
beginning_of_day = day.beginning_of_day.to_i
|
|
|
|
|
end_of_day = day.end_of_day.to_i
|
|
|
|
|
|
|
|
|
|
# We have to filter by user as well
|
2024-10-24 10:59:15 -04:00
|
|
|
points = user
|
|
|
|
|
.tracked_points
|
|
|
|
|
.without_raw_data
|
|
|
|
|
.order(timestamp: :asc)
|
|
|
|
|
.where(timestamp: beginning_of_day..end_of_day)
|
2024-03-24 13:55:35 -04:00
|
|
|
|
2024-04-02 17:20:25 -04:00
|
|
|
data = { day: index, distance: 0 }
|
|
|
|
|
|
2024-03-24 13:55:35 -04:00
|
|
|
points.each_cons(2) do |point1, point2|
|
|
|
|
|
distance = Geocoder::Calculations.distance_between(
|
2024-10-02 15:29:56 -04:00
|
|
|
point1.to_coordinates, point2.to_coordinates, units: ::DISTANCE_UNIT
|
2024-03-24 13:55:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
data[:distance] += distance
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
[data[:day], data[:distance].round(2)]
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-03-24 14:46:55 -04:00
|
|
|
|
2024-05-25 07:45:49 -04:00
|
|
|
def self.year_distance(year, user)
|
|
|
|
|
stats = where(year:, user:).order(:month)
|
2024-03-24 14:46:55 -04:00
|
|
|
|
2024-03-28 10:11:59 -04:00
|
|
|
(1..12).to_a.map do |month|
|
|
|
|
|
month_stat = stats.select { |stat| stat.month == month }.first
|
|
|
|
|
|
|
|
|
|
month_name = Date::MONTHNAMES[month]
|
|
|
|
|
distance = month_stat&.distance || 0
|
|
|
|
|
|
|
|
|
|
[month_name, distance]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-05-25 07:26:56 -04:00
|
|
|
def self.year_cities_and_countries(year, user)
|
2024-06-07 15:22:57 -04:00
|
|
|
start_at = DateTime.new(year).beginning_of_year
|
|
|
|
|
end_at = DateTime.new(year).end_of_year
|
|
|
|
|
|
|
|
|
|
points = user.tracked_points.without_raw_data.where(timestamp: start_at..end_at)
|
2024-03-28 10:11:59 -04:00
|
|
|
|
|
|
|
|
data = CountriesAndCities.new(points).call
|
|
|
|
|
|
2024-04-26 12:59:58 -04:00
|
|
|
{
|
|
|
|
|
countries: data.map { _1[:country] }.uniq.count,
|
2024-06-07 15:22:57 -04:00
|
|
|
cities: data.sum { _1[:cities].count }
|
2024-04-26 12:59:58 -04:00
|
|
|
}
|
2024-03-24 14:46:55 -04:00
|
|
|
end
|
2024-04-02 11:37:38 -04:00
|
|
|
|
|
|
|
|
def self.years
|
2024-04-02 17:20:25 -04:00
|
|
|
starting_year = select(:year).min&.year || Time.current.year
|
2024-04-02 11:37:38 -04:00
|
|
|
|
|
|
|
|
(starting_year..Time.current.year).to_a.reverse
|
|
|
|
|
end
|
2024-04-02 17:20:25 -04:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def timespan
|
|
|
|
|
DateTime.new(year, month).beginning_of_month..DateTime.new(year, month).end_of_month
|
|
|
|
|
end
|
2024-03-23 15:29:55 -04:00
|
|
|
end
|