Skip to content

Commit

Permalink
(DE-830) feat(dbt): add diversity metric in dbt
Browse files Browse the repository at this point in the history
  • Loading branch information
cdelabre-pass committed Dec 27, 2024
1 parent 96e7572 commit a0e7606
Showing 1 changed file with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,27 @@
}}

{% set entities = [
{"entity": "offer_category_id", "type": "OFFER_CATEGORY", "score_multiplier": 25},
{"entity": "venue_type_label", "type": "VENUE_TYPE", "score_multiplier": 20},
{"entity": "offer_subcategory_id", "type": "OFFER_SUBCATEGORY", "score_multiplier": 10},
{
"entity": "offer_category_id",
"type": "OFFER_CATEGORY",
"score_multiplier": 25,
},
{
"entity": "venue_type_label",
"type": "VENUE_TYPE",
"score_multiplier": 20,
},
{
"entity": "offer_subcategory_id",
"type": "OFFER_SUBCATEGORY",
"score_multiplier": 10,
},
{"entity": "venue_id", "type": "VENUE", "score_multiplier": 5},
{"entity": "extra_category", "type": "EXTRA_CATEGORY", "score_multiplier": 5},
{
"entity": "extra_category",
"type": "EXTRA_CATEGORY",
"score_multiplier": 5,
},
] %}

with
Expand All @@ -26,31 +42,35 @@ with
venue_type_label,
offer_category_id,
venue_id,
coalesce(offer_type_label, venue_id) as extra_category, -- venue_id is used as extra_category when offer_type_label is null
row_number() over (partition by user_id order by booking_created_at) as booking_rank
coalesce(offer_type_label, venue_id) as extra_category, -- venue_id is used as extra_category when offer_type_label is null
row_number() over (
partition by user_id order by booking_created_at
) as booking_rank
from {{ ref("int_global__booking") }}
where booking_status != 'CANCELLED'
),

entity_calculations as (
{% for entity in entities %}
select distinct
booking_id,
booking_created_at,
booking_creation_date,
booking_rank,
user_id,
{{ entity.entity }} as diversity_booked_entity,
'{{ entity.type }}' as diversity_booked_entity_type,
{{ entity.score_multiplier }} as score_multiplier,
row_number() over (partition by user_id, {{ entity.entity }} order by booking_created_at) as diversity_booking_entity_rank
from raw_data
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
)
),

entity_calculations as (
{% for entity in entities %}
select distinct
booking_id,
booking_created_at,
booking_creation_date,
booking_rank,
user_id,
{{ entity.entity }} as diversity_booked_entity,
'{{ entity.type }}' as diversity_booked_entity_type,
{{ entity.score_multiplier }} as score_multiplier,
row_number() over (
partition by user_id, {{ entity.entity }}
order by booking_created_at
) as diversity_booking_entity_rank
from raw_data
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
)

select
booking_rank,
Expand All @@ -62,9 +82,10 @@ select
user_id,
booking_creation_date,
case
when booking_rank = 1 then score_multiplier
when diversity_booking_entity_rank = 1 then score_multiplier
else 0
end
as diversity_score
when booking_rank = 1
then score_multiplier
when diversity_booking_entity_rank = 1
then score_multiplier
else 0
end as diversity_score
from entity_calculations

0 comments on commit a0e7606

Please sign in to comment.