Skip to content

Commit

Permalink
Merge pull request #34 from YotpoLtd/KOALA-830-add-dimensions-support
Browse files Browse the repository at this point in the history
added parameters, filters and dimension group support
  • Loading branch information
MDura92 authored Dec 27, 2023
2 parents 39dd4cb + 3506839 commit 5638dc1
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ include: "views/*"

explore: email__daily_total_email_campaigns {
description: "Email Marketing total campaigns per day in utc time"
sql_always_where: orders_count = 1 ;;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ view: ugc__shopper_experience_store_aggregation {
]
}

dimension_group: duration_dimension_group {
type: duration
sql_start: {% date_start date_range_filter %} ;;
sql_end: {% date_end date_range_filter %} ;;
description: ""
intervals: [day]
}

dimension: app_key {
type: string
sql: ${TABLE}.app_key ;;
Expand Down Expand Up @@ -82,4 +90,15 @@ view: ugc__shopper_experience_store_aggregation {
type: count
description: "Default count measure"
}

parameter: selected_store_id {
type: string
description: ""
label: "selected_store_id"
}

filter: date_range_filter {
description: ""
type: date
}
}
2 changes: 1 addition & 1 deletion tests/resources/test_target/manifest.json

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions tests/yoda_dbt2looker/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,107 @@ def test_lookml_calculated_dimension():
"sql": "case when 1=1 then 1 else 0 end",
"description": "custom dimension",
}


def test_lookml_parameter_exposure():
parameter1 = models.Dbt2LookerExploreParameter(
name="custom_parameter_1",
model="ref('model_1')",
type="number",
label="Param1",
allowed_value=["1", "2", "3"],
description="custom parameter",
)

parameter2 = models.Dbt2LookerExploreParameter(
name="custom_parameter_2",
model="ref('model_1')",
type="string",
)
value1 = generator.lookml_parameter_exposure(parameter1)
value2 = generator.lookml_parameter_exposure(parameter2)
assert value1 == {
"name": "custom_parameter_1",
"type": "number",
"label": "Param1",
"allowed_value": ["1", "2", "3"],
"description": "custom parameter",
}

assert value2 == {
"name": "custom_parameter_2",
"type": "string",
"description": "",
}


def test_lookml_filter_exposure():
filter1 = models.Dbt2LookerExploreFilter(
name="custom_filter_1",
model="ref('model_1')",
type="number",
label="Filter1",
sql="case when 1=1 then 1 else 0 end",
description="custom filter",
)

filter2 = models.Dbt2LookerExploreFilter(
name="custom_filter_2",
model="ref('model_1')",
type="date",
)
value1 = generator.lookml_filter_exposure(filter1)
value2 = generator.lookml_filter_exposure(filter2)
assert value1 == {
"name": "custom_filter_1",
"type": "number",
"label": "Filter1",
"sql": "case when 1=1 then 1 else 0 end",
"description": "custom filter",
}

assert value2 == {
"name": "custom_filter_2",
"type": "date",
"description": "",
}


def test_lookml_exposure_dimension_duration_group():
dim_group1 = models.Dbt2LookerExploreDimensionGroupDuration(
name="dim_group_1",
model="ref('model_1')",
type="duration",
sql_start="case when 1=1 then 1 else 0 end",
sql_end="case when 1=1 then 1 else 0 end",
intervals=["day"],
datatype="date",
description="custom dimension group",
)

dim_group2 = models.Dbt2LookerExploreDimensionGroupDuration(
name="dim_group_2",
model="ref('model_1')",
type="duration",
sql_start="case when 1=1 then 1 else 0 end",
sql_end="case when 1=1 then 1 else 0 end",
)
value1 = generator.lookml_exposure_dimension_group_duration(dim_group1)
value2 = generator.lookml_exposure_dimension_group_duration(dim_group2)
assert value1 == {
"name": "dim_group_1",
"type": "duration",
"sql_start": "case when 1=1 then 1 else 0 end",
"sql_end": "case when 1=1 then 1 else 0 end",
"description": "custom dimension group",
"datatype": "date",
"intervals": ["day"],
}

assert value2 == {
"name": "dim_group_2",
"type": "duration",
"sql_start": "case when 1=1 then 1 else 0 end",
"sql_end": "case when 1=1 then 1 else 0 end",
"description": "",
}
6 changes: 3 additions & 3 deletions tests/yoda_dbt2looker/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from yoda_dbt2looker import models
from yoda_dbt2looker.parser import (
_extract_measures_models,
_extract_calculated_dimensions_models,
_extract_exposure_models,
)


Expand Down Expand Up @@ -34,7 +34,7 @@ def test__extract_measures_models():
}


def test__extract_calculated_dimensions_models():
def test__extract_models():
exposure_model_views = set()
exposure = MagicMock()
model = {}
Expand All @@ -54,7 +54,7 @@ def test__extract_calculated_dimensions_models():
type="string",
),
]
_extract_calculated_dimensions_models(exposure_model_views, model, exposure)
_extract_exposure_models(exposure_model_views, model, exposure.meta.looker.dimensions)
assert exposure_model_views == {"model_1", "model_2"}
assert model == {
"model_1": [exposure.meta.looker.dimensions[0]],
Expand Down
85 changes: 82 additions & 3 deletions yoda_dbt2looker/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ def lookml_dimension_groups_from_model(
and map_adapter_type_to_looker(adapter_type, column.data_type)
in looker_date_types
]
return date_times + dates

exposure_dimension_groups = [
lookml_exposure_dimension_group_duration(exposure_dimension_group)
for exposure_dimension_group in model.dimension_groups_exposure
]
return date_times + dates + exposure_dimension_groups


def lookml_dimensions_from_model(
Expand Down Expand Up @@ -398,13 +403,70 @@ def lookml_non_aggregative_measure(measure: models.Dbt2LookerExploreMeasure):


def lookml_calculated_dimension(dimension: models.Dbt2LookerExploreDimension):
return {
tmp_dimension = {
"name": dimension.name,
"description": dimension.description,
"type": dimension.type,
"type": dimension.type.value,
"sql": dimension.sql,
}

if dimension.hidden:
tmp_dimension["hidden"] = dimension.hidden

return tmp_dimension


def lookml_parameter_exposure(exposure_parameter: models.Dbt2LookerExploreParameter):
tmp_parameter = {
"name": exposure_parameter.name,
"type": exposure_parameter.type.value,
"description": exposure_parameter.description,
}

if exposure_parameter.allowed_value:
tmp_parameter["allowed_value"] = exposure_parameter.allowed_value

if exposure_parameter.label:
tmp_parameter["label"] = exposure_parameter.label

return tmp_parameter


def lookml_filter_exposure(exposure_filter: models.Dbt2LookerExploreFilter):
tmp_filter = {
"name": exposure_filter.name,
"description": exposure_filter.description,
"type": exposure_filter.type.value,
}

if exposure_filter.sql:
tmp_filter["sql"] = exposure_filter.sql

if exposure_filter.label:
tmp_filter["label"] = exposure_filter.label

return tmp_filter


def lookml_exposure_dimension_group_duration(
dimension_group: models.Dbt2LookerExploreDimensionGroupDuration,
):
tmp_dimension_group_duration = {
"name": dimension_group.name,
"type": dimension_group.type.value,
"sql_start": dimension_group.sql_start,
"sql_end": dimension_group.sql_end,
"description": dimension_group.description,
}

if dimension_group.datatype:
tmp_dimension_group_duration["datatype"] = dimension_group.datatype

if dimension_group.intervals:
tmp_dimension_group_duration["intervals"] = dimension_group.intervals

return tmp_dimension_group_duration


def lookml_view_from_dbt_model(
model: models.DbtModel, adapter_type: models.SupportedDbtAdapters
Expand All @@ -418,6 +480,19 @@ def lookml_view_from_dbt_model(
"measures": lookml_measures_from_model(model),
}
}
parameters = [
lookml_parameter_exposure(parameter_exposure)
for parameter_exposure in model.parameters_exposure
]
filters = [
lookml_filter_exposure(filter_exposure)
for filter_exposure in model.filters_exposure
]
if parameters:
lookml["view"]["parameters"] = parameters
if filters:
lookml["view"]["filters"] = filters

logging.debug(
f"Created view from model %s with %d measures, %d dimensions",
model.name,
Expand Down Expand Up @@ -530,6 +605,10 @@ def lookml_model_data_from_dbt_model(model: models.DbtModel, dbt_project_name: s
],
},
}

if model.meta.looker.sql_always_where:
lookml["explore"]["sql_always_where"] = model.meta.looker.sql_always_where

return lkml.dump(lookml)


Expand Down
Loading

0 comments on commit 5638dc1

Please sign in to comment.