Skip to content

Commit

Permalink
Query average time and last usage metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline committed Oct 12, 2024
1 parent 7904041 commit 79bdcdd
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 18 deletions.
40 changes: 22 additions & 18 deletions dbtmetabase/_exposures.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def extract_exposures(
depends = set()
native_query = ""
header = ""
average_query_time_ms = None
last_used_at_utc = None
average_query_time = None
last_used_at = None

entity: Mapping
if item["model"] == "card":
card_entity = self.metabase.find_card(uid=item["id"])
Expand All @@ -130,8 +130,13 @@ def extract_exposures(
result = self.__extract_card_exposures(ctx, card=entity)
depends.update(result["depends"])
native_query = result["native_query"]
average_query_time_ms = entity.get('average_query_time')
last_used_at_utc = entity['last_used_at']

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"])
Expand Down Expand Up @@ -194,8 +199,8 @@ def extract_exposures(
created_at=entity["created_at"],
creator_name=creator_name or "",
creator_email=creator_email or "",
last_used_at_utc=last_used_at_utc,
average_query_time_ms=average_query_time_ms,
last_used_at=last_used_at,
average_query_time=average_query_time,
native_query=native_query,
depends_on=sorted(
[
Expand Down Expand Up @@ -316,8 +321,8 @@ def __format_exposure(
created_at: str,
creator_name: str,
creator_email: str,
last_used_at_utc: Optional[str],
average_query_time_ms: Optional[float],
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 @@ -354,11 +359,6 @@ def __format_exposure(
+ f"Created On: __{created_at}__"
)

if average_query_time_ms:
average_query_time_s = average_query_time_ms // 1000
average_query_time = f"{int(average_query_time_s // 60)}:{str(int(average_query_time_s % 60)).zfill(2)}"
else:
average_query_time = "n.a."
exposure = {
"name": name,
"label": label,
Expand All @@ -373,12 +373,16 @@ def __format_exposure(
"email": creator_email,
},
"depends_on": list(depends_on),
"meta": {
"avg_query_time": average_query_time,
"last_used_at_utc": last_used_at_utc or 'n.a.'
}
}

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'

0 comments on commit 79bdcdd

Please sign in to comment.