|
| 1 | +# (C) 2024 GoodData Corporation |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from gooddata_sdk import Attribute, ComputeToSdkConverter, Filter, Metric |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class ExecutionRequest: |
| 10 | + """ |
| 11 | + Information about the execution request that is sent to the FlexFun. |
| 12 | + """ |
| 13 | + |
| 14 | + attributes: list[Attribute] |
| 15 | + """ |
| 16 | + All the attributes that are part of the execution request. |
| 17 | + """ |
| 18 | + |
| 19 | + metrics: list[Metric] |
| 20 | + """ |
| 21 | + All the metrics that are part of the execution request. |
| 22 | + """ |
| 23 | + |
| 24 | + filters: list[Filter] |
| 25 | + """ |
| 26 | + All the filters that are part of the execution request. |
| 27 | + """ |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def from_dict(d: dict) -> "ExecutionRequest": |
| 31 | + """ |
| 32 | + Create ExecutionRequest from a dictionary. |
| 33 | + :param d: the dictionary to parse |
| 34 | + """ |
| 35 | + 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", [])], |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class ExecutionContext: |
| 44 | + """ |
| 45 | + Execution context of the FlexFun |
| 46 | + """ |
| 47 | + |
| 48 | + organization_id: str |
| 49 | + """ |
| 50 | + The ID of the organization that the FlexFun is executed in. |
| 51 | + """ |
| 52 | + |
| 53 | + workspace_id: str |
| 54 | + """ |
| 55 | + The ID of the workspace that the FlexFun is executed in. |
| 56 | + """ |
| 57 | + |
| 58 | + user_id: str |
| 59 | + """ |
| 60 | + The ID of the user that invoked the FlexFun. |
| 61 | + """ |
| 62 | + |
| 63 | + timestamp: Optional[str] |
| 64 | + """ |
| 65 | + The timestamp of the execution used as "now" in date filters. |
| 66 | + For example 2020-06-03T10:15:30+01:00. |
| 67 | + """ |
| 68 | + |
| 69 | + timezone: Optional[str] |
| 70 | + """ |
| 71 | + The timezone of the execution. |
| 72 | + """ |
| 73 | + |
| 74 | + execution_request: ExecutionRequest |
| 75 | + """ |
| 76 | + The execution request that the FlexFun should process. |
| 77 | + """ |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def from_dict(d: dict) -> Optional["ExecutionContext"]: |
| 81 | + """ |
| 82 | + Create ExecutionContext from a dictionary. |
| 83 | + :param d: the dictionary to parse |
| 84 | + """ |
| 85 | + if not d: |
| 86 | + return None |
| 87 | + return ExecutionContext( |
| 88 | + organization_id=d["organization_id"], |
| 89 | + workspace_id=d["workspace_id"], |
| 90 | + user_id=d["user_id"], |
| 91 | + timestamp=d.get("timestamp"), |
| 92 | + timezone=d.get("timezone"), |
| 93 | + execution_request=ExecutionRequest.from_dict(d["execution_request"]), |
| 94 | + ) |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def from_parameters(parameters: dict) -> Optional["ExecutionContext"]: |
| 98 | + """ |
| 99 | + Create ExecutionContext from FlexFun parameters. |
| 100 | + :param parameters: the parameters dictionary of the FlexFun invocation |
| 101 | + :return: None if the parameters do not contain the execution context, otherwise the execution context |
| 102 | + """ |
| 103 | + return ( |
| 104 | + ExecutionContext.from_dict(parameters["execution_context"]) if "execution_context" in parameters else None |
| 105 | + ) |
0 commit comments