mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Add timezone validation to Stats::DailyDistanceQuery
This commit is contained in:
parent
dfec1afd7e
commit
25a185b206
1 changed files with 12 additions and 6 deletions
|
|
@ -1,10 +1,10 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Stats::DailyDistanceQuery
|
class Stats::DailyDistanceQuery
|
||||||
def initialize(monthly_points, timespan, user_timezone = nil)
|
def initialize(monthly_points, timespan, timezone = nil)
|
||||||
@monthly_points = monthly_points
|
@monthly_points = monthly_points
|
||||||
@timespan = timespan
|
@timespan = timespan
|
||||||
@user_timezone = user_timezone || 'UTC'
|
@timezone = validate_timezone(timezone)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
|
|
@ -16,22 +16,22 @@ class Stats::DailyDistanceQuery
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_reader :monthly_points, :timespan, :user_timezone
|
attr_reader :monthly_points, :timespan, :timezone
|
||||||
|
|
||||||
def daily_distances(monthly_points)
|
def daily_distances(monthly_points)
|
||||||
Stat.connection.select_all(<<-SQL.squish)
|
Stat.connection.select_all(<<-SQL.squish)
|
||||||
WITH points_with_distances AS (
|
WITH points_with_distances AS (
|
||||||
SELECT
|
SELECT
|
||||||
EXTRACT(day FROM (to_timestamp(timestamp) AT TIME ZONE 'UTC' AT TIME ZONE '#{user_timezone}')) as day_of_month,
|
EXTRACT(day FROM (to_timestamp(timestamp) AT TIME ZONE 'UTC' AT TIME ZONE '#{timezone}')) as day_of_month,
|
||||||
CASE
|
CASE
|
||||||
WHEN LAG(lonlat) OVER (
|
WHEN LAG(lonlat) OVER (
|
||||||
PARTITION BY EXTRACT(day FROM (to_timestamp(timestamp) AT TIME ZONE 'UTC' AT TIME ZONE '#{user_timezone}'))
|
PARTITION BY EXTRACT(day FROM (to_timestamp(timestamp) AT TIME ZONE 'UTC' AT TIME ZONE '#{timezone}'))
|
||||||
ORDER BY timestamp
|
ORDER BY timestamp
|
||||||
) IS NOT NULL THEN
|
) IS NOT NULL THEN
|
||||||
ST_Distance(
|
ST_Distance(
|
||||||
lonlat::geography,
|
lonlat::geography,
|
||||||
LAG(lonlat) OVER (
|
LAG(lonlat) OVER (
|
||||||
PARTITION BY EXTRACT(day FROM (to_timestamp(timestamp) AT TIME ZONE 'UTC' AT TIME ZONE '#{user_timezone}'))
|
PARTITION BY EXTRACT(day FROM (to_timestamp(timestamp) AT TIME ZONE 'UTC' AT TIME ZONE '#{timezone}'))
|
||||||
ORDER BY timestamp
|
ORDER BY timestamp
|
||||||
)::geography
|
)::geography
|
||||||
)
|
)
|
||||||
|
|
@ -62,4 +62,10 @@ class Stats::DailyDistanceQuery
|
||||||
[index, distance_meters.to_i]
|
[index, distance_meters.to_i]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_timezone(timezone)
|
||||||
|
return timezone if ActiveSupport::TimeZone.all.any? { |tz| tz.name == timezone }
|
||||||
|
|
||||||
|
'UTC'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue