Support all time resolutions on all counter views
This commit is contained in:
73
app/queries/weekly_stats.py
Normal file
73
app/queries/weekly_stats.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import logging
|
||||
from queries.connection import connection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_all_weekly_analytics(end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date, 'weekday 0'))
|
||||
UNION ALL
|
||||
SELECT date(d, '-7 day') as d
|
||||
FROM timeseries
|
||||
WHERE d > date(:end_date, '-30 days')
|
||||
),
|
||||
weeks AS (
|
||||
SELECT strftime('%W',d) as w
|
||||
FROM timeseries
|
||||
),
|
||||
stats AS (
|
||||
SELECT
|
||||
strftime('%W', timestamp) as w,
|
||||
counter_id,
|
||||
sum(increment) as count
|
||||
FROM entries
|
||||
group by counter_id, strftime('%W', timestamp)
|
||||
)
|
||||
select
|
||||
s.w as week,
|
||||
case
|
||||
when counter_id is null then json_object()
|
||||
else json_group_object(name, count)
|
||||
end as counters
|
||||
FROM weeks s
|
||||
left outer join stats t on s.w = t.w
|
||||
left join counters c on t.counter_id = c.id
|
||||
GROUP by s.w
|
||||
''', params={"end_date": end_date}, ttl=0)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
|
||||
def get_weekly_analytics(counter_id:int, end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date, 'weekday 0'))
|
||||
UNION ALL
|
||||
SELECT date(d, '-7 day')
|
||||
FROM timeseries
|
||||
WHERE d > date(:end_date, '-30 days')
|
||||
),
|
||||
weeks AS (
|
||||
SELECT strftime('%W',d) as w
|
||||
FROM timeseries
|
||||
),
|
||||
stats AS (
|
||||
SELECT
|
||||
strftime('%W', timestamp) as w,
|
||||
sum(increment) as count
|
||||
FROM entries
|
||||
where counter_id = :id
|
||||
group by strftime('%W', timestamp)
|
||||
)
|
||||
SELECT
|
||||
w.w as "week",
|
||||
coalesce(s.count, 0) as count
|
||||
FROM weeks as w
|
||||
LEFT JOIN stats as s on s.w = w.w
|
||||
''', params={'id': counter_id, "end_date": end_date}, ttl=0)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
Reference in New Issue
Block a user