From e9678fd091a679fcfed61916a293834b5b9191d4 Mon Sep 17 00:00:00 2001 From: "gonzalo@tinybird.co" Date: Thu, 15 Feb 2024 11:33:07 +0100 Subject: [PATCH] Sync Data Project --- .../datasources/analytics_events.datasource | 12 +- .../datasources/analytics_pages_mv.datasource | 12 ++ .../analytics_sessions_mv.datasource | 13 ++ .../analytics_sources_mv.datasource | 12 ++ tinybird/pipes/analytics_hits.pipe | 82 ++++----- tinybird/pipes/analytics_pages.pipe | 28 ++-- tinybird/pipes/analytics_sessions.pipe | 27 ++- tinybird/pipes/analytics_sources.pipe | 33 ++-- tinybird/pipes/kpis.pipe | 155 ++++++++++-------- tinybird/pipes/top_browsers.pipe | 42 +++-- tinybird/pipes/top_devices.pipe | 42 +++-- tinybird/pipes/top_locations.pipe | 42 +++-- tinybird/pipes/top_pages.pipe | 42 +++-- tinybird/pipes/top_sources.pipe | 42 +++-- tinybird/pipes/trend.pipe | 36 +--- 15 files changed, 311 insertions(+), 309 deletions(-) create mode 100644 tinybird/datasources/analytics_pages_mv.datasource create mode 100644 tinybird/datasources/analytics_sessions_mv.datasource create mode 100644 tinybird/datasources/analytics_sources_mv.datasource diff --git a/tinybird/datasources/analytics_events.datasource b/tinybird/datasources/analytics_events.datasource index 8fbdd76..0c3b5b9 100644 --- a/tinybird/datasources/analytics_events.datasource +++ b/tinybird/datasources/analytics_events.datasource @@ -1,8 +1,8 @@ -TOKEN tracker APPEND - DESCRIPTION > Analytics events landing data source +TOKEN "tracker" APPEND + SCHEMA > `timestamp` DateTime `json:$.timestamp`, `session_id` String `json:$.session_id`, @@ -10,7 +10,7 @@ SCHEMA > `version` LowCardinality(String) `json:$.version`, `payload` String `json:$.payload` -ENGINE "MergeTree" -ENGINE_PARTITION_KEY "toYYYYMM(timestamp)" -ENGINE_SORTING_KEY "timestamp" -ENGINE_TTL "timestamp + toIntervalDay(60)" +ENGINE MergeTree +ENGINE_PARTITION_KEY toYYYYMM(timestamp) +ENGINE_SORTING_KEY timestamp +ENGINE_TTL timestamp + toIntervalDay(60) diff --git a/tinybird/datasources/analytics_pages_mv.datasource b/tinybird/datasources/analytics_pages_mv.datasource new file mode 100644 index 0000000..bc8b5f2 --- /dev/null +++ b/tinybird/datasources/analytics_pages_mv.datasource @@ -0,0 +1,12 @@ +SCHEMA > + `date` Date, + `device` String, + `browser` String, + `location` String, + `pathname` String, + `visits` AggregateFunction(uniq, String), + `hits` AggregateFunction(count) + +ENGINE AggregatingMergeTree +ENGINE_PARTITION_KEY toYYYYMM(date) +ENGINE_SORTING_KEY date, device, browser, location, pathname diff --git a/tinybird/datasources/analytics_sessions_mv.datasource b/tinybird/datasources/analytics_sessions_mv.datasource new file mode 100644 index 0000000..434b98f --- /dev/null +++ b/tinybird/datasources/analytics_sessions_mv.datasource @@ -0,0 +1,13 @@ +SCHEMA > + `date` Date, + `session_id` String, + `device` SimpleAggregateFunction(any, String), + `browser` SimpleAggregateFunction(any, String), + `location` SimpleAggregateFunction(any, String), + `first_hit` SimpleAggregateFunction(min, DateTime), + `latest_hit` SimpleAggregateFunction(max, DateTime), + `hits` AggregateFunction(count) + +ENGINE AggregatingMergeTree +ENGINE_PARTITION_KEY toYYYYMM(date) +ENGINE_SORTING_KEY date, session_id diff --git a/tinybird/datasources/analytics_sources_mv.datasource b/tinybird/datasources/analytics_sources_mv.datasource new file mode 100644 index 0000000..b756300 --- /dev/null +++ b/tinybird/datasources/analytics_sources_mv.datasource @@ -0,0 +1,12 @@ +SCHEMA > + `date` Date, + `device` String, + `browser` String, + `location` String, + `referrer` String, + `visits` AggregateFunction(uniq, String), + `hits` AggregateFunction(count) + +ENGINE AggregatingMergeTree +ENGINE_PARTITION_KEY toYYYYMM(date) +ENGINE_SORTING_KEY date, device, browser, location, referrer diff --git a/tinybird/pipes/analytics_hits.pipe b/tinybird/pipes/analytics_hits.pipe index 900d80d..e237656 100644 --- a/tinybird/pipes/analytics_hits.pipe +++ b/tinybird/pipes/analytics_hits.pipe @@ -1,52 +1,58 @@ -TOKEN dashboard READ - DESCRIPTION > Parsed `page_hit` events, implementing `browser` and `device` detection logic. +TOKEN "dashboard" READ + NODE parsed_hits DESCRIPTION > Parse raw page_hit events SQL > SELECT - timestamp, - action, - version, - coalesce(session_id,'0') as session_id, - JSONExtractString(payload, 'locale') as locale, - JSONExtractString(payload, 'location') as location, - JSONExtractString(payload, 'referrer') as referrer, - JSONExtractString(payload, 'pathname') as pathname, - JSONExtractString(payload, 'href') as href, - lower(JSONExtractString(payload, 'user-agent')) as user_agent - FROM - analytics_events + timestamp, + action, + version, + coalesce(session_id, '0') as session_id, + JSONExtractString(payload, 'locale') as locale, + JSONExtractString(payload, 'location') as location, + JSONExtractString(payload, 'referrer') as referrer, + JSONExtractString(payload, 'pathname') as pathname, + JSONExtractString(payload, 'href') as href, + lower(JSONExtractString(payload, 'user-agent')) as user_agent + FROM analytics_events where action = 'page_hit' NODE endpoint SQL > SELECT - timestamp, - action, - version, - session_id, - location, - referrer, - pathname, - href, - case - when match(user_agent, 'wget|ahrefsbot|curl|urllib|bitdiscovery|\+https://|googlebot') then 'bot' - when match(user_agent, 'android') then 'mobile-android' - when match(user_agent, 'ipad|iphone|ipod') then 'mobile-ios' - else 'desktop' - END as device, - case - when match(user_agent, 'firefox') then 'firefox' - when match(user_agent, 'chrome|crios') then 'chrome' - when match(user_agent, 'opera') then 'opera' - when match(user_agent, 'msie|trident') then 'ie' - when match(user_agent, 'iphone|ipad|safari') then 'safari' - else 'Unknown' - END as browser - FROM - parsed_hits + timestamp, + action, + version, + session_id, + location, + referrer, + pathname, + href, + case + when match(user_agent, 'wget|ahrefsbot|curl|urllib|bitdiscovery|\+https://|googlebot') + then 'bot' + when match(user_agent, 'android') + then 'mobile-android' + when match(user_agent, 'ipad|iphone|ipod') + then 'mobile-ios' + else 'desktop' + END as device, + case + when match(user_agent, 'firefox') + then 'firefox' + when match(user_agent, 'chrome|crios') + then 'chrome' + when match(user_agent, 'opera') + then 'opera' + when match(user_agent, 'msie|trident') + then 'ie' + when match(user_agent, 'iphone|ipad|safari') + then 'safari' + else 'Unknown' + END as browser + FROM parsed_hits diff --git a/tinybird/pipes/analytics_pages.pipe b/tinybird/pipes/analytics_pages.pipe index b57d7e1..aab9ae5 100644 --- a/tinybird/pipes/analytics_pages.pipe +++ b/tinybird/pipes/analytics_pages.pipe @@ -3,22 +3,16 @@ DESCRIPTION > Aggregate by pathname and calculate session and hits SQL > - select - toDate(timestamp) as date, - device, - browser, - location, - pathname, - uniqState(session_id) as visits, - countState() as hits - from - analytics_hits - group by - date, - device, - browser, - location, - pathname + SELECT + toDate(timestamp) AS date, + device, + browser, + location, + pathname, + uniqState(session_id) AS visits, + countState() AS hits + FROM analytics_hits + GROUP BY date, device, browser, location, pathname -TYPE materialized +TYPE MATERIALIZED DATASOURCE analytics_pages_mv diff --git a/tinybird/pipes/analytics_sessions.pipe b/tinybird/pipes/analytics_sessions.pipe index 656e056..3cfd034 100644 --- a/tinybird/pipes/analytics_sessions.pipe +++ b/tinybird/pipes/analytics_sessions.pipe @@ -3,20 +3,17 @@ DESCRIPTION > Aggregate by session_id and calculate session metrics SQL > - select - toDate(timestamp) as date, - session_id, - anySimpleState(device) as device, - anySimpleState(browser) as browser, - anySimpleState(location) as location, - minSimpleState(timestamp) as first_hit, - maxSimpleState(timestamp) as latest_hit, - countState() as hits - from - analytics_hits - group by - date, - session_id + SELECT + toDate(timestamp) AS date, + session_id, + anySimpleState(device) AS device, + anySimpleState(browser) AS browser, + anySimpleState(location) AS location, + minSimpleState(timestamp) AS first_hit, + maxSimpleState(timestamp) AS latest_hit, + countState() AS hits + FROM analytics_hits + GROUP BY date, session_id -TYPE materialized +TYPE MATERIALIZED DATASOURCE analytics_sessions_mv diff --git a/tinybird/pipes/analytics_sources.pipe b/tinybird/pipes/analytics_sources.pipe index 404f978..5354dd5 100644 --- a/tinybird/pipes/analytics_sources.pipe +++ b/tinybird/pipes/analytics_sources.pipe @@ -3,25 +3,18 @@ DESCRIPTION > Aggregate by referral and calculate session and hits SQL > - with (select domainWithoutWWW(href) from analytics_hits limit 1) as currenct_domain - select - toDate(timestamp) AS date, - device, - browser, - location, - referrer, - uniqState(session_id) AS visits, - countState() AS hits - from - analytics_hits - where - domainWithoutWWW(referrer) <> currenct_domain - group by - date, - device, - browser, - location, - referrer + WITH (SELECT domainWithoutWWW(href) FROM analytics_hits LIMIT 1) AS currenct_domain + SELECT + toDate(timestamp) AS date, + device, + browser, + location, + referrer, + uniqState(session_id) AS visits, + countState() AS hits + FROM analytics_hits + WHERE domainWithoutWWW(referrer) != currenct_domain + GROUP BY date, device, browser, location, referrer -TYPE materialized +TYPE MATERIALIZED DATASOURCE analytics_sources_mv diff --git a/tinybird/pipes/kpis.pipe b/tinybird/pipes/kpis.pipe index 7a1aeef..fa85568 100644 --- a/tinybird/pipes/kpis.pipe +++ b/tinybird/pipes/kpis.pipe @@ -1,10 +1,10 @@ -TOKEN dashboard READ - DESCRIPTION > Summary with general KPIs per date, including visits, page views, bounce rate and average session duration. Accepts `date_from` and `date_to` date filter, all historical data if not passed. Daily granularity, except when filtering one single day (hourly) +TOKEN "dashboard" READ + NODE timeseries DESCRIPTION > Generate a timeseries for the specified time range, so we call fill empty data points. @@ -13,23 +13,53 @@ DESCRIPTION > SQL > % {% set _single_day = defined(date_from) and day_diff(date_from, date_to) == 0 %} - with - {% if defined(date_from) %} - toStartOfDay(toDate({{Date(date_from, description="Starting day for filtering a date range", required=False)}})) as start, - {% else %} - toStartOfDay(timestampAdd(today(), interval -7 day)) as start, - {% end %} - {% if defined(date_to) %} - toStartOfDay(toDate({{Date(date_to, description="Finishing day for filtering a date range", required=False)}})) as end - {% else %} - toStartOfDay(today()) as end - {% end %} - + {% if defined(date_from) %} + toStartOfDay( + toDate( + {{ + Date( + date_from, + description="Starting day for filtering a date range", + required=False, + ) + }} + ) + ) as start, + {% else %} toStartOfDay(timestampAdd(today(), interval -7 day)) as start, + {% end %} + {% if defined(date_to) %} + toStartOfDay( + toDate( + {{ + Date( + date_to, + description="Finishing day for filtering a date range", + required=False, + ) + }} + ) + ) as end + {% else %} toStartOfDay(today()) as end + {% end %} {% if _single_day %} - select arrayJoin(arrayMap(x -> toDateTime(x), range(toUInt32(toDateTime(start)), toUInt32(timestampAdd(end, interval 1 day)), 3600))) as date + select + arrayJoin( + arrayMap( + x -> toDateTime(x), + range( + toUInt32(toDateTime(start)), toUInt32(timestampAdd(end, interval 1 day)), 3600 + ) + ) + ) as date {% else %} - select arrayJoin(arrayMap(x -> toDate(x), range(toUInt32(start), toUInt32(timestampAdd(end, interval 1 day)), 24 * 3600))) as date + select + arrayJoin( + arrayMap( + x -> toDate(x), + range(toUInt32(start), toUInt32(timestampAdd(end, interval 1 day)), 24 * 3600) + ) + ) as date {% end %} where date <= now() @@ -40,44 +70,35 @@ DESCRIPTION > SQL > % {% if defined(date_from) and day_diff(date_from, date_to) == 0 %} - select - toStartOfHour(timestamp) as date, - session_id, - uniq(session_id) as visits, - count() as pageviews, - case when min(timestamp) = max(timestamp) then 1 else 0 end as is_bounce, - max(timestamp) as latest_hit_aux, - min(timestamp) as first_hit_aux - from - analytics_hits - where - toDate(timestamp) = {{Date(date_from)}} - group by - toStartOfHour(timestamp), session_id + select + toStartOfHour(timestamp) as date, + session_id, + uniq(session_id) as visits, + count() as pageviews, + case when min(timestamp) = max(timestamp) then 1 else 0 end as is_bounce, + max(timestamp) as latest_hit_aux, + min(timestamp) as first_hit_aux + from analytics_hits + where toDate(timestamp) = {{ Date(date_from) }} + group by toStartOfHour(timestamp), session_id {% else %} - select - date, - session_id, - uniq(session_id) as visits, - countMerge(hits) as pageviews, - case when min(first_hit) = max(latest_hit) then 1 else 0 end as is_bounce, - max(latest_hit) as latest_hit_aux, - min(first_hit) as first_hit_aux - from - analytics_sessions_mv - where - {% if defined(date_from) %} - date >= {{Date(date_from)}} - {% else %} - date >= timestampAdd(today(), interval -7 day) - {% end %} - {% if defined(date_to) %} - and date <= {{Date(date_to)}} - {% else %} - and date <= today() - {% end %} - group by - date, session_id + select + date, + session_id, + uniq(session_id) as visits, + countMerge(hits) as pageviews, + case when min(first_hit) = max(latest_hit) then 1 else 0 end as is_bounce, + max(latest_hit) as latest_hit_aux, + min(first_hit) as first_hit_aux + from analytics_sessions_mv + where + {% if defined(date_from) %} date >= {{ Date(date_from) }} + {% else %} date >= timestampAdd(today(), interval -7 day) + {% end %} + {% if defined(date_to) %} and date <= {{ Date(date_to) }} + {% else %} and date <= today() + {% end %} + group by date, session_id {% end %} NODE data @@ -85,28 +106,20 @@ DESCRIPTION > General KPIs per date, works for both summary metrics and trends charts. SQL > - select - date, - uniq(session_id) as visits, - sum(pageviews) as pageviews, - sum(case when latest_hit_aux = first_hit_aux then 1 end) / visits as bounce_rate, - avg(latest_hit_aux - first_hit_aux) as avg_session_sec - from - hits - group by - date + select + date, + uniq(session_id) as visits, + sum(pageviews) as pageviews, + sum(case when latest_hit_aux = first_hit_aux then 1 end) / visits as bounce_rate, + avg(latest_hit_aux - first_hit_aux) as avg_session_sec + from hits + group by date NODE endpoint DESCRIPTION > Join and generate timeseries with metrics SQL > - select - a.date, - b.visits, - b.pageviews, - b.bounce_rate, - b.avg_session_sec - from - timeseries a + select a.date, b.visits, b.pageviews, b.bounce_rate, b.avg_session_sec + from timeseries a left join data b using date diff --git a/tinybird/pipes/top_browsers.pipe b/tinybird/pipes/top_browsers.pipe index cbea22a..8e4d136 100644 --- a/tinybird/pipes/top_browsers.pipe +++ b/tinybird/pipes/top_browsers.pipe @@ -1,35 +1,31 @@ -TOKEN dashboard READ - DESCRIPTION > Top Browsers ordered by most visits. Accepts `date_from` and `date_to` date filter. Defaults to last 7 days. Also `skip` and `limit` parameters for pagination. +TOKEN "dashboard" READ + NODE endpoint DESCRIPTION > Group by browser and calcualte hits and visits SQL > % - select - browser, - uniqMerge(visits) as visits, - countMerge(hits) as hits - from - analytics_sources_mv + select browser, uniqMerge(visits) as visits, countMerge(hits) as hits + from analytics_sources_mv where - {% if defined(date_from) %} - date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}} - {% else %} - date >= timestampAdd(today(), interval -7 day) - {% end %} - {% if defined(date_to) %} - and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}} - {% else %} - and date <= today() - {% end %} - group by - browser - order by - visits desc - limit {{Int32(skip, 0)}},{{Int32(limit, 50)}} + {% if defined(date_from) %} + date + >= + {{ Date(date_from, description="Starting day for filtering a date range", required=False) }} + {% else %} date >= timestampAdd(today(), interval -7 day) + {% end %} + {% if defined(date_to) %} + and date + <= + {{ Date(date_to, description="Finishing day for filtering a date range", required=False) }} + {% else %} and date <= today() + {% end %} + group by browser + order by visits desc + limit {{ Int32(skip, 0) }},{{ Int32(limit, 50) }} diff --git a/tinybird/pipes/top_devices.pipe b/tinybird/pipes/top_devices.pipe index e8cf2dc..7425f66 100644 --- a/tinybird/pipes/top_devices.pipe +++ b/tinybird/pipes/top_devices.pipe @@ -1,35 +1,31 @@ -TOKEN dashboard READ - DESCRIPTION > Top Device Types ordered by most visits. Accepts `date_from` and `date_to` date filter. Defaults to last 7 days. Also `skip` and `limit` parameters for pagination. +TOKEN "dashboard" READ + NODE endpoint DESCRIPTION > Group by device and calcualte hits and visits SQL > % - select - device, - uniqMerge(visits) as visits, - countMerge(hits) as hits - from - analytics_sources_mv + select device, uniqMerge(visits) as visits, countMerge(hits) as hits + from analytics_sources_mv where - {% if defined(date_from) %} - date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}} - {% else %} - date >= timestampAdd(today(), interval -7 day) - {% end %} - {% if defined(date_to) %} - and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}} - {% else %} - and date <= today() - {% end %} - group by - device - order by - visits desc - limit {{Int32(skip, 0)}},{{Int32(limit, 50)}} + {% if defined(date_from) %} + date + >= + {{ Date(date_from, description="Starting day for filtering a date range", required=False) }} + {% else %} date >= timestampAdd(today(), interval -7 day) + {% end %} + {% if defined(date_to) %} + and date + <= + {{ Date(date_to, description="Finishing day for filtering a date range", required=False) }} + {% else %} and date <= today() + {% end %} + group by device + order by visits desc + limit {{ Int32(skip, 0) }},{{ Int32(limit, 50) }} diff --git a/tinybird/pipes/top_locations.pipe b/tinybird/pipes/top_locations.pipe index a4b8eef..d6bc1ee 100644 --- a/tinybird/pipes/top_locations.pipe +++ b/tinybird/pipes/top_locations.pipe @@ -1,35 +1,31 @@ -TOKEN dashboard READ - DESCRIPTION > Top visting Countries ordered by most visits. Accepts `date_from` and `date_to` date filter. Defaults to last 7 days. Also `skip` and `limit` parameters for pagination. +TOKEN "dashboard" READ + NODE endpoint DESCRIPTION > Group by pagepath and calcualte hits and visits SQL > % - select - location, - uniqMerge(visits) as visits, - countMerge(hits) as hits - from - analytics_pages_mv + select location, uniqMerge(visits) as visits, countMerge(hits) as hits + from analytics_pages_mv where - {% if defined(date_from) %} - date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}} - {% else %} - date >= timestampAdd(today(), interval -7 day) - {% end %} - {% if defined(date_to) %} - and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}} - {% else %} - and date <= today() - {% end %} - group by - location - order by - visits desc - limit {{Int32(skip, 0)}},{{Int32(limit, 50)}} + {% if defined(date_from) %} + date + >= + {{ Date(date_from, description="Starting day for filtering a date range", required=False) }} + {% else %} date >= timestampAdd(today(), interval -7 day) + {% end %} + {% if defined(date_to) %} + and date + <= + {{ Date(date_to, description="Finishing day for filtering a date range", required=False) }} + {% else %} and date <= today() + {% end %} + group by location + order by visits desc + limit {{ Int32(skip, 0) }},{{ Int32(limit, 50) }} diff --git a/tinybird/pipes/top_pages.pipe b/tinybird/pipes/top_pages.pipe index 9d0a93e..536e177 100644 --- a/tinybird/pipes/top_pages.pipe +++ b/tinybird/pipes/top_pages.pipe @@ -1,35 +1,31 @@ -TOKEN dashboard READ - DESCRIPTION > Most visited pages for a given period. Accepts `date_from` and `date_to` date filter. Defaults to last 7 days. Also `skip` and `limit` parameters for pagination. +TOKEN "dashboard" READ + NODE endpoint DESCRIPTION > Group by pagepath and calcualte hits and visits SQL > % - select - pathname, - uniqMerge(visits) as visits, - countMerge(hits) as hits - from - analytics_pages_mv + select pathname, uniqMerge(visits) as visits, countMerge(hits) as hits + from analytics_pages_mv where - {% if defined(date_from) %} - date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}} - {% else %} - date >= timestampAdd(today(), interval -7 day) - {% end %} - {% if defined(date_to) %} - and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}} - {% else %} - and date <= today() - {% end %} - group by - pathname - order by - visits desc - limit {{Int32(skip, 0)}},{{Int32(limit, 50)}} + {% if defined(date_from) %} + date + >= + {{ Date(date_from, description="Starting day for filtering a date range", required=False) }} + {% else %} date >= timestampAdd(today(), interval -7 day) + {% end %} + {% if defined(date_to) %} + and date + <= + {{ Date(date_to, description="Finishing day for filtering a date range", required=False) }} + {% else %} and date <= today() + {% end %} + group by pathname + order by visits desc + limit {{ Int32(skip, 0) }},{{ Int32(limit, 50) }} diff --git a/tinybird/pipes/top_sources.pipe b/tinybird/pipes/top_sources.pipe index 29ade16..5d99901 100644 --- a/tinybird/pipes/top_sources.pipe +++ b/tinybird/pipes/top_sources.pipe @@ -1,35 +1,31 @@ -TOKEN dashboard READ - DESCRIPTION > Top traffic sources (domains), ordered by most visits. Accepts `date_from` and `date_to` date filter. Defaults to last 7 days. Also `skip` and `limit` parameters for pagination. +TOKEN "dashboard" READ + NODE endpoint DESCRIPTION > Group by referral and calcualte hits and visits SQL > % - select - domainWithoutWWW(referrer) as referrer, - uniqMerge(visits) as visits, - countMerge(hits) as hits - from - analytics_sources_mv + select domainWithoutWWW(referrer) as referrer, uniqMerge(visits) as visits, countMerge(hits) as hits + from analytics_sources_mv where - {% if defined(date_from) %} - date >= {{Date(date_from, description="Starting day for filtering a date range", required=False)}} - {% else %} - date >= timestampAdd(today(), interval -7 day) - {% end %} - {% if defined(date_to) %} - and date <= {{Date(date_to, description="Finishing day for filtering a date range", required=False)}} - {% else %} - and date <= today() - {% end %} - group by - referrer - order by - visits desc - limit {{Int32(skip, 0)}},{{Int32(limit, 50)}} + {% if defined(date_from) %} + date + >= + {{ Date(date_from, description="Starting day for filtering a date range", required=False) }} + {% else %} date >= timestampAdd(today(), interval -7 day) + {% end %} + {% if defined(date_to) %} + and date + <= + {{ Date(date_to, description="Finishing day for filtering a date range", required=False) }} + {% else %} and date <= today() + {% end %} + group by referrer + order by visits desc + limit {{ Int32(skip, 0) }},{{ Int32(limit, 50) }} diff --git a/tinybird/pipes/trend.pipe b/tinybird/pipes/trend.pipe index de7d96a..df84631 100644 --- a/tinybird/pipes/trend.pipe +++ b/tinybird/pipes/trend.pipe @@ -1,9 +1,9 @@ -TOKEN dashboard READ - DESCRIPTION > Visits trend over time for the last 30 minutes, filling the blanks. Works great for the realtime chart. +TOKEN "dashboard" READ + NODE timeseries DESCRIPTION > Generate a timeseries for the last 30 minutes, so we call fill empty data points @@ -11,40 +11,22 @@ DESCRIPTION > SQL > with (now() - interval 30 minute) as start select addMinutes(toStartOfMinute(start), number) as t - from ( - select arrayJoin(range(1, 31)) as number - ) + from (select arrayJoin(range(1, 31)) as number) NODE hits DESCRIPTION > Get last 30 minutes metrics gropued by minute SQL > - select - toStartOfMinute(timestamp) as t, - uniq(session_id) as visits - from - analytics_hits - where - timestamp >= (now() - interval 30 minute) - group by - toStartOfMinute(timestamp) - order by - toStartOfMinute(timestamp) + select toStartOfMinute(timestamp) as t, uniq(session_id) as visits + from analytics_hits + where timestamp >= (now() - interval 30 minute) + group by toStartOfMinute(timestamp) + order by toStartOfMinute(timestamp) NODE endpoint DESCRIPTION > Join and generate timeseries with metrics for the last 30 minutes SQL > - select - a.t, - b.visits - from - timeseries a - left join - hits b - on - a.t = b.t - order by - a.t + select a.t, b.visits from timeseries a left join hits b on a.t = b.t order by a.t