Metrics
Import the metrics
resource module and instantiate the client
[ ]:
from peak import metrics
from peak.constants import ArtifactInfo
metric_client: metrics.Metric = metrics.get_client()
Possible field values
Collection Scope
The scope of the collection. The following scopes are supported: PUBLIC
and PRIVATE
.
Publish a semantic model metrics
[ ]:
publish_body = {
"namespace": "dev",
}
artifact: ArtifactInfo = {
"path": "metrics",
}
collection_id: str = "bc8b6ef5-d2f6-4b7f-9365-0261b43997c9"
# Publish metrics using artifact and namespace
publish_artifact_metrics_response = metric_client.publish(
body=publish_body,
artifact=artifact,
)
# Publish metrics using collection id and namespace
publish_collection_metrics_response = metric_client.publish(
body=publish_body,
collection_id=collection_id,
)
Query published metrics
[ ]:
query_data = metric_client.query(
namespace="dev",
generate_sql=False,
measures=["product.count"],
dimensions=["product.name"],
filters=[{"dimension": "product.name", "operator": "equals", "values": ["car", "bike"]}],
time_dimensions=[
{
"dimension": "product.created_at",
"granularity": "day",
"date_range": {"start": "2021-01-01", "end": "2021-01-31"},
},
],
order={"product.count": "asc"},
segments=["product.segment1", "product.segment2"],
limit=100,
offset=0,
)
"""
query_data = {
"data": [
{
"product.name": "car",
"product.count": 100,
},
{
"product.name": "bike",
"product.count": 50,
},
],
}
"""
sql_query = metric_client.query(
namespace="dev",
generate_sql=True,
measures=["product.count"],
dimensions=["product.name"],
filters=[{"dimension": "product.name", "operator": "equals", "values": ["car", "bike"]}],
time_dimensions=[
{
"dimension": "product.created_at",
"granularity": "day",
"date_range": ["2021-01-01", "2021-01-31"],
},
],
order={"product.count": "asc"},
segments=["product.segment1", "product.segment2"],
limit=100,
offset=0,
)
"""
sql_query = {
"sql": "SELECT product.name, product.count FROM table WHERE product.name IN ('car', 'bike') AND created_at BETWEEN '2021-01-01' AND '2021-01-31' ORDER BY product.count ASC LIMIT 100 OFFSET 0",
}
"""
List published metrics
[ ]:
metrics_list_iterator = metric_client.list(
namespace="dev",
type="all",
)
metrics_list_iterated = next(metrics_list_iterator)
"""
{
"data": [
{
"name": "product",
"type": "cube",
"public": true,
"measures": [
{
"name": "product.max_price",
"type": "number",
"aggType": "max",
"public": true
},
{
"name": "product.max_discount",
"type": "number",
"aggType": "max",
"public": true
},
],
"dimensions": [
{
"name": "product.sale",
"type": "string",
"public": true,
"primaryKey": true
},
{
"name": "order.created_at",
"type": "time",
"public": true,
"primaryKey": false
},
],
"segments": [],
"collectionId": "b8d8308e-4f45-42e5-9c08-d4c4ba6db7f6",
"publicationId": "299e7d07-db2f-4050-88c3-40afd7603807"
}
],
"pageCount": 1,
"pageNumber": 1,
"pageSize": 25,
"totalCount": 2
}
"""
Delete Metrics
[ ]:
# Delete all metrics related to a publication id
metric_client.delete(
publication_id="299e7d07-db2f-4050-88c3-40afd7603807",
)
# Delete given metrics from a namespace
metric_client.delete(
namespace="dev",
measures=["product"],
)
Create Metrics Collection
[ ]:
metrics_collection_body = {
"name": "test",
"scope": "public",
"description": "This collection is used for product metrics",
}
artifact: ArtifactInfo = {
"path": "metrics",
}
create_collection_response = metric_client.publish(
body=metrics_collection_body,
artifact=artifact,
)
"""
create_collection_response = {
"artifactId": "7dc0feaa-be90-467b-9c3a-009a234e4b2b",
"collectionId": "bc8b6ef5-d2f6-4b7f-9365-0261b43997c9",
}
"""
Delete Metrics Collection
[ ]:
metric_client.delete_collection(collection_id="bc8b6ef5-d2f6-4b7f-9365-0261b43997c9")
List Metrics Collections
[ ]:
collections_list_iterator = metric_client.list_collections()
collections_list_iterated = next(collections_list_iterator)
"""
{
"id": "b8d8308e-4f45-42e5-9c08-d4c4ba6db7f6",
"name": "product",
"tenant": "tenant-name",
"scope": "PUBLIC",
"description": "Product metrics",
"artifactId": "7dc0feaa-be90-467b-9c3a-009a234e4b2b",
"createdAt": "2024-01-26T00:00:00Z",
"createdBy": "someone@peak.ai",
"updatedAt": "2024-01-26T00:00:00Z",
"updatedBy": "someone@peak.ai"
}
"""
List Namespaces
[ ]:
namespaces_list_iterator = metric_client.list_namespaces()
namespaces_list_iterated = next(namespaces_list_iterator)
"""
{
"name": "sktest-it-all",
"models": [
{
"name": "products",
"publicationId": "3ebf5556-fd48-43b5-8fa3-a74b8c96ee58",
"type": "cube"
},
{
"name": "sales",
"publicationId": "3ebf5556-fd48-43b5-8fa3-a74b8c96ee58",
"type": "cube"
}
]
}
"""