Coverage for dj/models/metric.py: 100%
24 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-17 20:05 -0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-17 20:05 -0700
1"""
2Models for metrics.
3"""
4from typing import List, Optional
6from sqlmodel import SQLModel
8from dj.models.engine import Dialect
9from dj.models.node import Node
10from dj.models.query import ColumnMetadata
11from dj.sql.dag import get_dimensions
12from dj.typing import UTCDatetime
15class Metric(SQLModel):
16 """
17 Class for a metric.
18 """
20 id: int
21 name: str
22 display_name: str
23 current_version: str
24 description: str = ""
26 created_at: UTCDatetime
27 updated_at: UTCDatetime
29 query: str
31 dimensions: List[str]
33 @classmethod
34 def parse_node(cls, node: Node) -> "Metric":
35 """
36 Parses a node into a metric.
37 """
39 return cls(
40 **node.dict(),
41 description=node.current.description,
42 updated_at=node.current.updated_at,
43 query=node.current.query,
44 dimensions=get_dimensions(node),
45 )
48class TranslatedSQL(SQLModel):
49 """
50 Class for SQL generated from a given metric.
51 """
53 # TODO: once type-inference is added to /query/ endpoint # pylint: disable=fixme
54 # columns attribute can be required
55 sql: str
56 columns: Optional[List[ColumnMetadata]] = None # pragma: no-cover
57 dialect: Optional[Dialect] = None