-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask.py
More file actions
275 lines (247 loc) · 9.98 KB
/
task.py
File metadata and controls
275 lines (247 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import json
from typing import Literal, Union, Optional, List, Dict, Any, TYPE_CHECKING
from uuid import UUID
from datetime import datetime
from pydantic import Field
from hydroserverpy.api.models import DataConnection, Task, TaskRun, TaskMapping, OrchestrationSystem
from hydroserverpy.api.utils import normalize_uuid
from ..base import HydroServerBaseService
if TYPE_CHECKING:
from hydroserverpy import HydroServer
from hydroserverpy.api.models import Workspace
class TaskService(HydroServerBaseService):
def __init__(self, client: "HydroServer"):
self.model = Task
super().__init__(client)
def list(
self,
page: int = ...,
page_size: int = ...,
order_by: List[str] = ...,
workspace: Optional[Union["Workspace", UUID, str]] = ...,
orchestration_system: Optional[Union["OrchestrationSystem", UUID, str]] = ...,
orchestration_system_type: str = ...,
data_connection: Union["Workspace", UUID, str] = ...,
data_connection_type: str = ...,
extractor_type: str = ...,
transformer_type: str = ...,
loader_type: str = ...,
source_identifier: str = ...,
target_identifier: str = ...,
latest_run_status: str = ...,
latest_run_started_at_max: datetime = ...,
latest_run_started_at_min: datetime = ...,
latest_run_finished_at_max: Optional[datetime] = ...,
latest_run_finished_at_min: Optional[datetime] = ...,
start_time_max: Optional[datetime] = ...,
start_time_min: Optional[datetime] = ...,
next_run_at_max: Optional[datetime] = ...,
next_run_at_min: Optional[datetime] = ...,
paused: bool = ...,
fetch_all: bool = False,
) -> List["Task"]:
"""Fetch a collection of ETL tasks."""
return super().list(
page=page,
page_size=page_size,
order_by=order_by,
workspace_id=normalize_uuid(workspace),
orchestration_system_id=normalize_uuid(orchestration_system),
orchestration_system_type=orchestration_system_type,
data_connection_id=normalize_uuid(data_connection),
data_connection_type=data_connection_type,
extractor_type=extractor_type,
transformer_type=transformer_type,
loader_type=loader_type,
source_identifier=source_identifier,
target_identifier=target_identifier,
latest_run_status=latest_run_status,
latest_run_started_at_max=latest_run_started_at_max,
latest_run_started_at_min=latest_run_started_at_min,
latest_run_finished_at_max=latest_run_finished_at_max,
latest_run_finished_at_min=latest_run_finished_at_min,
start_time_max=start_time_max,
start_time_min=start_time_min,
next_run_at_max=next_run_at_max,
next_run_at_min=next_run_at_min,
paused=paused,
fetch_all=fetch_all,
)
def create(
self,
name: str,
workspace: Union["Workspace", UUID, str],
data_connection: Union["DataConnection", UUID, str],
orchestration_system: Union["OrchestrationSystem", UUID, str],
extractor_variables: dict = Field(default_factory=dict),
transformer_variables: dict = Field(default_factory=dict),
loader_variables: dict = Field(default_factory=dict),
paused: bool = False,
start_time: Optional[datetime] = None,
next_run_at: Optional[datetime] = None,
crontab: Optional[str] = None,
interval: Optional[int] = None,
interval_period: Optional[str] = None,
mappings: List[dict] = Field(default_factory=list),
uid: Optional[UUID] = None
) -> "Task":
"""Create a new ETL task."""
body = {
"id": normalize_uuid(uid),
"name": name,
"workspaceId": normalize_uuid(workspace),
"dataConnectionId": normalize_uuid(data_connection),
"orchestrationSystemId": normalize_uuid(orchestration_system),
"extractorVariables": extractor_variables,
"transformerVariables": transformer_variables,
"loaderVariables": loader_variables,
"schedule": {
"paused": paused,
"startTime": start_time,
"nextRunAt": next_run_at,
"crontab": crontab,
"interval": interval,
"intervalPeriod": interval_period,
} if interval or crontab else ...,
"mappings": mappings if mappings else []
}
return super().create(**body)
def update(
self,
uid: Union[UUID, str],
name: str = ...,
data_connection: Union["DataConnection", UUID, str] = ...,
orchestration_system: Union["OrchestrationSystem", UUID, str] = ...,
extractor_variables: dict = ...,
transformer_variables: dict = ...,
loader_variables: dict = ...,
paused: bool = ...,
start_time: Optional[datetime] = ...,
next_run_at: Optional[datetime] = ...,
crontab: Optional[str] = ...,
interval: Optional[int] = ...,
interval_period: Optional[str] = ...,
mappings: List[dict] = ...
) -> "DataConnection":
"""Update an ETL task."""
body: Dict[str, Any] = {
"name": name,
"dataConnectionId": normalize_uuid(data_connection),
"orchestrationSystemId": normalize_uuid(orchestration_system),
"extractorVariables": extractor_variables,
"transformerVariables": transformer_variables,
"loaderVariables": loader_variables,
"mappings": mappings
}
if crontab is None and interval is None:
body["schedule"] = None
elif any(value is not ... for value in [paused, start_time, next_run_at, crontab, interval, interval_period]):
body["schedule"] = {
"paused": paused,
"startTime": start_time,
"nextRunAt": next_run_at,
"crontab": crontab,
"interval": interval,
"intervalPeriod": interval_period,
}
return super().update(uid=str(uid), **body)
def run(self, uid: Union[UUID, str]):
"""Run an ETL task."""
self.client.request(
"post", f"/{self.client.base_route}/{self.model.get_route()}/{str(uid)}"
)
def get_task_runs(
self,
uid: Union[UUID, str],
page: int = ...,
page_size: int = 100,
order_by: List[str] = ...,
status: str = ...,
started_at_max: datetime = ...,
started_at_min: datetime = ...,
finished_at_max: datetime = ...,
finished_at_min: datetime = ...,
) -> List["TaskRun"]:
"""Retrieve task runs of a task."""
params = {
"page": page,
"page_size": page_size,
"order_by": ",".join(order_by) if order_by is not ... else order_by,
"status": status,
"started_at_max": started_at_max,
"started_at_min": started_at_min,
"finished_at_max": finished_at_max,
"finished_at_min": finished_at_min,
}
params = {
k: ("null" if v is None else v)
for k, v in params.items()
if v is not ...
}
path = f"/{self.client.base_route}/{self.model.get_route()}/{str(uid)}/runs"
return [
TaskRun(**task_run) for task_run in self.client.request("get", path, params=params).json()
]
def create_task_run(
self,
uid: Union[UUID, str],
status: Literal["RUNNING", "SUCCESS", "FAILURE"],
started_at: datetime,
finished_at: datetime = ...,
result: dict = ...,
) -> TaskRun:
"""Create a task run record for a task."""
path = f"/{self.client.base_route}/{self.model.get_route()}/{str(uid)}/runs"
headers = {"Content-type": "application/json"}
body = {
k: ("null" if v is None else v) for k, v in {
"status": status,
"started_at": started_at,
"finished_at": finished_at,
"result": result,
}.items() if v is not ...
}
return TaskRun(**self.client.request(
"post", path, headers=headers, data=json.dumps(body, default=self.default_serializer)
).json())
def get_task_run(
self,
uid: Union[UUID, str],
task_run_id: Union[UUID, str]
) -> TaskRun:
"""Get a task run record for a task."""
return TaskRun(**self.client.request(
"get", f"/{self.client.base_route}/{self.model.get_route()}/{str(uid)}/runs/{str(task_run_id)}"
).json())
def update_task_run(
self,
uid: Union[UUID, str],
task_run_id: Union[UUID, str],
status: Literal["RUNNING", "SUCCESS", "FAILURE"] = ...,
started_at: datetime = ...,
finished_at: datetime = ...,
result: dict = ...,
) -> TaskRun:
"""Update a task run record for a task."""
path = f"/{self.client.base_route}/{self.model.get_route()}/{str(uid)}/runs/{str(task_run_id)}"
headers = {"Content-type": "application/json"}
body = {
k: ("null" if v is None else v) for k, v in {
"status": status,
"started_at": started_at,
"finished_at": finished_at,
"result": result,
}.items() if v is not ...
}
return TaskRun(**self.client.request(
"patch", path, headers=headers, data=json.dumps(body, default=self.default_serializer)
).json())
def delete_task_run(
self,
uid: Union[UUID, str],
task_run_id: Union[UUID, str]
) -> None:
"""Delete a task run record for a task."""
self.client.request(
"delete", f"/{self.client.base_route}/{self.model.get_route()}/{str(uid)}/runs/{str(task_run_id)}"
)