Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: query average time and last usage metadata #284

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dbtmetabase/_exposures.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def extract_exposures(
depends = set()
native_query = ""
header = ""
average_query_time = None
last_used_at = None

entity: Mapping
if item["model"] == "card":
Expand All @@ -129,6 +131,13 @@ def extract_exposures(
depends.update(result["depends"])
native_query = result["native_query"]

average_query_time_ms = entity.get("average_query_time")
if average_query_time_ms:
average_query_time_s = average_query_time_ms / 1000
average_query_time = f"{(average_query_time_s // 60):.0f}:{(average_query_time_s % 60):06.3f}"

last_used_at = entity.get("last_used_at")

elif item["model"] == "dashboard":
dashboard_entity = self.metabase.find_dashboard(uid=item["id"])
if dashboard_entity is None:
Expand Down Expand Up @@ -190,6 +199,8 @@ def extract_exposures(
created_at=entity["created_at"],
creator_name=creator_name or "",
creator_email=creator_email or "",
last_used_at=last_used_at,
average_query_time=average_query_time,
native_query=native_query,
depends_on=sorted(
[
Expand Down Expand Up @@ -310,6 +321,8 @@ def __format_exposure(
created_at: str,
creator_name: str,
creator_email: str,
last_used_at: Optional[str],
average_query_time: Optional[str],
native_query: Optional[str],
depends_on: Iterable[str],
tags: Optional[Sequence[str]],
Expand Down Expand Up @@ -362,6 +375,14 @@ def __format_exposure(
"depends_on": list(depends_on),
}

meta = {}
if average_query_time:
meta["average_query_time"] = average_query_time
if last_used_at:
meta["last_used_at"] = last_used_at
if meta:
exposure["meta"] = meta

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

Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/exposure/collection/our_analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
meta:
average_query_time: '0:00.028'
last_used_at: '2024-06-20T05:56:57.295379Z'
- name: dummy_1
label: Dummy
description: '### Visualization: Table
Expand Down Expand Up @@ -78,6 +81,9 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
meta:
average_query_time: '0:00.024'
last_used_at: '2024-06-20T05:57:12.297288Z'
- name: orders___customers
label: Orders + Customers
description: '### Visualization: Table
Expand All @@ -102,6 +108,9 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
meta:
average_query_time: '0:00.107'
last_used_at: '2024-06-20T05:55:59.079272Z'
- name: orders___customers__filtered_by_status_is_completed
label: Orders + Customers, Filtered by Status is completed
description: '### Visualization: Table
Expand All @@ -126,6 +135,9 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
meta:
average_query_time: '0:00.264'
last_used_at: '2024-06-19T12:09:31.689381Z'
- name: returned_order_count_sql
label: Returned Order Count SQL
description: "### Visualization: Scalar\n\nNo description provided in Metabase\n\
Expand All @@ -141,6 +153,9 @@ exposures:
depends_on:
- ref('stg_orders')
- ref('stg_payments')
meta:
average_query_time: '0:00.191'
last_used_at: '2024-06-19T12:09:31.486489Z'
- name: the_dashboard
label: The Dashboard
description: '### Dashboard Cards: 3
Expand Down Expand Up @@ -190,3 +205,6 @@ exposures:
email: [email protected]
depends_on:
- ref('payments')
meta:
average_query_time: '0:00.058'
last_used_at: '2024-10-12T01:52:30.585371Z'
18 changes: 18 additions & 0 deletions tests/fixtures/exposure/default/exposures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
meta:
average_query_time: '0:00.028'
last_used_at: '2024-06-20T05:56:57.295379Z'
tags:
- metabase
- name: dummy_1
Expand Down Expand Up @@ -82,6 +85,9 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
meta:
average_query_time: '0:00.024'
last_used_at: '2024-06-20T05:57:12.297288Z'
tags:
- metabase
- name: orders___customers
Expand All @@ -108,6 +114,9 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
meta:
average_query_time: '0:00.107'
last_used_at: '2024-06-20T05:55:59.079272Z'
tags:
- metabase
- name: orders___customers__filtered_by_status_is_completed
Expand All @@ -134,6 +143,9 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
meta:
average_query_time: '0:00.264'
last_used_at: '2024-06-19T12:09:31.689381Z'
tags:
- metabase
- name: returned_order_count_sql
Expand All @@ -151,6 +163,9 @@ exposures:
depends_on:
- ref('stg_orders')
- ref('stg_payments')
meta:
average_query_time: '0:00.191'
last_used_at: '2024-06-19T12:09:31.486489Z'
tags:
- metabase
- name: the_dashboard
Expand Down Expand Up @@ -204,5 +219,8 @@ exposures:
email: [email protected]
depends_on:
- ref('payments')
meta:
average_query_time: '0:00.058'
last_used_at: '2024-10-12T01:52:30.585371Z'
tags:
- metabase
3 changes: 3 additions & 0 deletions tests/fixtures/exposure/type/card/27.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
meta:
average_query_time: '0:00.107'
last_used_at: '2024-06-20T05:55:59.079272Z'
3 changes: 3 additions & 0 deletions tests/fixtures/exposure/type/card/28.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ exposures:
depends_on:
- ref('customers')
- ref('orders')
meta:
average_query_time: '0:00.264'
last_used_at: '2024-06-19T12:09:31.689381Z'
3 changes: 3 additions & 0 deletions tests/fixtures/exposure/type/card/29.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ exposures:
depends_on:
- ref('stg_orders')
- ref('stg_payments')
meta:
average_query_time: '0:00.191'
last_used_at: '2024-06-19T12:09:31.486489Z'
3 changes: 3 additions & 0 deletions tests/fixtures/exposure/type/card/30.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
meta:
average_query_time: '0:00.028'
last_used_at: '2024-06-20T05:56:57.295379Z'
3 changes: 3 additions & 0 deletions tests/fixtures/exposure/type/card/31.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ exposures:
name: dbtmetabase
email: [email protected]
depends_on: []
meta:
average_query_time: '0:00.024'
last_used_at: '2024-06-20T05:57:12.297288Z'
28 changes: 28 additions & 0 deletions tests/fixtures/exposure/type/card/33.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: 2
exposures:
- name: transactions
label: Transactions
description: '### Visualization: Table


No description provided in Metabase


#### Metadata


Metabase ID: __33__


Created On: __2024-10-11T23:46:29.272117Z__'
type: analysis
url: http://localhost:3000/card/33
maturity: medium
owner:
name: dbtmetabase
email: [email protected]
depends_on:
- ref('payments')
meta:
average_query_time: '0:00.058'
last_used_at: '2024-10-12T01:52:30.585371Z'