from metadata.sdk import configure
from metadata.sdk.entities import Tables
configure(
host="https://your-company.getcollate.io/api",
jwt_token="your-jwt-token"
)
table_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
# Add a table-level custom metric
Tables.add_custom_metric(table_id, {
"name": "active_customer_ratio",
"description": "Ratio of active customers to total customers",
"expression": "SELECT CAST(SUM(CASE WHEN active = true THEN 1 ELSE 0 END) AS FLOAT) / COUNT(*) FROM {table}"
})
# Add a column-level custom metric
Tables.add_custom_metric(table_id, {
"name": "email_domain_count",
"description": "Number of distinct email domains",
"columnName": "email",
"expression": "SELECT COUNT(DISTINCT SPLIT_PART(email, '@', 2)) FROM {table}"
})
# Delete a custom metric
Tables.delete_custom_metric(table_id, "active_customer_ratio")