dawarich/app/queries/stats_query.rb

44 lines
922 B
Ruby
Raw Normal View History

2025-07-22 13:43:27 -04:00
# frozen_string_literal: true
class StatsQuery
def initialize(user)
@user = user
end
def points_stats
2025-09-05 13:39:50 -04:00
cached_stats = Rails.cache.fetch("dawarich/user_#{user.id}_points_geocoded_stats", expires_in: 1.day) do
cached_points_geocoded_stats
end
{
total: user.points_count,
geocoded: cached_stats[:geocoded],
without_data: cached_stats[:without_data]
}
end
private
attr_reader :user
def cached_points_geocoded_stats
2025-07-22 13:56:12 -04:00
sql = ActiveRecord::Base.sanitize_sql_array([
<<~SQL.squish,
SELECT
COUNT(reverse_geocoded_at) as geocoded,
COUNT(CASE WHEN geodata = '{}'::jsonb THEN 1 END) as without_data
FROM points
WHERE user_id = ?
SQL
user.id
])
2025-07-22 13:43:27 -04:00
2025-07-22 13:56:12 -04:00
result = Point.connection.select_one(sql)
2025-07-22 13:43:27 -04:00
{
2025-07-22 13:56:12 -04:00
geocoded: result['geocoded'].to_i,
without_data: result['without_data'].to_i
2025-07-22 13:43:27 -04:00
}
end
end