11from __future__ import annotations
22from functools import cached_property
33import uuid
4- from typing import ClassVar , TYPE_CHECKING , List , Optional
5- from pydantic import Field
4+ from typing import ClassVar , TYPE_CHECKING , List , Optional , Literal , Union
5+ from datetime import datetime
6+ from pydantic import Field , AliasPath , AliasChoices
67from ..base import HydroServerBaseModel
78from .orchestration_system import OrchestrationSystem
8- from .job import Job
9- from .schedule import TaskSchedule
9+ from .data_connection import DataConnection
1010from .run import TaskRun
11- from .mapping import TaskMapping
1211
1312
1413if TYPE_CHECKING :
@@ -21,21 +20,39 @@ class Task(HydroServerBaseModel):
2120 extractor_settings : dict = Field (default_factory = dict , alias = "extractorSettings" )
2221 transformer_settings : dict = Field (default_factory = dict , alias = "transformerSettings" )
2322 loader_settings : dict = Field (default_factory = dict , alias = "loaderSettings" )
24- job_id : uuid .UUID
25- orchestration_system_id : Optional [uuid .UUID ] = None
26- workspace_id : uuid .UUID
27- schedule : Optional [TaskSchedule ] = None
28- latest_run : TaskRun
29- mappings : List [TaskMapping ]
23+ data_connection_id : uuid .UUID = Field (
24+ None , validation_alias = AliasChoices ("dataConnectionId" , AliasPath ("dataConnection" , "id" ))
25+ )
26+ orchestration_system_id : uuid .UUID = Field (
27+ None , validation_alias = AliasChoices ("orchestrationSystemId" , AliasPath ("orchestrationSystem" , "id" ))
28+ )
29+ workspace_id : uuid .UUID = Field (
30+ None , validation_alias = AliasChoices ("workspaceId" , AliasPath ("workspace" , "id" ))
31+ )
32+ start_time : Optional [datetime ] = Field (None , validation_alias = AliasPath ("schedule" , "startTime" ))
33+ next_run_at : Optional [datetime ] = Field (None , validation_alias = AliasPath ("schedule" , "nextRunAt" ))
34+ paused : bool = Field (False , validation_alias = AliasPath ("schedule" , "paused" ))
35+ interval : Optional [int ] = Field (None , gt = 0 , validation_alias = AliasPath ("schedule" , "interval" ))
36+ interval_period : Optional [Literal ["minutes" , "hours" , "days" ]] = Field (
37+ None , validation_alias = AliasPath ("schedule" , "intervalPeriod" )
38+ )
39+ crontab : Optional [str ] = Field (None , validation_alias = AliasPath ("schedule" , "crontab" ))
40+ latest_run : Optional [TaskRun ] = None
41+ mappings : List [dict ]
3042
3143 _editable_fields : ClassVar [set [str ]] = {
3244 "name" ,
3345 "extractor_settings" ,
3446 "transformer_settings" ,
3547 "loader_settings" ,
36- "job_id " ,
48+ "data_connection_id " ,
3749 "orchestration_system_id" ,
38- "schedule" ,
50+ "start_time" ,
51+ "next_run_at" ,
52+ "paused" ,
53+ "interval" ,
54+ "interval_period" ,
55+ "crontab" ,
3956 "mappings"
4057 }
4158
@@ -51,12 +68,91 @@ def workspace(self) -> Workspace:
5168 return self .client .workspaces .get (uid = self .workspace_id )
5269
5370 @cached_property
54- def orchestration_system (self ) -> OrchestrationSystem :
71+ def orchestration_system (self ) -> Optional [ OrchestrationSystem ] :
5572 return self .client .orchestrationsystems .get (uid = self .orchestration_system_id )
5673
5774 @cached_property
58- def job (self ) -> Job :
59- return self .client .jobs .get (uid = self .job_id )
75+ def data_connection (self ) -> Optional [DataConnection ]:
76+ return self .client .dataconnections .get (uid = self .data_connection_id )
77+
78+ def get_task_runs (
79+ self ,
80+ page : int = ...,
81+ page_size : int = 100 ,
82+ order_by : List [str ] = ...,
83+ status : str = ...,
84+ started_at_max : datetime = ...,
85+ started_at_min : datetime = ...,
86+ finished_at_max : datetime = ...,
87+ finished_at_min : datetime = ...,
88+ ):
89+ """Get a collection of task runs associated with this task."""
90+
91+ return self .client .tasks .get_task_runs (
92+ uid = self .uid ,
93+ page = page ,
94+ page_size = page_size ,
95+ order_by = order_by ,
96+ status = status ,
97+ started_at_max = started_at_max ,
98+ started_at_min = started_at_min ,
99+ finished_at_max = finished_at_max ,
100+ finished_at_min = finished_at_min ,
101+ )
102+
103+ def create_task_run (
104+ self ,
105+ status : Literal ["RUNNING" , "SUCCESS" , "FAILURE" ],
106+ started_at : datetime ,
107+ finished_at : datetime = ...,
108+ result : dict = ...,
109+ ):
110+ """Create a new task run for this task."""
111+
112+ return self .client .tasks .create_task_run (
113+ uid = self .uid ,
114+ status = status ,
115+ started_at = started_at ,
116+ finished_at = finished_at ,
117+ result = result ,
118+ )
119+
120+ def get_task_run (
121+ self ,
122+ uid : Union [uuid .UUID , str ],
123+ ):
124+ """Get a task run record for this task."""
125+
126+ return self .client .tasks .get_task_run (uid = self .uid , task_run_id = uid )
127+
128+ def update_task_run (
129+ self ,
130+ uid : Union [uuid .UUID , str ],
131+ status : Literal ["RUNNING" , "SUCCESS" , "FAILURE" ] = ...,
132+ started_at : datetime = ...,
133+ finished_at : datetime = ...,
134+ result : dict = ...,
135+ ):
136+ """Update a task run record of this task."""
137+
138+ return self .client .tasks .update_task_run (
139+ uid = self .uid ,
140+ task_run_id = uid ,
141+ status = status ,
142+ started_at = started_at ,
143+ finished_at = finished_at ,
144+ result = result ,
145+ )
146+
147+ def delete_task_run (
148+ self ,
149+ uid : Union [uuid .UUID , str ],
150+ ):
151+ """Delete a task run record of this task."""
152+
153+ return self .client .tasks .delete_task_run (uid = self .uid , task_run_id = uid )
60154
61155 def run (self ):
62- """"""
156+ """Run this task."""
157+
158+ return self .client .tasks .run (uid = self .uid )
0 commit comments