taipy.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from datetime import datetime
  12. from typing import Any, Callable, Dict, List, Literal, Optional, Set, Union, overload
  13. from taipy.config import Scope
  14. from taipy.logger._taipy_logger import _TaipyLogger
  15. from ._core import Core
  16. from ._entity._entity import _Entity
  17. from ._version._version_manager_factory import _VersionManagerFactory
  18. from .common._check_instance import (
  19. _is_cycle,
  20. _is_data_node,
  21. _is_job,
  22. _is_scenario,
  23. _is_sequence,
  24. _is_submission,
  25. _is_task,
  26. )
  27. from .common._warnings import _warn_deprecated, _warn_no_core_service
  28. from .config.data_node_config import DataNodeConfig
  29. from .config.scenario_config import ScenarioConfig
  30. from .cycle._cycle_manager_factory import _CycleManagerFactory
  31. from .cycle.cycle import Cycle
  32. from .cycle.cycle_id import CycleId
  33. from .data._data_manager_factory import _DataManagerFactory
  34. from .data.data_node import DataNode
  35. from .data.data_node_id import DataNodeId
  36. from .exceptions.exceptions import (
  37. DataNodeConfigIsNotGlobal,
  38. ModelNotFound,
  39. NonExistingVersion,
  40. VersionIsNotProductionVersion,
  41. )
  42. from .job._job_manager_factory import _JobManagerFactory
  43. from .job.job import Job
  44. from .job.job_id import JobId
  45. from .reason import EntityIsNotSubmittableEntity, ReasonCollection
  46. from .scenario._scenario_manager_factory import _ScenarioManagerFactory
  47. from .scenario.scenario import Scenario
  48. from .scenario.scenario_id import ScenarioId
  49. from .sequence._sequence_manager_factory import _SequenceManagerFactory
  50. from .sequence.sequence import Sequence
  51. from .sequence.sequence_id import SequenceId
  52. from .submission._submission_manager_factory import _SubmissionManagerFactory
  53. from .submission.submission import Submission, SubmissionId
  54. from .task._task_manager_factory import _TaskManagerFactory
  55. from .task.task import Task
  56. from .task.task_id import TaskId
  57. __logger = _TaipyLogger._get_logger()
  58. def set(entity: Union[DataNode, Task, Sequence, Scenario, Cycle, Submission]):
  59. """Save or update an entity.
  60. This function allows you to save or update an entity in Taipy.
  61. Parameters:
  62. entity (Union[DataNode^, Task^, Sequence^, Scenario^, Cycle^, Submission^]): The
  63. entity to save or update.
  64. """
  65. if isinstance(entity, Cycle):
  66. return _CycleManagerFactory._build_manager()._set(entity)
  67. if isinstance(entity, Scenario):
  68. return _ScenarioManagerFactory._build_manager()._set(entity)
  69. if isinstance(entity, Sequence):
  70. return _SequenceManagerFactory._build_manager()._set(entity)
  71. if isinstance(entity, Task):
  72. return _TaskManagerFactory._build_manager()._set(entity)
  73. if isinstance(entity, DataNode):
  74. return _DataManagerFactory._build_manager()._set(entity)
  75. if isinstance(entity, Submission):
  76. return _SubmissionManagerFactory._build_manager()._set(entity)
  77. def is_submittable(entity: Union[Scenario, ScenarioId, Sequence, SequenceId, Task, TaskId, str]) -> ReasonCollection:
  78. """Indicate if an entity can be submitted.
  79. This function checks if the given entity can be submitted for execution.
  80. Returns:
  81. True if the given entity can be submitted. False otherwise.
  82. """
  83. if isinstance(entity, Scenario):
  84. return _ScenarioManagerFactory._build_manager()._is_submittable(entity)
  85. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  86. return _ScenarioManagerFactory._build_manager()._is_submittable(ScenarioId(entity))
  87. if isinstance(entity, Sequence):
  88. return _SequenceManagerFactory._build_manager()._is_submittable(entity)
  89. if isinstance(entity, str) and entity.startswith(Sequence._ID_PREFIX):
  90. return _SequenceManagerFactory._build_manager()._is_submittable(SequenceId(entity))
  91. if isinstance(entity, Task):
  92. return _TaskManagerFactory._build_manager()._is_submittable(entity)
  93. if isinstance(entity, str) and entity.startswith(Task._ID_PREFIX):
  94. return _TaskManagerFactory._build_manager()._is_submittable(TaskId(entity))
  95. return ReasonCollection()._add_reason(str(entity), EntityIsNotSubmittableEntity(str(entity)))
  96. def is_editable(
  97. entity: Union[
  98. DataNode,
  99. Task,
  100. Job,
  101. Sequence,
  102. Scenario,
  103. Cycle,
  104. Submission,
  105. DataNodeId,
  106. TaskId,
  107. JobId,
  108. SequenceId,
  109. ScenarioId,
  110. CycleId,
  111. SubmissionId,
  112. ],
  113. ) -> bool:
  114. """Indicate if an entity can be edited.
  115. This function checks if the given entity can be edited.
  116. Returns:
  117. True if the given entity can be edited. False otherwise.
  118. """
  119. if isinstance(entity, Cycle):
  120. return _CycleManagerFactory._build_manager()._is_editable(entity)
  121. if isinstance(entity, str) and entity.startswith(Cycle._ID_PREFIX):
  122. return _CycleManagerFactory._build_manager()._is_editable(CycleId(entity))
  123. if isinstance(entity, Scenario):
  124. return _ScenarioManagerFactory._build_manager()._is_editable(entity)
  125. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  126. return _ScenarioManagerFactory._build_manager()._is_editable(ScenarioId(entity))
  127. if isinstance(entity, Sequence):
  128. return _SequenceManagerFactory._build_manager()._is_editable(entity)
  129. if isinstance(entity, str) and entity.startswith(Sequence._ID_PREFIX):
  130. return _SequenceManagerFactory._build_manager()._is_editable(SequenceId(entity))
  131. if isinstance(entity, Task):
  132. return _TaskManagerFactory._build_manager()._is_editable(entity)
  133. if isinstance(entity, str) and entity.startswith(Task._ID_PREFIX):
  134. return _TaskManagerFactory._build_manager()._is_editable(TaskId(entity))
  135. if isinstance(entity, Job):
  136. return _JobManagerFactory._build_manager()._is_editable(entity)
  137. if isinstance(entity, str) and entity.startswith(Job._ID_PREFIX):
  138. return _JobManagerFactory._build_manager()._is_editable(JobId(entity))
  139. if isinstance(entity, DataNode):
  140. return _DataManagerFactory._build_manager()._is_editable(entity)
  141. if isinstance(entity, str) and entity.startswith(DataNode._ID_PREFIX):
  142. return _DataManagerFactory._build_manager()._is_editable(DataNodeId(entity))
  143. if isinstance(entity, Submission):
  144. return _SubmissionManagerFactory._build_manager()._is_editable(entity)
  145. if isinstance(entity, str) and entity.startswith(Submission._ID_PREFIX):
  146. return _SubmissionManagerFactory._build_manager()._is_editable(SequenceId(entity))
  147. return False
  148. def is_readable(
  149. entity: Union[
  150. DataNode,
  151. Task,
  152. Job,
  153. Sequence,
  154. Scenario,
  155. Cycle,
  156. Submission,
  157. DataNodeId,
  158. TaskId,
  159. JobId,
  160. SequenceId,
  161. ScenarioId,
  162. CycleId,
  163. SubmissionId,
  164. ],
  165. ) -> bool:
  166. """Indicate if an entity can be read.
  167. This function checks if the given entity can be read.
  168. Returns:
  169. True if the given entity can be read. False otherwise.
  170. """
  171. if isinstance(entity, Cycle):
  172. return _CycleManagerFactory._build_manager()._is_readable(entity)
  173. if isinstance(entity, str) and entity.startswith(Cycle._ID_PREFIX):
  174. return _CycleManagerFactory._build_manager()._is_readable(CycleId(entity))
  175. if isinstance(entity, Scenario):
  176. return _ScenarioManagerFactory._build_manager()._is_readable(entity)
  177. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  178. return _ScenarioManagerFactory._build_manager()._is_readable(ScenarioId(entity))
  179. if isinstance(entity, Sequence):
  180. return _SequenceManagerFactory._build_manager()._is_readable(entity)
  181. if isinstance(entity, str) and entity.startswith(Sequence._ID_PREFIX):
  182. return _SequenceManagerFactory._build_manager()._is_readable(SequenceId(entity))
  183. if isinstance(entity, Task):
  184. return _TaskManagerFactory._build_manager()._is_readable(entity)
  185. if isinstance(entity, str) and entity.startswith(Task._ID_PREFIX):
  186. return _TaskManagerFactory._build_manager()._is_readable(TaskId(entity))
  187. if isinstance(entity, Job):
  188. return _JobManagerFactory._build_manager()._is_readable(entity)
  189. if isinstance(entity, str) and entity.startswith(Job._ID_PREFIX):
  190. return _JobManagerFactory._build_manager()._is_readable(JobId(entity))
  191. if isinstance(entity, DataNode):
  192. return _DataManagerFactory._build_manager()._is_readable(entity)
  193. if isinstance(entity, str) and entity.startswith(DataNode._ID_PREFIX):
  194. return _DataManagerFactory._build_manager()._is_readable(DataNodeId(entity))
  195. if isinstance(entity, Submission):
  196. return _SubmissionManagerFactory._build_manager()._is_readable(entity)
  197. if isinstance(entity, str) and entity.startswith(Submission._ID_PREFIX):
  198. return _SubmissionManagerFactory._build_manager()._is_readable(SequenceId(entity))
  199. return False
  200. @_warn_no_core_service("The submitted entity will not be executed until the Core service is running.")
  201. def submit(
  202. entity: Union[Scenario, Sequence, Task],
  203. force: bool = False,
  204. wait: bool = False,
  205. timeout: Optional[Union[float, int]] = None,
  206. **properties,
  207. ) -> Submission:
  208. """Submit a scenario, sequence or task entity for execution.
  209. This function submits the given entity for execution and returns the created job(s).
  210. If the entity is a sequence or a scenario, all the tasks of the entity are
  211. submitted for execution.
  212. Parameters:
  213. entity (Union[Scenario^, Sequence^, Task^]): The scenario, sequence or task to submit.
  214. force (bool): If True, the execution is forced even if for skippable tasks.
  215. wait (bool): Wait for the orchestrated jobs created from the submission to be finished
  216. in asynchronous mode.
  217. timeout (Union[float, int]): The optional maximum number of seconds to wait
  218. for the jobs to be finished before returning.
  219. **properties (dict[str, any]): A key-worded variable length list of user additional arguments
  220. that will be stored within the `Submission^`. It can be accessed via `Submission.properties^`.
  221. Returns:
  222. The created `Submission^` containing the information about the submission.
  223. """
  224. if isinstance(entity, Scenario):
  225. return _ScenarioManagerFactory._build_manager()._submit(
  226. entity, force=force, wait=wait, timeout=timeout, **properties
  227. )
  228. if isinstance(entity, Sequence):
  229. return _SequenceManagerFactory._build_manager()._submit(
  230. entity, force=force, wait=wait, timeout=timeout, **properties
  231. )
  232. if isinstance(entity, Task):
  233. return _TaskManagerFactory._build_manager()._submit(
  234. entity, force=force, wait=wait, timeout=timeout, **properties
  235. )
  236. return None
  237. @overload
  238. def exists(entity_id: TaskId) -> bool:
  239. ...
  240. @overload
  241. def exists(entity_id: DataNodeId) -> bool:
  242. ...
  243. @overload
  244. def exists(entity_id: SequenceId) -> bool:
  245. ...
  246. @overload
  247. def exists(entity_id: ScenarioId) -> bool:
  248. ...
  249. @overload
  250. def exists(entity_id: CycleId) -> bool:
  251. ...
  252. @overload
  253. def exists(entity_id: JobId) -> bool:
  254. ...
  255. @overload
  256. def exists(entity_id: SubmissionId) -> bool:
  257. ...
  258. @overload
  259. def exists(entity_id: str) -> bool:
  260. ...
  261. def exists(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId, str]) -> bool:
  262. """Check if an entity with the specified identifier exists.
  263. This function checks if an entity with the given identifier exists.
  264. It supports various types of entity identifiers, including `TaskId^`,
  265. `DataNodeId^`, `SequenceId^`, `ScenarioId^`, `JobId^`, `CycleId^`, `SubmissionId^`, and string
  266. representations.
  267. Parameters:
  268. entity_id (Union[DataNodeId^, TaskId^, SequenceId^, ScenarioId^, JobId^, CycleId^, SubmissionId^, str]): The
  269. identifier of the entity to check for existence.
  270. Returns:
  271. True if the given entity exists. False otherwise.
  272. Raises:
  273. ModelNotFound: If the entity's type cannot be determined.
  274. Note:
  275. The function performs checks for various entity types
  276. (`Job^`, `Cycle^`, `Scenario^`, `Sequence^`, `Task^`, `DataNode^`, `Submission^`)
  277. based on their respective identifier prefixes.
  278. """
  279. if _is_job(entity_id):
  280. return _JobManagerFactory._build_manager()._exists(JobId(entity_id))
  281. if _is_cycle(entity_id):
  282. return _CycleManagerFactory._build_manager()._exists(CycleId(entity_id))
  283. if _is_scenario(entity_id):
  284. return _ScenarioManagerFactory._build_manager()._exists(ScenarioId(entity_id))
  285. if _is_sequence(entity_id):
  286. return _SequenceManagerFactory._build_manager()._exists(SequenceId(entity_id))
  287. if _is_task(entity_id):
  288. return _TaskManagerFactory._build_manager()._exists(TaskId(entity_id))
  289. if _is_data_node(entity_id):
  290. return _DataManagerFactory._build_manager()._exists(DataNodeId(entity_id))
  291. if _is_submission(entity_id):
  292. return _SubmissionManagerFactory._build_manager()._exists(SubmissionId(entity_id))
  293. raise ModelNotFound("NOT_DETERMINED", entity_id)
  294. @overload
  295. def get(entity_id: TaskId) -> Task:
  296. ...
  297. @overload
  298. def get(entity_id: DataNodeId) -> DataNode:
  299. ...
  300. @overload
  301. def get(entity_id: SequenceId) -> Sequence:
  302. ...
  303. @overload
  304. def get(entity_id: ScenarioId) -> Scenario:
  305. ...
  306. @overload
  307. def get(entity_id: CycleId) -> Cycle:
  308. ...
  309. @overload
  310. def get(entity_id: JobId) -> Job:
  311. ...
  312. @overload
  313. def get(entity_id: SubmissionId) -> Submission:
  314. ...
  315. @overload
  316. def get(entity_id: str) -> Union[Task, DataNode, Sequence, Scenario, Job, Cycle, Submission]:
  317. ...
  318. def get(
  319. entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId, str],
  320. ) -> Union[Task, DataNode, Sequence, Scenario, Job, Cycle, Submission]:
  321. """Retrieve an entity by its unique identifier.
  322. This function allows you to retrieve an entity by specifying its identifier.
  323. The identifier must match the pattern of one of the supported entity types:
  324. `Task^`, `DataNode^`, `Sequence^`, `Job^`, `Cycle^`, `Submission^`, or `Scenario^`.
  325. Parameters:
  326. entity_id (Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, str]):
  327. The identifier of the entity to retrieve.<br/>
  328. It should conform to the identifier pattern of one of the entities (`Task^`, `DataNode^`,
  329. `Sequence^`, `Job^`, `Cycle^` or `Scenario^`).
  330. Returns:
  331. The entity that corresponds to the provided identifier. Returns None if no matching entity is found.
  332. Raises:
  333. ModelNotFound^: If the provided *entity_id* does not match any known entity pattern.
  334. """
  335. if _is_job(entity_id):
  336. return _JobManagerFactory._build_manager()._get(JobId(entity_id))
  337. if _is_cycle(entity_id):
  338. return _CycleManagerFactory._build_manager()._get(CycleId(entity_id))
  339. if _is_scenario(entity_id):
  340. return _ScenarioManagerFactory._build_manager()._get(ScenarioId(entity_id))
  341. if _is_sequence(entity_id):
  342. return _SequenceManagerFactory._build_manager()._get(SequenceId(entity_id))
  343. if _is_task(entity_id):
  344. return _TaskManagerFactory._build_manager()._get(TaskId(entity_id))
  345. if _is_data_node(entity_id):
  346. return _DataManagerFactory._build_manager()._get(DataNodeId(entity_id))
  347. if _is_submission(entity_id):
  348. return _SubmissionManagerFactory._build_manager()._get(SubmissionId(entity_id))
  349. raise ModelNotFound("NOT_DETERMINED", entity_id)
  350. def get_tasks() -> List[Task]:
  351. """Retrieve a list of all existing tasks.
  352. This function returns a list of all tasks that currently exist.
  353. Returns:
  354. A list containing all the tasks.
  355. """
  356. return _TaskManagerFactory._build_manager()._get_all()
  357. def is_deletable(entity: Union[Scenario, Job, Submission, ScenarioId, JobId, SubmissionId]) -> bool:
  358. """Check if a `Scenario^`, a `Job^` or a `Submission^` can be deleted.
  359. This function determines whether a scenario or a job can be safely
  360. deleted without causing conflicts or issues.
  361. Parameters:
  362. entity (Union[Scenario, Job, Submission, ScenarioId, JobId, SubmissionId]): The scenario,
  363. job or submission to check.
  364. Returns:
  365. True if the given scenario, job or submission can be deleted. False otherwise.
  366. """
  367. if isinstance(entity, Job):
  368. return _JobManagerFactory._build_manager()._is_deletable(entity)
  369. if isinstance(entity, str) and entity.startswith(Job._ID_PREFIX):
  370. return _JobManagerFactory._build_manager()._is_deletable(JobId(entity))
  371. if isinstance(entity, Scenario):
  372. return _ScenarioManagerFactory._build_manager()._is_deletable(entity)
  373. if isinstance(entity, str) and entity.startswith(Scenario._ID_PREFIX):
  374. return _ScenarioManagerFactory._build_manager()._is_deletable(ScenarioId(entity))
  375. if isinstance(entity, Submission):
  376. return _SubmissionManagerFactory._build_manager()._is_deletable(entity)
  377. if isinstance(entity, str) and entity.startswith(Submission._ID_PREFIX):
  378. return _SubmissionManagerFactory._build_manager()._is_deletable(SubmissionId(entity))
  379. return True
  380. def delete(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId]):
  381. """Delete an entity and its nested entities.
  382. This function deletes the specified entity and recursively deletes all its nested entities.
  383. The behavior varies depending on the type of entity provided:
  384. - If a `CycleId` is provided, the nested scenarios, tasks, data nodes, and jobs are deleted.
  385. - If a `ScenarioId` is provided, the nested sequences, tasks, data nodes, submissions and jobs are deleted.
  386. If the scenario is primary, it can only be deleted if it is the only scenario in the cycle.
  387. In that case, its cycle is also deleted. Use the `is_deletable()^` function to check if
  388. the scenario can be deleted.
  389. - If a `SequenceId` is provided, the related jobs are deleted.
  390. - If a `TaskId` is provided, the related data nodes, and jobs are deleted.
  391. - If a `DataNodeId` is provided, the data node is deleted.
  392. - If a `SubmissionId^` is provided, the related jobs are deleted.
  393. The submission can only be deleted if the execution has been finished.
  394. - If a `JobId^` is provided, the job entity can only be deleted if the execution has been finished.
  395. Parameters:
  396. entity_id (Union[TaskId, DataNodeId, SequenceId, ScenarioId, SubmissionId, JobId, CycleId]):
  397. The identifier of the entity to delete.
  398. Raises:
  399. ModelNotFound: No entity corresponds to the specified *entity_id*.
  400. """
  401. if _is_job(entity_id):
  402. job_manager = _JobManagerFactory._build_manager()
  403. return job_manager._delete(job_manager._get(JobId(entity_id)))
  404. if _is_cycle(entity_id):
  405. return _CycleManagerFactory._build_manager()._hard_delete(CycleId(entity_id))
  406. if _is_scenario(entity_id):
  407. return _ScenarioManagerFactory._build_manager()._hard_delete(ScenarioId(entity_id))
  408. if _is_sequence(entity_id):
  409. return _SequenceManagerFactory._build_manager()._hard_delete(SequenceId(entity_id))
  410. if _is_task(entity_id):
  411. return _TaskManagerFactory._build_manager()._hard_delete(TaskId(entity_id))
  412. if _is_data_node(entity_id):
  413. return _DataManagerFactory._build_manager()._delete(DataNodeId(entity_id))
  414. if _is_submission(entity_id):
  415. return _SubmissionManagerFactory._build_manager()._hard_delete(SubmissionId(entity_id))
  416. raise ModelNotFound("NOT_DETERMINED", entity_id)
  417. def get_scenarios(
  418. cycle: Optional[Cycle] = None,
  419. tag: Optional[str] = None,
  420. is_sorted: bool = False,
  421. descending: bool = False,
  422. created_start_time: Optional[datetime] = None,
  423. created_end_time: Optional[datetime] = None,
  424. sort_key: Literal["name", "id", "config_id", "creation_date", "tags"] = "name",
  425. ) -> List[Scenario]:
  426. """Retrieve a list of existing scenarios filtered by cycle or tag.
  427. This function allows you to retrieve a list of scenarios based on optional
  428. filtering criteria. If both a *cycle* and a *tag* are provided, the returned
  429. list contains scenarios that belong to the specified *cycle* and also
  430. have the specified *tag*.
  431. Parameters:
  432. cycle (Optional[Cycle^]): The optional `Cycle^` to filter scenarios by.
  433. tag (Optional[str]): The optional tag to filter scenarios by.
  434. is_sorted (bool): If True, sort the output list of scenarios using the sorting key.
  435. The default value is False.
  436. descending (bool): If True, sort the output list of scenarios in descending order.
  437. The default value is False.
  438. created_start_time (Optional[datetime]): The optional inclusive start date to filter scenarios by creation date.
  439. created_end_time (Optional[datetime]): The optional exclusive end date to filter scenarios by creation date.
  440. sort_key (Literal["name", "id", "creation_date", "tags"]): The optional sort_key to
  441. decide upon what key scenarios are sorted. The sorting is in increasing order for
  442. dates, in alphabetical order for name and id, and in lexicographical order for tags.
  443. The default value is "name".<br/>
  444. If an incorrect sorting key is provided, the scenarios are sorted by name.
  445. Returns:
  446. The list of scenarios filtered by cycle or tag.
  447. """
  448. scenario_manager = _ScenarioManagerFactory._build_manager()
  449. if not cycle and not tag:
  450. scenarios = scenario_manager._get_all()
  451. elif cycle and not tag:
  452. scenarios = scenario_manager._get_all_by_cycle(cycle)
  453. elif not cycle and tag:
  454. scenarios = scenario_manager._get_all_by_tag(tag)
  455. elif cycle and tag:
  456. cycles_scenarios = scenario_manager._get_all_by_cycle(cycle)
  457. scenarios = [scenario for scenario in cycles_scenarios if scenario.has_tag(tag)]
  458. else:
  459. scenarios = []
  460. if created_start_time or created_end_time:
  461. scenarios = scenario_manager._filter_by_creation_time(scenarios, created_start_time, created_end_time)
  462. if is_sorted:
  463. scenario_manager._sort_scenarios(scenarios, descending, sort_key)
  464. return scenarios
  465. def get_primary(cycle: Cycle) -> Optional[Scenario]:
  466. """Retrieve the primary scenario associated with a cycle.
  467. Parameters:
  468. cycle (Cycle^): The cycle for which to retrieve the primary scenario.
  469. Returns:
  470. The primary scenario of the given _cycle_. If the cycle has no
  471. primary scenario, this method returns None.
  472. """
  473. return _ScenarioManagerFactory._build_manager()._get_primary(cycle)
  474. def get_primary_scenarios(
  475. is_sorted: bool = False,
  476. descending: bool = False,
  477. created_start_time: Optional[datetime] = None,
  478. created_end_time: Optional[datetime] = None,
  479. sort_key: Literal["name", "id", "config_id", "creation_date", "tags"] = "name",
  480. ) -> List[Scenario]:
  481. """Retrieve a list of all primary scenarios.
  482. Parameters:
  483. is_sorted (bool): If True, sort the output list of scenarios using the sorting key.
  484. The default value is False.
  485. descending (bool): If True, sort the output list of scenarios in descending order.
  486. The default value is False.
  487. created_start_time (Optional[datetime]): The optional inclusive start date to filter scenarios by creation date.
  488. created_end_time (Optional[datetime]): The optional exclusive end date to filter scenarios by creation date.
  489. sort_key (Literal["name", "id", "creation_date", "tags"]): The optional sort_key to
  490. decide upon what key scenarios are sorted. The sorting is in increasing order for
  491. dates, in alphabetical order for name and id, and in lexicographical order for tags.
  492. The default value is "name".<br/>
  493. If an incorrect sorting key is provided, the scenarios are sorted by name.
  494. Returns:
  495. A list contains all primary scenarios.
  496. """
  497. scenario_manager = _ScenarioManagerFactory._build_manager()
  498. scenarios = scenario_manager._get_primary_scenarios()
  499. if created_start_time or created_end_time:
  500. scenarios = scenario_manager._filter_by_creation_time(scenarios, created_start_time, created_end_time)
  501. if is_sorted:
  502. scenario_manager._sort_scenarios(scenarios, descending, sort_key)
  503. return scenarios
  504. def is_promotable(scenario: Union[Scenario, ScenarioId]) -> bool:
  505. """Determine if a scenario can be promoted to become a primary scenario.
  506. This function checks whether the given scenario is eligible to be promoted
  507. as a primary scenario.
  508. Parameters:
  509. scenario (Union[Scenario, ScenarioId]): The scenario to be evaluated for promotion.
  510. Returns:
  511. True if the given scenario can be promoted to be a primary scenario. False otherwise.
  512. """
  513. return _ScenarioManagerFactory._build_manager()._is_promotable_to_primary(scenario)
  514. def set_primary(scenario: Scenario):
  515. """Promote a scenario as the primary scenario of its cycle.
  516. This function promotes the given scenario as the primary scenario of its associated cycle.
  517. If the cycle already has a primary scenario, that scenario is demoted and is
  518. no longer considered the primary scenario for its cycle.
  519. Parameters:
  520. scenario (Scenario^): The scenario to promote as the new _primary_ scenario.
  521. """
  522. return _ScenarioManagerFactory._build_manager()._set_primary(scenario)
  523. def tag(scenario: Scenario, tag: str):
  524. """Add a tag to a scenario.
  525. This function adds a user-defined tag to the specified scenario. If another scenario
  526. within the same cycle already has the same tag applied, the previous scenario is untagged.
  527. Parameters:
  528. scenario (Scenario^): The scenario to which the tag will be added.
  529. tag (str): The tag to apply to the scenario.
  530. """
  531. return _ScenarioManagerFactory._build_manager()._tag(scenario, tag)
  532. def untag(scenario: Scenario, tag: str):
  533. """Remove a tag from a scenario.
  534. This function removes a specified tag from the given scenario. If the scenario does
  535. not have the specified tag, it has no effect.
  536. Parameters:
  537. scenario (Scenario^): The scenario from which the tag will be removed.
  538. tag (str): The tag to remove from the scenario.
  539. """
  540. return _ScenarioManagerFactory._build_manager()._untag(scenario, tag)
  541. def compare_scenarios(*scenarios: Scenario, data_node_config_id: Optional[str] = None) -> Dict[str, Any]:
  542. """Compare the data nodes of several scenarios.
  543. You can specify which data node config identifier should the comparison be performed
  544. on.
  545. Parameters:
  546. *scenarios (*Scenario^): The list of the scenarios to compare.
  547. data_node_config_id (Optional[str]): The config identifier of the DataNode to perform
  548. the comparison on. <br/>
  549. If _data_node_config_id_ is not provided, the scenarios are
  550. compared on all defined comparators.<br/>
  551. Returns:
  552. The comparison results. The key is the data node config identifier used for
  553. comparison.
  554. Raises:
  555. InsufficientScenarioToCompare^: Raised when only one or no scenario for comparison
  556. is provided.
  557. NonExistingComparator^: Raised when the scenario comparator does not exist.
  558. DifferentScenarioConfigs^: Raised when the provided scenarios do not share the
  559. same scenario config.
  560. NonExistingScenarioConfig^: Raised when the scenario config of the provided
  561. scenarios could not be found.
  562. """
  563. return _ScenarioManagerFactory._build_manager()._compare(*scenarios, data_node_config_id=data_node_config_id)
  564. def subscribe_scenario(
  565. callback: Callable[[Scenario, Job], None],
  566. params: Optional[List[Any]] = None,
  567. scenario: Optional[Scenario] = None,
  568. ):
  569. """Subscribe a function to be called on job status change.
  570. The subscription is applied to all jobs created for the execution of _scenario_.
  571. If no scenario is provided, the subscription applies to all scenarios.
  572. Parameters:
  573. callback (Callable[[Scenario^, Job^], None]): The function to be called on
  574. status change.
  575. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  576. scenario (Optional[Scenario^]): The scenario to which the callback is applied.
  577. If None, the subscription is registered for all scenarios.
  578. Note:
  579. Notifications are applied only for jobs created **after** this subscription.
  580. """
  581. params = [] if params is None else params
  582. return _ScenarioManagerFactory._build_manager()._subscribe(callback, params, scenario)
  583. def unsubscribe_scenario(
  584. callback: Callable[[Scenario, Job], None], params: Optional[List[Any]] = None, scenario: Optional[Scenario] = None
  585. ):
  586. """Unsubscribe a function that is called when the status of a `Job^` changes.
  587. If no scenario is provided, the subscription is removed for all scenarios.
  588. Parameters:
  589. callback (Callable[[Scenario^, Job^], None]): The function to unsubscribe from.
  590. params (Optional[List[Any]]): The parameters to be passed to the callback.
  591. scenario (Optional[Scenario]): The scenario to unsubscribe from. If None, it
  592. applies to all scenarios.
  593. Note:
  594. The callback function will continue to be called for ongoing jobs.
  595. """
  596. return _ScenarioManagerFactory._build_manager()._unsubscribe(callback, params, scenario)
  597. def subscribe_sequence(
  598. callback: Callable[[Sequence, Job], None], params: Optional[List[Any]] = None, sequence: Optional[Sequence] = None
  599. ):
  600. """Subscribe a function to be called on job status change.
  601. The subscription is applied to all jobs created for the execution of _sequence_.
  602. Parameters:
  603. callback (Callable[[Sequence^, Job^], None]): The callable function to be called on
  604. status change.
  605. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  606. sequence (Optional[Sequence^]): The sequence to subscribe on. If None, the subscription
  607. is applied to all sequences.
  608. Note:
  609. Notifications are applied only for jobs created **after** this subscription.
  610. """
  611. return _SequenceManagerFactory._build_manager()._subscribe(callback, params, sequence)
  612. def unsubscribe_sequence(
  613. callback: Callable[[Sequence, Job], None], params: Optional[List[Any]] = None, sequence: Optional[Sequence] = None
  614. ):
  615. """Unsubscribe a function that is called when the status of a Job changes.
  616. Parameters:
  617. callback (Callable[[Sequence^, Job^], None]): The callable function to be called on
  618. status change.
  619. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  620. sequence (Optional[Sequence^]): The sequence to unsubscribe to. If None, it applies
  621. to all sequences.
  622. Note:
  623. The function will continue to be called for ongoing jobs.
  624. """
  625. return _SequenceManagerFactory._build_manager()._unsubscribe(callback, params, sequence)
  626. def get_sequences() -> List[Sequence]:
  627. """Return all existing sequences.
  628. Returns:
  629. The list of all sequences.
  630. """
  631. return _SequenceManagerFactory._build_manager()._get_all()
  632. def get_jobs() -> List[Job]:
  633. """Return all the existing jobs.
  634. Returns:
  635. The list of all jobs.
  636. """
  637. return _JobManagerFactory._build_manager()._get_all()
  638. def get_submissions() -> List[Submission]:
  639. """Return all the existing submissions.
  640. Returns:
  641. The list of all submissions.
  642. """
  643. return _SubmissionManagerFactory._build_manager()._get_all()
  644. def delete_job(job: Job, force: Optional[bool] = False):
  645. """Delete a job.
  646. This function deletes the specified job. If the job is not completed and
  647. *force* is not set to True, a `JobNotDeletedException^` may be raised.
  648. Parameters:
  649. job (Job^): The job to delete.
  650. force (Optional[bool]): If True, forces the deletion of _job_, even
  651. if it is not completed yet.
  652. Raises:
  653. JobNotDeletedException^: If the job is not finished.
  654. """
  655. return _JobManagerFactory._build_manager()._delete(job, force)
  656. def delete_jobs():
  657. """Delete all jobs."""
  658. return _JobManagerFactory._build_manager()._delete_all()
  659. def cancel_job(job: Union[str, Job]):
  660. """Cancel a job and set the status of the subsequent jobs to ABANDONED.
  661. This function cancels the specified job and sets the status of any subsequent jobs to ABANDONED.
  662. Parameters:
  663. job (Job^): The job to cancel.
  664. """
  665. _JobManagerFactory._build_manager()._cancel(job)
  666. def get_latest_job(task: Task) -> Optional[Job]:
  667. """Return the latest job of a task.
  668. This function retrieves the latest job associated with a task.
  669. Parameters:
  670. task (Task^): The task to retrieve the latest job from.
  671. Returns:
  672. The latest job created from _task_, or None if no job has been created from _task_.
  673. """
  674. return _JobManagerFactory._build_manager()._get_latest(task)
  675. def get_latest_submission(entity: Union[Scenario, Sequence, Task]) -> Optional[Submission]:
  676. """Return the latest submission of a scenario, sequence or task.
  677. This function retrieves the latest submission associated with a scenario, sequence or task.
  678. Parameters:
  679. entity (Union[Scenario^, Sequence^, Task^]): The scenario, sequence or task to
  680. retrieve the latest submission from.
  681. Returns:
  682. The latest submission created from _scenario_, _sequence_ and _task_, or None
  683. if no submission has been created from _scenario_, _sequence_ and _task_.
  684. """
  685. return _SubmissionManagerFactory._build_manager()._get_latest(entity)
  686. def get_data_nodes() -> List[DataNode]:
  687. """Return all the existing data nodes.
  688. Returns:
  689. The list of all data nodes.
  690. """
  691. return _DataManagerFactory._build_manager()._get_all()
  692. def get_cycles() -> List[Cycle]:
  693. """Return the list of all existing cycles.
  694. Returns:
  695. The list of all cycles.
  696. """
  697. return _CycleManagerFactory._build_manager()._get_all()
  698. def can_create(config: Optional[Union[ScenarioConfig, DataNodeConfig]] = None) -> ReasonCollection:
  699. """Indicate if a config can be created. The config should be a scenario or data node config.
  700. If no config is provided, the function indicates if any scenario or data node config can be created.
  701. Returns:
  702. True if the given config can be created. False otherwise.
  703. """
  704. if isinstance(config, DataNodeConfig):
  705. return _DataManagerFactory._build_manager()._can_create(config)
  706. return _ScenarioManagerFactory._build_manager()._can_create(config)
  707. def create_scenario(
  708. config: ScenarioConfig,
  709. creation_date: Optional[datetime] = None,
  710. name: Optional[str] = None,
  711. ) -> Scenario:
  712. """Create and return a new scenario based on a scenario configuration.
  713. This function checks and locks the configuration, manages application's version,
  714. and creates a new scenario from the scenario configuration provided.
  715. If the scenario belongs to a cycle, the cycle (corresponding to the _creation_date_
  716. and the configuration frequency attribute) is created if it does not exist yet.
  717. Parameters:
  718. config (ScenarioConfig^): The scenario configuration used to create a new scenario.
  719. creation_date (Optional[datetime.datetime]): The creation date of the scenario.
  720. If None, the current date time is used.
  721. name (Optional[str]): The displayable name of the scenario.
  722. Returns:
  723. The new scenario.
  724. Raises:
  725. SystemExit: If the configuration check returns some errors.
  726. """
  727. Core._manage_version_and_block_config()
  728. return _ScenarioManagerFactory._build_manager()._create(config, creation_date, name)
  729. def create_global_data_node(config: DataNodeConfig) -> DataNode:
  730. """Create and return a new GLOBAL data node from a data node configuration.
  731. This function checks and locks the configuration, manages application's version,
  732. and creates the new data node from the data node configuration provided.
  733. Parameters:
  734. config (DataNodeConfig^): The data node configuration. It must have a `GLOBAL` scope.
  735. Returns:
  736. The new global data node.
  737. Raises:
  738. DataNodeConfigIsNotGlobal^: If the data node configuration does not have GLOBAL scope.
  739. SystemExit: If the configuration check returns some errors.
  740. """
  741. # Check if the data node config has GLOBAL scope
  742. if config.scope is not Scope.GLOBAL:
  743. raise DataNodeConfigIsNotGlobal(config.id)
  744. Core._manage_version_and_block_config()
  745. if dns := _DataManagerFactory._build_manager()._get_by_config_id(config.id):
  746. return dns[0]
  747. return _DataManagerFactory._build_manager()._create_and_set(config, None, None)
  748. def clean_all_entities_by_version(version_number=None) -> bool:
  749. """Deprecated. Use `clean_all_entities` function instead."""
  750. _warn_deprecated("'clean_all_entities_by_version'", suggest="the 'clean_all_entities' function")
  751. return clean_all_entities(version_number)
  752. def clean_all_entities(version_number: str) -> bool:
  753. """Deletes all entities associated with the specified version.
  754. Parameters:
  755. version_number (str): The version number of the entities to be deleted.
  756. The version_number should not be a production version.
  757. Returns:
  758. True if the operation succeeded, False otherwise.
  759. Notes:
  760. - If the specified version does not exist, the operation will be aborted, and False will be returned.
  761. - If the specified version is a production version, the operation will be aborted, and False will be returned.
  762. - This function cleans all entities, including jobs, submissions, scenarios, cycles, sequences, tasks,
  763. and data nodes.
  764. """
  765. version_manager = _VersionManagerFactory._build_manager()
  766. try:
  767. version_number = version_manager._replace_version_number(version_number)
  768. except NonExistingVersion as e:
  769. __logger.warning(f"{e.message} Abort cleaning the entities of version '{version_number}'.")
  770. return False
  771. if version_number in version_manager._get_production_versions():
  772. __logger.warning(
  773. f"Abort cleaning the entities of version '{version_number}'. A production version can not be deleted."
  774. )
  775. return False
  776. _JobManagerFactory._build_manager()._delete_by_version(version_number)
  777. _SubmissionManagerFactory._build_manager()._delete_by_version(version_number)
  778. _ScenarioManagerFactory._build_manager()._delete_by_version(version_number)
  779. _SequenceManagerFactory._build_manager()._delete_by_version(version_number)
  780. _TaskManagerFactory._build_manager()._delete_by_version(version_number)
  781. _DataManagerFactory._build_manager()._delete_by_version(version_number)
  782. version_manager._delete(version_number)
  783. try:
  784. version_manager._delete_production_version(version_number)
  785. except VersionIsNotProductionVersion:
  786. pass
  787. return True
  788. def get_parents(
  789. entity: Union[TaskId, DataNodeId, SequenceId, Task, DataNode, Sequence], parent_dict=None
  790. ) -> Dict[str, Set[_Entity]]:
  791. """Get the parents of an entity from itself or its identifier.
  792. Parameters:
  793. entity (Union[TaskId, DataNodeId, SequenceId, Task, DataNode, Sequence]): The entity or its
  794. identifier to get the parents.
  795. Returns:
  796. The dictionary of all parent entities.
  797. They are grouped by their type (Scenario^, Sequences^, or tasks^) so each key corresponds
  798. to a level of the parents and the value is a set of the parent entities.
  799. An empty dictionary is returned if the entity does not have parents.<br/>
  800. Example: The following instruction returns all the scenarios that include the
  801. datanode identified by "my_datanode_id".
  802. `taipy.get_parents("id_of_my_datanode")["scenario"]`
  803. Raises:
  804. ModelNotFound^: If _entity_ does not match a correct entity pattern.
  805. """
  806. def update_parent_dict(parents_set, parent_dict):
  807. for k, value in parents_set.items():
  808. if k in parent_dict.keys():
  809. parent_dict[k].update(value)
  810. else:
  811. parent_dict[k] = value
  812. if isinstance(entity, str):
  813. entity = get(entity)
  814. parent_dict = parent_dict or {}
  815. if isinstance(entity, (Scenario, Cycle)):
  816. return parent_dict
  817. current_parent_dict: Dict[str, Set] = {}
  818. for parent in entity.parent_ids:
  819. parent_entity = get(parent)
  820. if parent_entity._MANAGER_NAME in current_parent_dict.keys():
  821. current_parent_dict[parent_entity._MANAGER_NAME].add(parent_entity)
  822. else:
  823. current_parent_dict[parent_entity._MANAGER_NAME] = {parent_entity}
  824. if isinstance(entity, Sequence):
  825. update_parent_dict(current_parent_dict, parent_dict)
  826. if isinstance(entity, Task):
  827. parent_entity_key_to_search_next = "scenario"
  828. update_parent_dict(current_parent_dict, parent_dict)
  829. for parent in parent_dict.get(parent_entity_key_to_search_next, []):
  830. get_parents(parent, parent_dict)
  831. if isinstance(entity, DataNode):
  832. parent_entity_key_to_search_next = "task"
  833. update_parent_dict(current_parent_dict, parent_dict)
  834. for parent in parent_dict.get(parent_entity_key_to_search_next, []):
  835. get_parents(parent, parent_dict)
  836. return parent_dict
  837. def get_cycles_scenarios() -> Dict[Optional[Cycle], List[Scenario]]:
  838. """Get the scenarios grouped by cycles.
  839. Returns:
  840. The dictionary of all cycles and their corresponding scenarios.
  841. """
  842. cycles_scenarios: Dict[Optional[Cycle], List[Scenario]] = {}
  843. for scenario in get_scenarios():
  844. if scenario.cycle in cycles_scenarios.keys():
  845. cycles_scenarios[scenario.cycle].append(scenario)
  846. else:
  847. cycles_scenarios[scenario.cycle] = [scenario]
  848. return cycles_scenarios
  849. def get_entities_by_config_id(
  850. config_id: str,
  851. ) -> Union[List, List[Task], List[DataNode], List[Sequence], List[Scenario]]:
  852. """Get the entities by its config id.
  853. Parameters:
  854. config_id (str): The config id of the entities
  855. Returns:
  856. The list of all entities by the config id.
  857. """
  858. entities: List = []
  859. if entities := _ScenarioManagerFactory._build_manager()._get_by_config_id(config_id):
  860. return entities
  861. if entities := _TaskManagerFactory._build_manager()._get_by_config_id(config_id):
  862. return entities
  863. if entities := _DataManagerFactory._build_manager()._get_by_config_id(config_id):
  864. return entities
  865. return entities