Skip to content

Commit 595bf04

Browse files
gnzjgoampcode-com
andauthored
Fix KPI hourly granularity with previous period (#208)
Amp-Thread-ID: https://ampcode.com/threads/T-019c4ebf-6034-7789-a533-627df02d1097 Co-authored-by: Amp <amp@ampcode.com>
1 parent f41138f commit 595bf04

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{"timestamp":"2026-02-10 10:10:00","session_id":"session-yesterday-1","action":"page_hit","version":"v1","payload":"{\"domain\":\"example.com\",\"location\":\"US\",\"referrer\":\"https://google.com\",\"pathname\":\"/\",\"href\":\"https://example.com/\",\"user-agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36\"}","tenant_id":"test-tenant","domain":"example.com"}
2+
{"timestamp":"2026-02-10 11:45:00","session_id":"session-yesterday-2","action":"page_hit","version":"v1","payload":"{\"domain\":\"example.com\",\"location\":\"US\",\"referrer\":\"https://google.com\",\"pathname\":\"/pricing\",\"href\":\"https://example.com/pricing\",\"user-agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36\"}","tenant_id":"test-tenant","domain":"example.com"}
3+
{"timestamp":"2026-02-11 10:15:00","session_id":"session-today-1","action":"page_hit","version":"v1","payload":"{\"domain\":\"example.com\",\"location\":\"US\",\"referrer\":\"https://news.ycombinator.com\",\"pathname\":\"/\",\"href\":\"https://example.com/\",\"user-agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36\"}","tenant_id":"test-tenant","domain":"example.com"}
4+
{"timestamp":"2026-02-11 11:20:00","session_id":"session-today-2","action":"page_hit","version":"v1","payload":"{\"domain\":\"example.com\",\"location\":\"US\",\"referrer\":\"https://news.ycombinator.com\",\"pathname\":\"/docs\",\"href\":\"https://example.com/docs\",\"user-agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36\"}","tenant_id":"test-tenant","domain":"example.com"}

tinybird/src/endpoints.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ export const kpis = defineEndpoint("kpis", {
379379
FROM timeseries a
380380
LEFT JOIN current_period_data b ON a.date = b.date
381381
LEFT JOIN previous_period_data p ON a.date = timestampAdd(p.date, interval (SELECT period_days FROM date_calculations) day)
382-
WHERE a.date >= (SELECT current_start FROM date_calculations) AND a.date <= (SELECT current_end FROM date_calculations)
382+
WHERE a.date >= (SELECT current_start FROM date_calculations)
383+
AND a.date <= timestampAdd((SELECT current_end FROM date_calculations), interval 1 day)
383384
{% else %}
384385
SELECT
385386
a.date date,

0 commit comments

Comments
 (0)