Skip to content

Commit bf0d0b8

Browse files
authored
feat: add extra fields to SavedQuery (#39)
Thid commit adds `Exports` and `QueryParams` to the fields of a saved query. This makes querying for them more useful than just returning a name, description and label. To make this work, I had to add an extra check in the base `GraphQLFragmentMixin` class because it didn't handle `Optional` field well.
1 parent b5e8d6e commit bf0d0b8

File tree

8 files changed

+107
-6
lines changed

8 files changed

+107
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Features
2+
body: Add `Export` list to `SavedQuery`
3+
time: 2024-08-19T13:03:13.553574+02:00
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Features
2+
body: Add `SavedQueryQueryParam` list to `SavedQuery`
3+
time: 2024-08-19T13:03:27.820242+02:00

dbtsl/models/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
from .measure import AggregationType, Measure
1111
from .metric import Metric, MetricType
1212
from .query import QueryResult
13-
from .saved_query import SavedQuery
14-
from .time_granularity import TimeGranularity
13+
from .saved_query import (
14+
Export,
15+
ExportConfig,
16+
ExportDestinationType,
17+
SavedQuery,
18+
SavedQueryGroupByParam,
19+
SavedQueryMetricParam,
20+
SavedQueryQueryParams,
21+
SavedQueryWhereParam,
22+
)
23+
from .time import DatePart, TimeGranularity
1524

1625
# Only importing this so it registers aliases
1726
_ = QueryResult
@@ -20,13 +29,22 @@
2029

2130
__all__ = [
2231
"AggregationType",
32+
"DatePart",
2333
"Dimension",
2434
"DimensionType",
2535
"Entity",
2636
"EntityType",
37+
"Export",
38+
"ExportConfig",
39+
"ExportDestinationType",
2740
"Measure",
2841
"Metric",
2942
"MetricType",
3043
"SavedQuery",
44+
"SavedQuery",
45+
"SavedQueryGroupByParam",
46+
"SavedQueryMetricParam",
47+
"SavedQueryQueryParams",
48+
"SavedQueryWhereParam",
3149
"TimeGranularity",
3250
]

dbtsl/models/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def _get_fragments_for_field(type: Union[Type[Any], str], field_name: str) -> Un
6666
if inspect.isclass(type) and issubclass(type, GraphQLFragmentMixin):
6767
return type.gql_fragments()
6868

69-
if get_type_origin(type) is list:
69+
type_origin = get_type_origin(type)
70+
# Optional = Union[X, None]
71+
if type_origin is list or type_origin is Union:
7072
inner_type = get_type_args(type)[0]
7173
return GraphQLFragmentMixin._get_fragments_for_field(inner_type, field_name)
7274

dbtsl/models/dimension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import List, Optional
44

55
from dbtsl.models.base import BaseModel, GraphQLFragmentMixin
6-
from dbtsl.models.time_granularity import TimeGranularity
6+
from dbtsl.models.time import TimeGranularity
77

88

99
class DimensionType(str, Enum):

dbtsl/models/metric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dbtsl.models.dimension import Dimension
77
from dbtsl.models.entity import Entity
88
from dbtsl.models.measure import Measure
9-
from dbtsl.models.time_granularity import TimeGranularity
9+
from dbtsl.models.time import TimeGranularity
1010

1111

1212
class MetricType(str, Enum):

dbtsl/models/saved_query.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,69 @@
11
from dataclasses import dataclass
2-
from typing import Optional
2+
from enum import Enum
3+
from typing import List, Optional
34

45
from dbtsl.models.base import BaseModel, GraphQLFragmentMixin
6+
from dbtsl.models.time import DatePart, TimeGranularity
7+
8+
9+
class ExportDestinationType(str, Enum):
10+
"""All kinds of export destinations."""
11+
12+
TABLE = "TABLE"
13+
VIEW = "VIEW"
14+
15+
16+
@dataclass(frozen=True)
17+
class ExportConfig(BaseModel, GraphQLFragmentMixin):
18+
"""A saved query export config."""
19+
20+
alias: Optional[str]
21+
schema: Optional[str]
22+
export_as: ExportDestinationType
23+
24+
25+
@dataclass(frozen=True)
26+
class Export(BaseModel, GraphQLFragmentMixin):
27+
"""A saved query export."""
28+
29+
name: str
30+
config: ExportConfig
31+
32+
33+
@dataclass(frozen=True)
34+
class SavedQueryMetricParam(BaseModel, GraphQLFragmentMixin):
35+
"""The metric param of a saved query."""
36+
37+
name: str
38+
39+
40+
@dataclass(frozen=True)
41+
class SavedQueryGroupByParam(BaseModel, GraphQLFragmentMixin):
42+
"""The groupBy param of a saved query."""
43+
44+
name: str
45+
grain: Optional[TimeGranularity]
46+
date_part: Optional[DatePart]
47+
48+
49+
@dataclass(frozen=True)
50+
class SavedQueryWhereParam(BaseModel, GraphQLFragmentMixin):
51+
"""The where param of a saved query."""
52+
53+
@classmethod
54+
def gql_model_name(cls) -> str: # noqa: D102
55+
return "WhereFilter"
56+
57+
where_sql_template: str
58+
59+
60+
@dataclass(frozen=True)
61+
class SavedQueryQueryParams(BaseModel, GraphQLFragmentMixin):
62+
"""The parameters of a saved query."""
63+
64+
metrics: List[SavedQueryMetricParam]
65+
group_by: List[SavedQueryGroupByParam]
66+
where: Optional[SavedQueryWhereParam]
567

668

769
@dataclass(frozen=True)
@@ -11,3 +73,5 @@ class SavedQuery(BaseModel, GraphQLFragmentMixin):
1173
name: str
1274
description: Optional[str]
1375
label: Optional[str]
76+
query_params: SavedQueryQueryParams
77+
exports: List[Export]

dbtsl/models/time_granularity.py renamed to dbtsl/models/time.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ class TimeGranularity(str, Enum):
1515
MONTH = "MONTH"
1616
QUARTER = "QUARTER"
1717
YEAR = "YEAR"
18+
19+
20+
class DatePart(str, Enum):
21+
"""Date part."""
22+
23+
DOY = "DOY"
24+
DOW = "DOW"
25+
DAY = "DAY"
26+
MONTH = "MONTH"
27+
QUARTER = "QUARTER"
28+
YEAR = "YEAR"

0 commit comments

Comments
 (0)