Skip to content

Commit

Permalink
Allow tags on exposures
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline committed Jul 25, 2024
1 parent 8647627 commit 365dd48
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ sandbox-exposures:
--metabase-password $$MB_PASSWORD \
--output-path sandbox/models/exposures \
--output-grouping collection \
--tag metabase \
--verbose )

( . sandbox/.env && cd sandbox && \
Expand Down
8 changes: 8 additions & 0 deletions dbtmetabase/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,20 @@ def models(
is_flag=True,
help="Exclude items that have not been verified. Only applies to entity types that support verification.",
)
@click.option(
"--tag",
"tags",
multiple=True,
help="Optional tags for exported dbt exposures.",
)
def exposures(
output_path: str,
output_grouping: Optional[str],
include_collections: Optional[Sequence[str]],
exclude_collections: Optional[Sequence[str]],
allow_personal_collections: bool,
exclude_unverified: bool,
tags: Sequence[str],
core: DbtMetabase,
):
core.extract_exposures(
Expand All @@ -395,6 +402,7 @@ def exposures(
),
allow_personal_collections=allow_personal_collections,
exclude_unverified=exclude_unverified,
tags=tags,
)


Expand Down
21 changes: 19 additions & 2 deletions dbtmetabase/_exposures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
from abc import ABCMeta, abstractmethod
from operator import itemgetter
from pathlib import Path
from typing import Iterable, Mapping, MutableMapping, MutableSequence, Optional, Tuple
from typing import (
Iterable,
Mapping,
MutableMapping,
MutableSequence,
Optional,
Sequence,
Tuple,
)

from dbtmetabase.metabase import Metabase

Expand Down Expand Up @@ -47,6 +55,7 @@ def extract_exposures(
collection_filter: Optional[Filter] = None,
allow_personal_collections: bool = False,
exclude_unverified: bool = False,
tags: Optional[Sequence[str]] = None,
) -> Iterable[Mapping]:
"""Extract dbt exposures from Metabase.
Expand All @@ -56,6 +65,7 @@ def extract_exposures(
collection_filter (Optional[Filter], optional): Filter Metabase collections. Defaults to None.
allow_personal_collections (bool, optional): Allow personal Metabase collections. Defaults to False.
exclude_unverified (bool, optional): Exclude items that have not been verified. Only applies to entity types that support verification. Defaults to False.
tags (Sequence[str], optional): Optional tags for resulting dbt exposures. Defaults to None.
Returns:
Iterable[Mapping]: List of parsed exposures.
Expand Down Expand Up @@ -188,6 +198,7 @@ def extract_exposures(
if depend.lower() in ctx.model_refs
]
),
tags=tags,
),
}
)
Expand Down Expand Up @@ -301,6 +312,7 @@ def __format_exposure(
creator_email: str,
native_query: Optional[str],
depends_on: Iterable[str],
tags: Optional[Sequence[str]],
) -> Mapping:
"""Builds dbt exposure representation (see https://docs.getdbt.com/reference/exposure-properties)."""

Expand Down Expand Up @@ -334,7 +346,7 @@ def __format_exposure(
+ f"Created On: __{created_at}__"
)

return {
exposure = {
"name": name,
"label": label,
"description": safe_description(
Expand All @@ -350,6 +362,11 @@ def __format_exposure(
"depends_on": list(depends_on),
}

if tags:
exposure["tags"] = list(tags)

return exposure

@staticmethod
def __write_exposures(
exposures: Iterable[Mapping],
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/exposure/default/exposures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ exposures:
email: [email protected]
depends_on:
- ref('orders')
tags:
- metabase
- name: dummy
label: Dummy
description: '### Visualization: Table
Expand Down Expand Up @@ -46,6 +48,8 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
tags:
- metabase
- name: dummy_1
label: Dummy
description: '### Visualization: Table
Expand Down Expand Up @@ -78,6 +82,8 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
tags:
- metabase
- name: orders___customers
label: Orders + Customers
description: '### Visualization: Table
Expand All @@ -102,6 +108,8 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
tags:
- metabase
- name: orders___customers__filtered_by_status_is_completed
label: Orders + Customers, Filtered by Status is completed
description: '### Visualization: Table
Expand All @@ -126,6 +134,8 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
tags:
- metabase
- name: returned_order_count_sql
label: Returned Order Count SQL
description: "### Visualization: Scalar\n\nNo description provided in Metabase\n\
Expand All @@ -141,6 +151,8 @@ exposures:
depends_on:
- ref('stg_orders')
- ref('stg_payments')
tags:
- metabase
- name: the_dashboard
label: The Dashboard
description: '### Dashboard Cards: 3
Expand All @@ -167,3 +179,5 @@ exposures:
- ref('orders')
- ref('stg_orders')
- ref('stg_payments')
tags:
- metabase
5 changes: 4 additions & 1 deletion tests/test_exposures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

from tests._mocks import FIXTURES_PATH, TMP_PATH, MockDbtMetabase

TMP_PATH.mkdir(exist_ok=True)

def setup_module():
TMP_PATH.mkdir(exist_ok=True)


@pytest.fixture(name="core")
Expand All @@ -29,6 +31,7 @@ def test_exposures(core: MockDbtMetabase):
core.extract_exposures(
output_path=str(output_path),
output_grouping=None,
tags=["metabase"],
)

_assert_exposures(
Expand Down

0 comments on commit 365dd48

Please sign in to comment.