|
@@ -20,6 +20,7 @@ WHERE activity = 'Eating'
|
|
|
GROUP BY delta.activity);
|
|
|
|
|
|
SELECT 'chart' AS component
|
|
|
+
|
|
|
, :total_time || ' min.' AS title
|
|
|
, TRUE AS time
|
|
|
, 'area' AS type
|
|
@@ -29,70 +30,47 @@ SELECT 'chart' AS component
|
|
|
, '100' AS height
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
, 'azure' AS color
|
|
|
-, 'green' AS color
|
|
|
, 'red' AS color
|
|
|
+, 'green' AS color
|
|
|
;
|
|
|
|
|
|
|
|
|
-SELECT :start_of_day AS x, NULL as y, 'Unknown' AS series;
|
|
|
|
|
|
|
|
|
-SELECT :end_of_day AS x, NULL as y, 'Unknown' AS series;
|
|
|
-SELECT datetime(CASE y
|
|
|
- WHEN 0 THEN datetime(x, '-00:00:01.000')
|
|
|
- WHEN 2 THEN datetime(x, '+00:00:01.000')
|
|
|
- ELSE x
|
|
|
-END, 'localtime') AS x, series, CASE y % 2 WHEN 1 THEN 1 ELSE NULL END AS y FROM (
|
|
|
-SELECT ts AS x
|
|
|
-, activity AS series
|
|
|
-, 0 AS y
|
|
|
-FROM activities a
|
|
|
-WHERE user = :user
|
|
|
-AND datetime(datetime(ts, 'localtime'), 'start of day') = :start_of_day
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-UNION
|
|
|
-SELECT ts AS x
|
|
|
-, activity AS series
|
|
|
-, 1 AS y
|
|
|
-FROM activities a
|
|
|
-WHERE user = :user
|
|
|
-AND datetime(datetime(ts, 'localtime'), 'start of day') = :start_of_day
|
|
|
-UNION
|
|
|
-SELECT COALESCE((
|
|
|
- SELECT min(lag.ts, datetime(:end_of_day, 'utc'))
|
|
|
- FROM activities lag
|
|
|
- WHERE lag.ts > a.ts AND lag.activity <> a.activity
|
|
|
-
|
|
|
- ORDER BY lag.ts ASC
|
|
|
- LIMIT 1
|
|
|
-), min(CURRENT_TIMESTAMP, datetime(:end_of_day, 'utc'))) AS x
|
|
|
-, activity AS series
|
|
|
-, 1 AS y
|
|
|
-FROM activities a
|
|
|
-WHERE user = :user
|
|
|
-AND datetime(datetime(ts, 'localtime'), 'start of day') = :start_of_day
|
|
|
-UNION
|
|
|
-SELECT COALESCE((
|
|
|
- SELECT min(lag.ts, datetime(:end_of_day, 'utc'))
|
|
|
- FROM activities lag
|
|
|
- WHERE lag.ts > a.ts AND lag.activity <> a.activity
|
|
|
-
|
|
|
- ORDER BY lag.ts ASC
|
|
|
- LIMIT 1
|
|
|
-), min(CURRENT_TIMESTAMP, datetime(:end_of_day, 'utc'))) AS x
|
|
|
-, activity AS series
|
|
|
-, 2 AS y
|
|
|
-FROM activities a
|
|
|
-WHERE user = :user
|
|
|
-AND datetime(datetime(ts, 'localtime'), 'start of day') = :start_of_day
|
|
|
+WITH plot AS (
|
|
|
+ SELECT ROW_NUMBER() OVER (ORDER BY ts) AS row
|
|
|
+ , ts
|
|
|
+ , activity
|
|
|
+ FROM (
|
|
|
+ SELECT ts, activity FROM activities
|
|
|
+ WHERE user = :user
|
|
|
+ AND datetime(ts, 'localtime') BETWEEN :start_of_day AND :end_of_day
|
|
|
+ UNION
|
|
|
+ SELECT datetime(:start_of_day, 'utc'), 'Unknown'
|
|
|
+ UNION
|
|
|
+ SELECT datetime(:end_of_day, 'utc'), 'Unknown'
|
|
|
+ )
|
|
|
+)
|
|
|
+SELECT datetime(x, 'localtime') AS x, CASE value WHEN 0 THEN NULL ELSE value END AS y, series FROM (
|
|
|
+ SELECT datetime(next.ts, '-00:00:01.000') AS x, 0 AS value, next.activity AS series
|
|
|
+ FROM plot, plot next
|
|
|
+ WHERE plot.row = next.row - 1
|
|
|
+ AND plot.activity <> next.activity
|
|
|
+ UNION
|
|
|
+ SELECT ts AS x, 1 AS value, activity AS series
|
|
|
+ FROM plot
|
|
|
+ UNION
|
|
|
+ SELECT datetime(next.ts, '-00:00:01.000') AS x, 1 AS value, plot.activity AS series
|
|
|
+ FROM plot, plot next
|
|
|
+ WHERE plot.row = next.row - 1
|
|
|
+ AND plot.activity <> next.activity
|
|
|
+ UNION
|
|
|
+ SELECT next.ts AS x, 0 AS value, plot.activity AS series
|
|
|
+ FROM plot, plot next
|
|
|
+ WHERE plot.row = next.row - 1
|
|
|
+ AND plot.activity <> next.activity
|
|
|
) q
|
|
|
-ORDER BY series, x ASC, y ASC
|
|
|
+ORDER BY series DESC, x ASC
|
|
|
;
|