Skip to content

Commit b924897

Browse files
authored
Merge pull request #774 from no23reason/dho/cq-577-attrs
feat: add support for ExecutionContextAttributes
2 parents a0a7a1b + ba24cc5 commit b924897

2 files changed

Lines changed: 88 additions & 9 deletions

File tree

gooddata-flight-server/gooddata_flight_server/flexfun/flex_fun_execution_context.py

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,93 @@
11
# (C) 2024 GoodData Corporation
2-
from dataclasses import dataclass
32
from typing import Optional
43

4+
from attrs import define, field
55
from gooddata_sdk import Attribute, ComputeToSdkConverter, Filter, Metric
66

77

8-
@dataclass
8+
def _dict_to_attributes(attributes: list[dict]) -> list[Attribute]:
9+
return [ComputeToSdkConverter.convert_attribute(a) for a in attributes]
10+
11+
12+
def _dict_to_metrics(metrics: list[dict]) -> list[Metric]:
13+
return [ComputeToSdkConverter.convert_metric(m) for m in metrics]
14+
15+
16+
def _dict_to_filters(filters: list[dict]) -> list[Filter]:
17+
return [ComputeToSdkConverter.convert_filter(f) for f in filters]
18+
19+
20+
@define
21+
class ExecutionContextAttributeSorting:
22+
"""
23+
Information about the sorting of an attribute.
24+
"""
25+
26+
sort_column: str
27+
"""
28+
Column to sort by.
29+
"""
30+
31+
sort_direction: str
32+
"""
33+
Direction of the sorting.
34+
"""
35+
36+
37+
@define
38+
class ExecutionContextAttribute:
39+
"""
40+
Information about an attribute used in the execution.
41+
"""
42+
43+
attribute_identifier: str
44+
"""
45+
Identifier of the attribute used.
46+
"""
47+
48+
attribute_title: str
49+
"""
50+
Title of the attribute used.
51+
"""
52+
53+
label_identifier: str
54+
"""
55+
Identifier of the particular label used.
56+
"""
57+
58+
label_title: str
59+
"""
60+
Title of the particular label used.
61+
"""
62+
63+
date_granularity: str
64+
"""
65+
Date granularity of the attribute if it is a date attribute.
66+
"""
67+
68+
sorting: Optional[ExecutionContextAttributeSorting]
69+
"""
70+
Sorting of the attribute. If not present, the attribute is not sorted.
71+
"""
72+
73+
74+
@define
975
class ExecutionRequest:
1076
"""
1177
Information about the execution request that is sent to the FlexFun.
1278
"""
1379

14-
attributes: list[Attribute]
80+
attributes: list[Attribute] = field(converter=_dict_to_attributes)
1581
"""
1682
All the attributes that are part of the execution request.
1783
"""
1884

19-
metrics: list[Metric]
85+
metrics: list[Metric] = field(converter=_dict_to_metrics)
2086
"""
2187
All the metrics that are part of the execution request.
2288
"""
2389

24-
filters: list[Filter]
90+
filters: list[Filter] = field(converter=_dict_to_filters)
2591
"""
2692
All the filters that are part of the execution request.
2793
"""
@@ -33,13 +99,13 @@ def from_dict(d: dict) -> "ExecutionRequest":
3399
:param d: the dictionary to parse
34100
"""
35101
return ExecutionRequest(
36-
attributes=[ComputeToSdkConverter.convert_attribute(a) for a in d.get("attributes", [])],
37-
metrics=[ComputeToSdkConverter.convert_metric(m) for m in d.get("measures", [])],
38-
filters=[ComputeToSdkConverter.convert_filter(f) for f in d.get("filters", [])],
102+
attributes=d.get("attributes", []),
103+
metrics=d.get("measures", []),
104+
filters=d.get("filters", []),
39105
)
40106

41107

42-
@dataclass
108+
@define
43109
class ExecutionContext:
44110
"""
45111
Execution context of the FlexFun
@@ -71,11 +137,21 @@ class ExecutionContext:
71137
The timezone of the execution.
72138
"""
73139

140+
week_start: Optional[str]
141+
"""
142+
The start of the week. Either "monday" or "sunday".
143+
"""
144+
74145
execution_request: ExecutionRequest
75146
"""
76147
The execution request that the FlexFun should process.
77148
"""
78149

150+
attributes: list[ExecutionContextAttribute]
151+
"""
152+
All the attributes that are part of the execution request.
153+
"""
154+
79155
@staticmethod
80156
def from_dict(d: dict) -> Optional["ExecutionContext"]:
81157
"""
@@ -90,7 +166,9 @@ def from_dict(d: dict) -> Optional["ExecutionContext"]:
90166
user_id=d["user_id"],
91167
timestamp=d.get("timestamp"),
92168
timezone=d.get("timezone"),
169+
week_start=d.get("week_start"),
93170
execution_request=ExecutionRequest.from_dict(d["execution_request"]),
171+
attributes=d.get("attributes", []),
94172
)
95173

96174
@staticmethod

gooddata-flight-server/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
long_description = (this_directory / "README.md").read_text(encoding="utf-8")
88

99
REQUIRES = [
10+
"attrs>=21.4.0,<=23.2.0",
1011
"dynaconf>=3.1.11,<4.0.0",
1112
"gooddata-sdk~=1.24.0",
1213
"opentelemetry-api>=1.24.0,<=2.0.0",

0 commit comments

Comments
 (0)