-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from mjirv/feat-add_flows
Feat: add flows
- Loading branch information
Showing
7 changed files
with
186 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
integration_tests/models/product_analytics/dmt/dmt_expected__flows_simple.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
select | ||
'placed' as event_0 | ||
, 'completed' as event_1 | ||
, cast(null as varchar) as event_2 | ||
, cast(null as varchar) as event_3 | ||
, cast(null as varchar) as event_4 | ||
, cast(null as varchar) as event_5 | ||
, 3 as n_events | ||
|
||
union all | ||
|
||
select | ||
'placed' as event_0 | ||
, 'completed' as event_1 | ||
, 'returned' as event_2 | ||
, 'returned' as event_3 | ||
, cast(null as varchar) as event_4 | ||
, cast(null as varchar) as event_5 | ||
, 1 as n_events | ||
|
||
union all | ||
|
||
select | ||
'placed' as event_0 | ||
, 'completed' as event_1 | ||
, 'returned' as event_2 | ||
, cast(null as varchar) as event_3 | ||
, cast(null as varchar) as event_4 | ||
, cast(null as varchar) as event_5 | ||
, 1 as n_events | ||
|
||
union all | ||
|
||
select | ||
'placed' as event_0 | ||
, 'returned' as event_1 | ||
, cast(null as varchar) as event_2 | ||
, cast(null as varchar) as event_3 | ||
, cast(null as varchar) as event_4 | ||
, cast(null as varchar) as event_5 | ||
, 1 as n_events |
19 changes: 19 additions & 0 deletions
19
integration_tests/models/product_analytics/dmt/dmt_expected__flows_simple_before.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
select | ||
cast(null as varchar) as event_5 | ||
, cast(null as varchar) as event_4 | ||
, cast(null as varchar) as event_3 | ||
, cast(null as varchar) as event_2 | ||
, cast(null as varchar) as event_1 | ||
, 'placed' as event_0 | ||
, 5 as n_events | ||
|
||
union all | ||
|
||
select | ||
cast(null as varchar) as event_5 | ||
, cast(null as varchar) as event_4 | ||
, cast(null as varchar) as event_3 | ||
, 'completed' as event_2 | ||
, 'returned' as event_1 | ||
, 'placed' as event_0 | ||
, 1 as n_events |
16 changes: 16 additions & 0 deletions
16
integration_tests/models/product_analytics/flows_orders.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% set events = | ||
dbt_product_analytics.event_stream( | ||
from=ref('orders'), | ||
event_type_col="status", | ||
user_id_col="customer_id", | ||
date_col="order_date", | ||
start_date="2018-01-01", | ||
end_date="2019-01-01") | ||
%} | ||
|
||
{{ | ||
dbt_product_analytics.flows( | ||
event_stream=events, | ||
primary_event='placed' | ||
) | ||
}} |
17 changes: 17 additions & 0 deletions
17
integration_tests/models/product_analytics/flows_orders_before.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% set events = | ||
dbt_product_analytics.event_stream( | ||
from=ref('orders'), | ||
event_type_col="status", | ||
user_id_col="customer_id", | ||
date_col="order_date", | ||
start_date="2018-01-01", | ||
end_date="2019-01-01") | ||
%} | ||
|
||
{{ | ||
dbt_product_analytics.flows( | ||
event_stream=events, | ||
primary_event='placed', | ||
before_or_after='before' | ||
) | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{% macro flows(event_stream=None, primary_event=None, n_events_from=5, before_or_after='after', top_n=20) %} | ||
|
||
{% if event_stream is none %} | ||
{{ exceptions.raise_compiler_error('parameter \'event_stream\' must be provided')}} | ||
{% endif %} | ||
|
||
{% if primary_event is none %} | ||
{{ exceptions.raise_compiler_error('parameter \'primary_event\' must be provided')}} | ||
{% endif %} | ||
|
||
with event_stream as {{ dbt_product_analytics._select_event_stream(event_stream) }} | ||
|
||
, flow_events as ( | ||
select | ||
{% if before_or_after == 'after' %} event_type as event_0 {% endif %} | ||
{% for i in range(n_events_from) %} | ||
{% if before_or_after == 'before' %} {% set index = n_events_from - i %} {% else %} {% set index = i + 1 %} {% endif %} | ||
{% if before_or_after == 'before' %}{% if not loop.first %},{% endif %}lag{% else %}, lead{% endif %}(event_type, {{ index }}) over(partition by user_id order by event_date) as event_{{ index }} | ||
{% endfor %} | ||
{% if before_or_after == 'before' %}, event_type as event_0 {% endif %} | ||
from event_stream | ||
) | ||
|
||
, flow_counts as ( | ||
select | ||
* | ||
, count(*) as n_events | ||
from flow_events | ||
where event_0 = '{{ primary_event }}' | ||
group by 1 {% for i in range(n_events_from) %}, {{ i + 2 }} {% endfor %} | ||
) | ||
|
||
, final as ( | ||
select * | ||
from flow_counts | ||
order by n_events desc | ||
limit {{ top_n }} | ||
) | ||
|
||
select * from final | ||
{% endmacro %} |