Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update sales timeseries endpoint

Kiran K277f2d58e2b3
diff --git a/packages/tinybird/endpoints/sales_timeseries.pipe b/packages/tinybird/endpoints/sales_timeseries.pipe
index 24b55a0897..1a6ba7ca66 100644
--- a/packages/tinybird/endpoints/sales_timeseries.pipe
+++ b/packages/tinybird/endpoints/sales_timeseries.pipe
@@ -1,14 +1,15 @@
DESCRIPTION >
Timeseries sales data
+
NODE day_intervals
SQL >
%
- WITH
- toStartOfDay(toDateTime64({{ DateTime64(start, '2022-09-25 00:00:00.000') }}, 3)) AS start,
- toStartOfDay(toDateTime64({{ DateTime64(end, '2022-12-20 00:00:00.000') }}, 3)) AS end
- SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 86400), toUInt32(end + 86400), 86400))) as interval
+ WITH
+ toStartOfDay(toDateTime64({{ DateTime64(start, '2022-09-25 00:00:00.000') }}, 3)) AS start,
+ toStartOfDay(toDateTime64({{ DateTime64(end, '2022-12-20 00:00:00.000') }}, 3)) AS end
+ SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 86400), toUInt32(end + 86400), 86400))) as interval
@@ -16,10 +17,10 @@ NODE hour_intervals
SQL >
%
- WITH
- toStartOfHour(toDateTime64({{ DateTime64(start, '2022-12-19 00:00:00.000') }}, 3)) AS start,
- toStartOfHour(toDateTime64({{ DateTime64(end, '2022-12-20 00:00:00.000') }}, 3)) AS end
- SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 3600), toUInt32(end + 3600), 3600))) as interval
+ WITH
+ toStartOfHour(toDateTime64({{ DateTime64(start, '2022-12-19 00:00:00.000') }}, 3)) AS start,
+ toStartOfHour(toDateTime64({{ DateTime64(end, '2022-12-20 00:00:00.000') }}, 3)) AS end
+ SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 3600), toUInt32(end + 3600), 3600))) as interval
@@ -27,10 +28,10 @@ NODE minute_intervals
SQL >
%
- WITH
- toStartOfMinute(toDateTime64({{ DateTime64(start, '2022-12-20 15:08:57.000') }}, 3)) AS start,
- toStartOfMinute(toDateTime64({{ DateTime64(end, '2022-12-20 16:08:57.000') }}, 3)) AS end
- SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 60), toUInt32(end + 60), 60))) as interval
+ WITH
+ toStartOfMinute(toDateTime64({{ DateTime64(start, '2022-12-20 15:08:57.000') }}, 3)) AS start,
+ toStartOfMinute(toDateTime64({{ DateTime64(end, '2022-12-20 16:08:57.000') }}, 3)) AS end
+ SELECT arrayJoin(arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 60), toUInt32(end + 60), 60))) as interval
@@ -38,15 +39,15 @@ NODE month_intervals
SQL >
%
- SELECT
- toStartOfMonth(toDateTime64({{ DateTime64(start, '2023-02-23 20:45:02.959') }}, 3)) as interval
- ORDER BY interval ASC
- WITH FILL
- FROM
- toStartOfMonth(toDateTime64({{ DateTime64(start, '2023-02-23 20:45:02.959') }}, 3))
- TO
- toStartOfMonth(addMonths(toDateTime64({{ DateTime64(end, '2023-06-06 20:45:02.959') }}, 3), 1))
- STEP interval 1 month
+ SELECT
+ toStartOfMonth(toDateTime64({{ DateTime64(start, '2023-02-23 20:45:02.959') }}, 3)) as interval
+ ORDER BY interval ASC
+ WITH FILL
+ FROM
+ toStartOfMonth(toDateTime64({{ DateTime64(start, '2023-02-23 20:45:02.959') }}, 3))
+ TO
+ toStartOfMonth(addMonths(toDateTime64({{ DateTime64(end, '2023-06-06 20:45:02.959') }}, 3), 1))
+ STEP interval 1 month
@@ -57,61 +58,62 @@ DESCRIPTION >
SQL >
%
- SELECT
- {% if granularity == "minute" %} toStartOfMinute(timestamp)
- {% elif granularity == "hour" %} toStartOfHour(timestamp)
- {% elif granularity == "month" %} toDateTime(toStartOfMonth(timestamp))
- {% else %} toDateTime(toStartOfDay(timestamp))
- {% end %} AS interval,
- uniq(*) as sales
- FROM
- dub_sale_events_mv
- {% if not defined(linkId) and defined(workspaceId) %}
- PREWHERE link_id in (
- SELECT link_id
- from dub_links_metadata_latest FINAL
- WHERE
- workspace_id
- = {{
- String(
- workspaceId,
- 'cl7wsy2836920mjrb352g5wfx',
- description="The ID of the workspace",
- required=True,
- )
- }}
- AND deleted == 0
- {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
- {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
- {% if defined(root) %}
- {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
- {% end %}
- )
- {% end %}
- WHERE
- true
- {% if defined(linkId) %}
- AND link_id
- = {{
- String(
- linkId,
- 'clsdzhhlq0003vskwcaz6nm39',
- description="The ID of the link",
+ SELECT
+ {% if granularity == "minute" %} toStartOfMinute(timestamp)
+ {% elif granularity == "hour" %} toStartOfHour(timestamp)
+ {% elif granularity == "month" %} toDateTime(toStartOfMonth(timestamp))
+ {% else %} toDateTime(toStartOfDay(timestamp))
+ {% end %} AS interval,
+ uniq(*) as sales,
+ sum(amount) as amount
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (
+ SELECT link_id
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7wsy2836920mjrb352g5wfx',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
)
- }}
- {% end %}
- {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
- {% if defined(country) %} AND country = {{ country }} {% end %}
- {% if defined(city) %} AND city = {{ city }} {% end %}
- {% if defined(device) %} AND device = {{ device }} {% end %}
- {% if defined(browser) %} AND browser = {{ browser }} {% end %}
- {% if defined(os) %} AND os = {{ os }} {% end %}
- {% if defined(referer) %} AND referer = {{ referer }} {% end %}
- {% if defined(url) %} AND url = {{ url }} {% end %}
- AND timestamp >= {{ DateTime64(start, '2023-06-25 00:00:00.000') }}
- AND timestamp < {{ DateTime64(end, '2023-10-20 00:00:00.000') }}
- GROUP BY interval
- ORDER BY interval
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr)}} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ AND timestamp >= {{ DateTime(start, '2023-06-25 00:00:00') }}
+ AND timestamp < {{ DateTime(end, '2023-10-20 00:00:00') }}
+ GROUP BY interval
+ ORDER BY interval
@@ -119,13 +121,12 @@ NODE endpoint
SQL >
%
- SELECT formatDateTime(interval, '%FT%T.000Z') as start, sales
- FROM
- {% if granularity == "minute" %} minute_intervals
- {% elif granularity == "hour" %} hour_intervals
- {% elif granularity == "month" %} month_intervals
- {% else %} day_intervals
- {% end %}
- LEFT JOIN timeseries_data USING interval
-
+ SELECT formatDateTime(interval, '%FT%T.000Z') as start, sales, timeseries_data.amount
+ FROM
+ {% if granularity == "minute" %} minute_intervals
+ {% elif granularity == "hour" %} hour_intervals
+ {% elif granularity == "month" %} month_intervals
+ {% else %} day_intervals
+ {% end %}
+ LEFT JOIN timeseries_data USING interval