2024-04-26 12:59:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-04-06 14:46:10 -04:00
|
|
|
class User < ApplicationRecord
|
|
|
|
|
# Include default devise modules. Others available are:
|
|
|
|
|
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
|
|
|
|
devise :database_authenticatable, :registerable,
|
|
|
|
|
:recoverable, :rememberable, :validatable
|
2024-03-15 18:27:31 -04:00
|
|
|
|
|
|
|
|
has_many :imports, dependent: :destroy
|
|
|
|
|
has_many :points, through: :imports
|
2024-04-26 12:59:58 -04:00
|
|
|
has_many :stats, dependent: :destroy
|
2024-05-25 07:26:56 -04:00
|
|
|
has_many :tracked_points, class_name: 'Point', dependent: :destroy
|
2024-03-23 16:16:11 -04:00
|
|
|
|
2024-04-04 14:14:11 -04:00
|
|
|
after_create :create_api_key
|
|
|
|
|
|
2024-05-23 14:12:23 -04:00
|
|
|
def export_data(start_at: nil, end_at: nil)
|
|
|
|
|
geopoints = time_framed_points(start_at, end_at)
|
|
|
|
|
|
|
|
|
|
::ExportSerializer.new(geopoints, email).call
|
2024-04-26 12:59:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def countries_visited
|
|
|
|
|
stats.pluck(:toponyms).flatten.map { _1['country'] }.uniq.compact
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def cities_visited
|
|
|
|
|
stats
|
|
|
|
|
.where.not(toponyms: nil)
|
|
|
|
|
.pluck(:toponyms)
|
|
|
|
|
.flatten
|
|
|
|
|
.reject { |toponym| toponym['cities'].blank? }
|
|
|
|
|
.pluck('cities')
|
|
|
|
|
.flatten
|
|
|
|
|
.pluck('city')
|
|
|
|
|
.uniq
|
|
|
|
|
.compact
|
2024-03-23 16:46:18 -04:00
|
|
|
end
|
|
|
|
|
|
2024-03-23 16:16:11 -04:00
|
|
|
def total_km
|
2024-04-26 12:59:58 -04:00
|
|
|
stats.sum(:distance)
|
2024-03-23 16:16:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def total_countries
|
2024-04-26 12:59:58 -04:00
|
|
|
countries_visited.size
|
2024-03-23 16:16:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def total_cities
|
2024-04-26 12:59:58 -04:00
|
|
|
cities_visited.size
|
2024-03-23 16:16:11 -04:00
|
|
|
end
|
2024-03-24 13:55:35 -04:00
|
|
|
|
|
|
|
|
def total_reverse_geocoded
|
2024-05-23 14:12:23 -04:00
|
|
|
points.select(:id).where.not(country: nil, city: nil).count
|
2024-03-24 13:55:35 -04:00
|
|
|
end
|
2024-04-04 14:14:11 -04:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def create_api_key
|
|
|
|
|
self.api_key = SecureRandom.hex(16)
|
2024-05-23 14:12:23 -04:00
|
|
|
|
2024-04-04 14:14:11 -04:00
|
|
|
save
|
|
|
|
|
end
|
2024-05-23 14:12:23 -04:00
|
|
|
|
|
|
|
|
def time_framed_points(start_at, end_at)
|
2024-05-30 17:36:12 -04:00
|
|
|
return tracked_points.without_raw_data if start_at.nil? && end_at.nil?
|
2024-05-23 14:12:23 -04:00
|
|
|
|
|
|
|
|
if start_at && end_at
|
2024-05-30 17:36:12 -04:00
|
|
|
tracked_points.without_raw_data.where('timestamp >= ? AND timestamp <= ?', start_at, end_at)
|
2024-05-23 14:12:23 -04:00
|
|
|
elsif start_at
|
2024-05-30 17:36:12 -04:00
|
|
|
tracked_points.without_raw_data.where('timestamp >= ?', start_at)
|
2024-05-23 14:12:23 -04:00
|
|
|
elsif end_at
|
2024-05-30 17:36:12 -04:00
|
|
|
tracked_points.without_raw_data.where('timestamp <= ?', end_at)
|
2024-05-23 14:12:23 -04:00
|
|
|
end
|
|
|
|
|
end
|
2022-04-06 14:46:10 -04:00
|
|
|
end
|