taipy.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. import pathlib
  12. import shutil
  13. from datetime import datetime
  14. from typing import Any, Callable, Dict, List, Optional, Set, Union, overload
  15. from taipy.config.common.scope import Scope
  16. from taipy.logger._taipy_logger import _TaipyLogger
  17. from ._entity._entity import _Entity
  18. from ._entity._reload import _get_manager
  19. from ._version._version_manager_factory import _VersionManagerFactory
  20. from .common._check_entity_and_get_manager import _check_entity_and_get_manager
  21. from .common._warnings import _warn_no_core_service
  22. from .config.data_node_config import DataNodeConfig
  23. from .config.scenario_config import ScenarioConfig
  24. from .cycle._cycle_manager_factory import _CycleManagerFactory
  25. from .cycle.cycle import Cycle
  26. from .cycle.cycle_id import CycleId
  27. from .data._data_manager_factory import _DataManagerFactory
  28. from .data.data_node import DataNode
  29. from .data.data_node_id import DataNodeId
  30. from .exceptions.exceptions import (
  31. DataNodeConfigIsNotGlobal,
  32. ModelNotFound,
  33. NonExistingVersion,
  34. VersionIsNotProductionVersion,
  35. )
  36. from .job._job_manager_factory import _JobManagerFactory
  37. from .job.job import Job
  38. from .job.job_id import JobId
  39. from .scenario._scenario_manager_factory import _ScenarioManagerFactory
  40. from .scenario.scenario import Scenario
  41. from .scenario.scenario_id import ScenarioId
  42. from .sequence._sequence_manager_factory import _SequenceManagerFactory
  43. from .sequence.sequence import Sequence
  44. from .sequence.sequence_id import SequenceId
  45. from .submission._submission_manager_factory import _SubmissionManagerFactory
  46. from .submission.submission import Submission, SubmissionId
  47. from .task._task_manager_factory import _TaskManagerFactory
  48. from .task.task import Task
  49. from .task.task_id import TaskId
  50. __logger = _TaipyLogger._get_logger()
  51. def set(entity: Union[DataNode, Task, Sequence, Scenario, Cycle]):
  52. """Save or update an entity.
  53. This function allows you to save or update an entity in Taipy.
  54. Parameters:
  55. entity (Union[DataNode^, Task^, Sequence^, Scenario^, Cycle^]): The
  56. entity to save or update.
  57. """
  58. if manager := _check_entity_and_get_manager(entity, [Cycle, Scenario, Sequence, Task, DataNode]):
  59. manager._set(entity)
  60. def is_submittable(entity: Union[Scenario, ScenarioId, Sequence, SequenceId, Task, TaskId]) -> bool:
  61. """Indicate if an entity can be submitted.
  62. This function checks if the given entity can be submitted for execution.
  63. Returns:
  64. True if the given entity can be submitted. False otherwise.
  65. """
  66. if manager := _check_entity_and_get_manager(entity, [Scenario, Sequence, Task]):
  67. return manager._is_submittable(entity) # type: ignore
  68. return False
  69. def is_editable(
  70. entity: Union[
  71. DataNode,
  72. Task,
  73. Job,
  74. Sequence,
  75. Scenario,
  76. Cycle,
  77. Submission,
  78. DataNodeId,
  79. TaskId,
  80. JobId,
  81. SequenceId,
  82. ScenarioId,
  83. CycleId,
  84. SubmissionId,
  85. ]
  86. ) -> bool:
  87. """Indicate if an entity can be edited.
  88. This function checks if the given entity can be edited.
  89. Returns:
  90. True if the given entity can be edited. False otherwise.
  91. """
  92. if manager := _check_entity_and_get_manager(entity, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]):
  93. return manager._is_editable(entity)
  94. return False
  95. def is_readable(
  96. entity: Union[
  97. DataNode,
  98. Task,
  99. Job,
  100. Sequence,
  101. Scenario,
  102. Cycle,
  103. Submission,
  104. DataNodeId,
  105. TaskId,
  106. JobId,
  107. SequenceId,
  108. ScenarioId,
  109. CycleId,
  110. SubmissionId,
  111. ]
  112. ) -> bool:
  113. """Indicate if an entity can be read.
  114. This function checks if the given entity can be read.
  115. Returns:
  116. True if the given entity can be read. False otherwise.
  117. """
  118. if manager := _check_entity_and_get_manager(entity, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]):
  119. return manager._is_readable(entity)
  120. return False
  121. @_warn_no_core_service()
  122. def submit(
  123. entity: Union[Scenario, Sequence, Task],
  124. force: bool = False,
  125. wait: bool = False,
  126. timeout: Optional[Union[float, int]] = None,
  127. ) -> Union[Job, List[Job]]:
  128. """Submit a scenario, sequence or task entity for execution.
  129. This function submits the given entity for execution and returns the created job(s).
  130. If the entity is a sequence or a scenario, all the tasks of the entity are
  131. submitted for execution.
  132. Parameters:
  133. entity (Union[Scenario^, Sequence^, Task^]): The scenario, sequence or task to submit.
  134. force (bool): If True, the execution is forced even if for skippable tasks.
  135. wait (bool): Wait for the orchestrated jobs created from the submission to be finished
  136. in asynchronous mode.
  137. timeout (Union[float, int]): The optional maximum number of seconds to wait
  138. for the jobs to be finished before returning.
  139. Returns:
  140. The created `Job^` or a collection of the created `Job^` depends on the submitted entity.
  141. - If a `Scenario^` or a `Sequence^` is provided, it will return a list of `Job^`.
  142. - If a `Task^` is provided, it will return the created `Job^`.
  143. """
  144. if manager := _check_entity_and_get_manager(entity, [Scenario, Sequence, Task]):
  145. return manager._submit(entity, force=force, wait=wait, timeout=timeout) # type: ignore
  146. @overload
  147. def exists(entity_id: TaskId) -> bool:
  148. ...
  149. @overload
  150. def exists(entity_id: DataNodeId) -> bool:
  151. ...
  152. @overload
  153. def exists(entity_id: SequenceId) -> bool:
  154. ...
  155. @overload
  156. def exists(entity_id: ScenarioId) -> bool:
  157. ...
  158. @overload
  159. def exists(entity_id: CycleId) -> bool:
  160. ...
  161. @overload
  162. def exists(entity_id: JobId) -> bool:
  163. ...
  164. @overload
  165. def exists(entity_id: SubmissionId) -> bool:
  166. ...
  167. @overload
  168. def exists(entity_id: str) -> bool:
  169. ...
  170. def exists(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId, str]) -> bool:
  171. """Check if an entity with the specified identifier exists.
  172. This function checks if an entity with the given identifier exists.
  173. It supports various types of entity identifiers, including `TaskId^`,
  174. `DataNodeId^`, `SequenceId^`, `ScenarioId^`, `JobId^`, `CycleId^`, `SubmissionId^`, and string
  175. representations.
  176. Parameters:
  177. entity_id (Union[DataNodeId^, TaskId^, SequenceId^, ScenarioId^, JobId^, CycleId^]): The
  178. identifier of the entity to check for existence.
  179. Returns:
  180. True if the given entity exists. False otherwise.
  181. Raises:
  182. ModelNotFound: If the entity's type cannot be determined.
  183. Note:
  184. The function performs checks for various entity types
  185. (`Job^`, `Cycle^`, `Scenario^`, `Sequence^`, `Task^`, `DataNode^`)
  186. based on their respective identifier prefixes.
  187. """
  188. if manager := _check_entity_and_get_manager(
  189. entity_id, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]
  190. ):
  191. return manager._exists(entity_id)
  192. raise ModelNotFound("NOT_DETERMINED", entity_id)
  193. @overload
  194. def get(entity_id: TaskId) -> Task:
  195. ...
  196. @overload
  197. def get(entity_id: DataNodeId) -> DataNode:
  198. ...
  199. @overload
  200. def get(entity_id: SequenceId) -> Sequence:
  201. ...
  202. @overload
  203. def get(entity_id: ScenarioId) -> Scenario:
  204. ...
  205. @overload
  206. def get(entity_id: CycleId) -> Cycle:
  207. ...
  208. @overload
  209. def get(entity_id: JobId) -> Job:
  210. ...
  211. @overload
  212. def get(entity_id: SubmissionId) -> Submission:
  213. ...
  214. @overload
  215. def get(entity_id: str) -> Union[Task, DataNode, Sequence, Scenario, Job, Cycle, Submission]:
  216. ...
  217. def get(
  218. entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId, str]
  219. ) -> Union[Task, DataNode, Sequence, Scenario, Job, Cycle, Submission]:
  220. """Retrieve an entity by its unique identifier.
  221. This function allows you to retrieve an entity by specifying its identifier.
  222. The identifier must match the pattern of one of the supported entity types:
  223. Task^, DataNode^, Sequence^, Job^, Cycle^, Submission^, or Scenario^.
  224. Parameters:
  225. entity_id (Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, str]):
  226. The identifier of the entity to retrieve.<br/>
  227. It should conform to the identifier pattern of one of the entities (`Task^`, `DataNode^`,
  228. `Sequence^`, `Job^`, `Cycle^` or `Scenario^`).
  229. Returns:
  230. The entity that corresponds to the provided identifier. Returns None if no matching entity is found.
  231. Raises:
  232. ModelNotFound^: If the provided *entity_id* does not match any known entity pattern.
  233. """
  234. if manager := _check_entity_and_get_manager(
  235. entity_id, [Cycle, Scenario, Sequence, Task, Job, DataNode, Submission]
  236. ):
  237. return manager._get(entity_id)
  238. raise ModelNotFound("NOT_DETERMINED", entity_id)
  239. def get_tasks() -> List[Task]:
  240. """Retrieve a list of all existing tasks.
  241. This function returns a list of all tasks that currently exist.
  242. Returns:
  243. A list containing all the tasks.
  244. """
  245. return _get_manager(Task._MANAGER_NAME)._get_all()
  246. def is_deletable(entity: Union[Scenario, Job, Submission, ScenarioId, JobId, SubmissionId]) -> bool:
  247. """Check if a `Scenario^`, a `Job^` or a `Submission^` can be deleted.
  248. This function determines whether a scenario or a job can be safely
  249. deleted without causing conflicts or issues.
  250. Parameters:
  251. entity (Union[Scenario, Job, Submission, ScenarioId, JobId, SubmissionId]): The scenario,
  252. job or submission to check.
  253. Returns:
  254. True if the given scenario, job or submission can be deleted. False otherwise.
  255. """
  256. if manager := _check_entity_and_get_manager(entity, [Scenario, Job, Submission]):
  257. return manager._is_deletable(entity) # type: ignore
  258. return True
  259. def delete(entity_id: Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId, SubmissionId]):
  260. """Delete an entity and its nested entities.
  261. This function deletes the specified entity and recursively deletes all its nested entities.
  262. The behavior varies depending on the type of entity provided:
  263. - If a `CycleId` is provided, the nested scenarios, tasks, data nodes, and jobs are deleted.
  264. - If a `ScenarioId` is provided, the nested tasks, data nodes, and jobs are deleted.
  265. If the scenario is primary, it can only be deleted if it is the only scenario in the cycle.
  266. In that case, its cycle is also deleted. Use the `is_deletable()^` function to check if
  267. the scenario can be deleted.
  268. - If a `SequenceId` is provided, the related jobs are deleted.
  269. - If a `TaskId` is provided, the related data nodes, and jobs are deleted.
  270. - If a `SubmissionId^` or a `JobId^` is provided, the submission or job entity can only be deleted if
  271. the execution has been finished.
  272. Parameters:
  273. entity_id (Union[TaskId, DataNodeId, SequenceId, ScenarioId, JobId, CycleId]):
  274. The identifier of the entity to delete.
  275. Raises:
  276. ModelNotFound: No entity corresponds to the specified *entity_id*.
  277. """
  278. if manager := _check_entity_and_get_manager(entity_id, [Cycle, Scenario, Sequence, Task]):
  279. return manager._hard_delete(entity_id) # type: ignore
  280. if manager := _check_entity_and_get_manager(entity_id, [Job, DataNode, Submission]):
  281. return manager._delete(entity_id) # type: ignore
  282. raise ModelNotFound("NOT_DETERMINED", entity_id)
  283. def get_scenarios(cycle: Optional[Cycle] = None, tag: Optional[str] = None) -> List[Scenario]:
  284. """Retrieve a list of existing scenarios filtered by cycle or tag.
  285. This function allows you to retrieve a list of scenarios based on optional
  286. filtering criteria. If both a _cycle_ and a _tag_ are provided, the returned
  287. list contains scenarios that belong to the specified _cycle_ **and** also
  288. have the specified _tag_.
  289. Parameters:
  290. cycle (Optional[Cycle^]): The optional `Cycle^` to filter scenarios by.
  291. tag (Optional[str]): The optional tag to filter scenarios by.
  292. Returns:
  293. The list of scenarios filtered by cycle or tag. If no filtering criteria
  294. are provided, this method returns all existing scenarios.
  295. """
  296. if not cycle and not tag:
  297. return _get_manager(Scenario._MANAGER_NAME)._get_all()
  298. if cycle and not tag:
  299. return _get_manager(Scenario._MANAGER_NAME)._get_all_by_cycle(cycle) # type: ignore
  300. if not cycle and tag:
  301. return _get_manager(Scenario._MANAGER_NAME)._get_all_by_tag(tag) # type: ignore
  302. if cycle and tag:
  303. cycles_scenarios = _get_manager(Scenario._MANAGER_NAME)._get_all_by_cycle(cycle) # type: ignore
  304. return [scenario for scenario in cycles_scenarios if scenario.has_tag(tag)] # type: ignore
  305. return []
  306. def get_primary(cycle: Cycle) -> Optional[Scenario]:
  307. """Retrieve the primary scenario associated with a cycle.
  308. Parameters:
  309. cycle (Cycle^): The cycle for which to retrieve the primary scenario.
  310. Returns:
  311. The primary scenario of the given _cycle_. If the cycle has no
  312. primary scenario, this method returns None.
  313. """
  314. return _ScenarioManagerFactory._build_manager()._get_primary(cycle)
  315. def get_primary_scenarios() -> List[Scenario]:
  316. """Retrieve a list of all primary scenarios.
  317. Returns:
  318. A list containing all primary scenarios.
  319. """
  320. return _ScenarioManagerFactory._build_manager()._get_primary_scenarios()
  321. def is_promotable(scenario: Union[Scenario, ScenarioId]) -> bool:
  322. """Determine if a scenario can be promoted to become a primary scenario.
  323. This function checks whether the given scenario is eligible to be promoted
  324. as a primary scenario.
  325. Parameters:
  326. scenario (Union[Scenario, ScenarioId]): The scenario to be evaluated for promotability.
  327. Returns:
  328. True if the given scenario can be promoted to be a primary scenario. False otherwise.
  329. """
  330. return _ScenarioManagerFactory._build_manager()._is_promotable_to_primary(scenario)
  331. def set_primary(scenario: Scenario):
  332. """Promote a scenario as the primary scenario of its cycle.
  333. This function promotes the given scenario as the primary scenario of its associated cycle.
  334. If the cycle already has a primary scenario, that scenario is demoted and is
  335. no longer considered the primary scenario for its cycle.
  336. Parameters:
  337. scenario (Scenario^): The scenario to promote as the new _primary_ scenario.
  338. """
  339. return _ScenarioManagerFactory._build_manager()._set_primary(scenario)
  340. def tag(scenario: Scenario, tag: str):
  341. """Add a tag to a scenario.
  342. This function adds a user-defined tag to the specified scenario. If another scenario
  343. within the same cycle already has the same tag applied, the previous scenario is untagged.
  344. Parameters:
  345. scenario (Scenario^): The scenario to which the tag will be added.
  346. tag (str): The tag to apply to the scenario.
  347. """
  348. return _ScenarioManagerFactory._build_manager()._tag(scenario, tag)
  349. def untag(scenario: Scenario, tag: str):
  350. """Remove a tag from a scenario.
  351. This function removes a specified tag from the given scenario. If the scenario does
  352. not have the specified tag, it has no effect.
  353. Parameters:
  354. scenario (Scenario^): The scenario from which the tag will be removed.
  355. tag (str): The tag to remove from the scenario.
  356. """
  357. return _ScenarioManagerFactory._build_manager()._untag(scenario, tag)
  358. def compare_scenarios(*scenarios: Scenario, data_node_config_id: Optional[str] = None) -> Dict[str, Any]:
  359. """Compare the data nodes of several scenarios.
  360. You can specify which data node config identifier should the comparison be performed
  361. on.
  362. Parameters:
  363. *scenarios (*Scenario^): The list of the scenarios to compare.
  364. data_node_config_id (Optional[str]): The config identifier of the DataNode to perform
  365. the comparison on. <br/>
  366. If _data_node_config_id_ is not provided, the scenarios are
  367. compared on all defined comparators.<br/>
  368. Returns:
  369. The comparison results. The key is the data node config identifier used for
  370. comparison.
  371. Raises:
  372. InsufficientScenarioToCompare^: Raised when only one or no scenario for comparison
  373. is provided.
  374. NonExistingComparator^: Raised when the scenario comparator does not exist.
  375. DifferentScenarioConfigs^: Raised when the provided scenarios do not share the
  376. same scenario config.
  377. NonExistingScenarioConfig^: Raised when the scenario config of the provided
  378. scenarios could not be found.
  379. """
  380. return _ScenarioManagerFactory._build_manager()._compare(*scenarios, data_node_config_id=data_node_config_id)
  381. def subscribe_scenario(
  382. callback: Callable[[Scenario, Job], None],
  383. params: Optional[List[Any]] = None,
  384. scenario: Optional[Scenario] = None,
  385. ):
  386. """Subscribe a function to be called on job status change.
  387. The subscription is applied to all jobs created for the execution of _scenario_.
  388. If no scenario is provided, the subscription applies to all scenarios.
  389. Parameters:
  390. callback (Callable[[Scenario^, Job^], None]): The function to be called on
  391. status change.
  392. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  393. scenario (Optional[Scenario^]): The scenario to which the callback is applied.
  394. If None, the subscription is registered for all scenarios.
  395. Note:
  396. Notifications are applied only for jobs created **after** this subscription.
  397. """
  398. params = [] if params is None else params
  399. return _ScenarioManagerFactory._build_manager()._subscribe(callback, params, scenario)
  400. def unsubscribe_scenario(
  401. callback: Callable[[Scenario, Job], None], params: Optional[List[Any]] = None, scenario: Optional[Scenario] = None
  402. ):
  403. """Unsubscribe a function that is called when the status of a `Job^` changes.
  404. If no scenario is provided, the subscription is removed for all scenarios.
  405. Parameters:
  406. callback (Callable[[Scenario^, Job^], None]): The function to unsubscribe from.
  407. params (Optional[List[Any]]): The parameters to be passed to the callback.
  408. scenario (Optional[Scenario]): The scenario to unsubscribe from. If None, it
  409. applies to all scenarios.
  410. Note:
  411. The callback function will continue to be called for ongoing jobs.
  412. """
  413. return _ScenarioManagerFactory._build_manager()._unsubscribe(callback, params, scenario)
  414. def subscribe_sequence(
  415. callback: Callable[[Sequence, Job], None], params: Optional[List[Any]] = None, sequence: Optional[Sequence] = None
  416. ):
  417. """Subscribe a function to be called on job status change.
  418. The subscription is applied to all jobs created for the execution of _sequence_.
  419. Parameters:
  420. callback (Callable[[Sequence^, Job^], None]): The callable function to be called on
  421. status change.
  422. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  423. sequence (Optional[Sequence^]): The sequence to subscribe on. If None, the subscription
  424. is applied to all sequences.
  425. Note:
  426. Notifications are applied only for jobs created **after** this subscription.
  427. """
  428. return _SequenceManagerFactory._build_manager()._subscribe(callback, params, sequence)
  429. def unsubscribe_sequence(
  430. callback: Callable[[Sequence, Job], None], params: Optional[List[Any]] = None, sequence: Optional[Sequence] = None
  431. ):
  432. """Unsubscribe a function that is called when the status of a Job changes.
  433. Parameters:
  434. callback (Callable[[Sequence^, Job^], None]): The callable function to be called on
  435. status change.
  436. params (Optional[List[Any]]): The parameters to be passed to the _callback_.
  437. sequence (Optional[Sequence^]): The sequence to unsubscribe to. If None, it applies
  438. to all sequences.
  439. Note:
  440. The function will continue to be called for ongoing jobs.
  441. """
  442. return _SequenceManagerFactory._build_manager()._unsubscribe(callback, params, sequence)
  443. def get_sequences() -> List[Sequence]:
  444. """Return all existing sequences.
  445. Returns:
  446. The list of all sequences.
  447. """
  448. return _SequenceManagerFactory._build_manager()._get_all()
  449. def get_jobs() -> List[Job]:
  450. """Return all the existing jobs.
  451. Returns:
  452. The list of all jobs.
  453. """
  454. return _JobManagerFactory._build_manager()._get_all()
  455. def delete_job(job: Job, force: Optional[bool] = False):
  456. """Delete a job.
  457. This function deletes the specified job. If the job is not completed and
  458. *force* is not set to True, a `JobNotDeletedException^` may be raised.
  459. Parameters:
  460. job (Job^): The job to delete.
  461. force (Optional[bool]): If True, forces the deletion of _job_, even
  462. if it is not completed yet.
  463. Raises:
  464. JobNotDeletedException^: If the job is not finished.
  465. """
  466. return _JobManagerFactory._build_manager()._delete(job, force)
  467. def delete_jobs():
  468. """Delete all jobs."""
  469. return _JobManagerFactory._build_manager()._delete_all()
  470. def cancel_job(job: Union[str, Job]):
  471. """Cancel a job and set the status of the subsequent jobs to ABANDONED.
  472. This function cancels the specified job and sets the status of any subsequent jobs to ABANDONED.
  473. Parameters:
  474. job (Job^): The job to cancel.
  475. """
  476. _JobManagerFactory._build_manager()._cancel(job)
  477. def get_latest_job(task: Task) -> Optional[Job]:
  478. """Return the latest job of a task.
  479. This function retrieves the latest job associated with a task.
  480. Parameters:
  481. task (Task^): The task to retrieve the latest job from.
  482. Returns:
  483. The latest job created from _task_, or None if no job has been created from _task_.
  484. """
  485. return _JobManagerFactory._build_manager()._get_latest(task)
  486. def get_latest_submission(entity: Union[Scenario, Sequence, Task]) -> Optional[Submission]:
  487. """Return the latest submission of a scenario, sequence or task.
  488. This function retrieves the latest submission associated with a scenario, sequence or task.
  489. Parameters:
  490. entity (Union[Scenario^, Sequence^, Task^]): The scenario, sequence or task to
  491. retrieve the latest submission from.
  492. Returns:
  493. The latest submission created from _scenario_, _sequence_ and _task_, or None
  494. if no submission has been created from _scenario_, _sequence_ and _task_.
  495. """
  496. return _SubmissionManagerFactory._build_manager()._get_latest(entity)
  497. def get_data_nodes() -> List[DataNode]:
  498. """Return all the existing data nodes.
  499. Returns:
  500. The list of all data nodes.
  501. """
  502. return _DataManagerFactory._build_manager()._get_all()
  503. def get_cycles() -> List[Cycle]:
  504. """Return the list of all existing cycles.
  505. Returns:
  506. The list of all cycles.
  507. """
  508. return _CycleManagerFactory._build_manager()._get_all()
  509. def create_scenario(
  510. config: ScenarioConfig,
  511. creation_date: Optional[datetime] = None,
  512. name: Optional[str] = None,
  513. ) -> Scenario:
  514. """Create and return a new scenario based on a scenario configuration.
  515. If the scenario belongs to a cycle, a cycle (corresponding to the _creation_date_
  516. and the configuration frequency attribute) is created if it does not exist yet.
  517. Parameters:
  518. config (ScenarioConfig^): The scenario configuration used to create a new scenario.
  519. creation_date (Optional[datetime.datetime]): The creation date of the scenario.
  520. If None, the current date time is used.
  521. name (Optional[str]): The displayable name of the scenario.
  522. Returns:
  523. The new scenario.
  524. """
  525. return _ScenarioManagerFactory._build_manager()._create(config, creation_date, name)
  526. def create_global_data_node(config: DataNodeConfig) -> DataNode:
  527. """Create and return a new GLOBAL data node from a data node configuration.
  528. Parameters:
  529. config (DataNodeConfig^): The data node configuration. It must have a `GLOBAL` scope.
  530. Returns:
  531. The new global data node.
  532. Raises:
  533. DataNodeConfigIsNotGlobal^: If the data node configuration does not have GLOBAL scope.
  534. """
  535. # Check if the data node config has GLOBAL scope
  536. if config.scope is not Scope.GLOBAL:
  537. raise DataNodeConfigIsNotGlobal(config.id) # type: ignore
  538. if dns := _get_manager(DataNode._MANAGER_NAME)._get_by_config_id(config.id): # type: ignore
  539. return dns[0]
  540. return _get_manager(DataNode._MANAGER_NAME)._create_and_set(config, None, None) # type: ignore
  541. def clean_all_entities_by_version(version_number=None) -> bool:
  542. """Delete all entities of a specific version.
  543. This function deletes all entities associated with the specified version.
  544. Parameters:
  545. version_number (optional[str]): The version number of the entities to be deleted.
  546. If None, the default behavior may apply.
  547. Returns:
  548. True if the operation succeeded, False otherwise.
  549. Notes:
  550. - If the specified version does not exist, the operation will be aborted, and False will be returned.
  551. - This function cleans all entities, including jobs, scenarios, sequences, tasks, and data nodes.
  552. - The production version of the specified version is also deleted if it exists.
  553. """
  554. version_manager = _VersionManagerFactory._build_manager()
  555. try:
  556. version_number = version_manager._replace_version_number(version_number)
  557. except NonExistingVersion as e:
  558. __logger.warning(f"{e.message} Abort cleaning the entities of version '{version_number}'.")
  559. return False
  560. _get_manager(Job._MANAGER_NAME)._delete_by_version(version_number)
  561. _get_manager(Scenario._MANAGER_NAME)._delete_by_version(version_number)
  562. _get_manager(Sequence._MANAGER_NAME)._delete_by_version(version_number)
  563. _get_manager(Task._MANAGER_NAME)._delete_by_version(version_number)
  564. _get_manager(DataNode._MANAGER_NAME)._delete_by_version(version_number)
  565. _get_manager(Submission._MANAGER_NAME)._delete_by_version(version_number)
  566. version_manager._delete(version_number)
  567. try:
  568. version_manager._delete_production_version(version_number)
  569. except VersionIsNotProductionVersion:
  570. pass
  571. return True
  572. def export_scenario(
  573. scenario_id: ScenarioId,
  574. folder_path: Union[str, pathlib.Path],
  575. ):
  576. """Export all related entities of a scenario to a folder.
  577. This function exports all related entities of the specified scenario to the
  578. specified folder.
  579. Parameters:
  580. scenario_id (ScenarioId): The ID of the scenario to export.
  581. folder_path (Union[str, pathlib.Path]): The folder path to export the scenario to.
  582. """
  583. manager = _ScenarioManagerFactory._build_manager()
  584. scenario = manager._get(scenario_id)
  585. entity_ids = manager._get_children_entity_ids(scenario) # type: ignore
  586. entity_ids.scenario_ids = {scenario_id}
  587. entity_ids.cycle_ids = {scenario.cycle.id}
  588. shutil.rmtree(folder_path, ignore_errors=True)
  589. for data_node_id in entity_ids.data_node_ids:
  590. _get_manager(DataNode._MANAGER_NAME)._export(data_node_id, folder_path)
  591. for task_id in entity_ids.task_ids:
  592. _get_manager(Task._MANAGER_NAME)._export(task_id, folder_path)
  593. for sequence_id in entity_ids.sequence_ids:
  594. _get_manager(Sequence._MANAGER_NAME)._export(sequence_id, folder_path)
  595. for cycle_id in entity_ids.cycle_ids:
  596. _get_manager(Cycle._MANAGER_NAME)._export(cycle_id, folder_path)
  597. for scenario_id in entity_ids.scenario_ids:
  598. _get_manager(Scenario._MANAGER_NAME)._export(scenario_id, folder_path)
  599. for job_id in entity_ids.job_ids:
  600. _get_manager(Job._MANAGER_NAME)._export(job_id, folder_path)
  601. def get_parents(
  602. entity: Union[TaskId, DataNodeId, SequenceId, Task, DataNode, Sequence], parent_dict=None
  603. ) -> Dict[str, Set[_Entity]]:
  604. """Get the parents of an entity from itself or its identifier.
  605. Parameters:
  606. entity (Union[TaskId, DataNodeId, SequenceId, Task, DataNode, Sequence]): The entity or its
  607. identifier to get the parents.
  608. Returns:
  609. The dictionary of all parent entities.
  610. They are grouped by their type (Scenario^, Sequences^, or tasks^) so each key corresponds
  611. to a level of the parents and the value is a set of the parent entities.
  612. An empty dictionary is returned if the entity does not have parents.<br/>
  613. Example: The following instruction returns all the scenarios that include the
  614. datanode identified by "my_datanode_id".
  615. `taipy.get_parents("id_of_my_datanode")["scenario"]`
  616. Raises:
  617. ModelNotFound^: If _entity_ does not match a correct entity pattern.
  618. """
  619. def update_parent_dict(parents_set, parent_dict):
  620. for k, value in parents_set.items():
  621. if k in parent_dict.keys():
  622. parent_dict[k].update(value)
  623. else:
  624. parent_dict[k] = value
  625. if isinstance(entity, str):
  626. entity = get(entity) # type: ignore
  627. parent_dict = parent_dict or dict()
  628. if isinstance(entity, (Scenario, Cycle)):
  629. return parent_dict
  630. current_parent_dict: Dict[str, Set] = {}
  631. for parent in entity.parent_ids:
  632. parent_entity = get(parent)
  633. if parent_entity._MANAGER_NAME in current_parent_dict.keys():
  634. current_parent_dict[parent_entity._MANAGER_NAME].add(parent_entity)
  635. else:
  636. current_parent_dict[parent_entity._MANAGER_NAME] = {parent_entity}
  637. if isinstance(entity, Sequence):
  638. update_parent_dict(current_parent_dict, parent_dict)
  639. if isinstance(entity, Task):
  640. parent_entity_key_to_search_next = "scenario"
  641. update_parent_dict(current_parent_dict, parent_dict)
  642. for parent in parent_dict.get(parent_entity_key_to_search_next, []):
  643. get_parents(parent, parent_dict)
  644. if isinstance(entity, DataNode):
  645. parent_entity_key_to_search_next = "task"
  646. update_parent_dict(current_parent_dict, parent_dict)
  647. for parent in parent_dict.get(parent_entity_key_to_search_next, []):
  648. get_parents(parent, parent_dict)
  649. return parent_dict
  650. def get_cycles_scenarios() -> Dict[Optional[Cycle], List[Scenario]]:
  651. """Get the scenarios grouped by cycles.
  652. Returns:
  653. The dictionary of all cycles and their corresponding scenarios.
  654. """
  655. cycles_scenarios: Dict[Optional[Cycle], List[Scenario]] = {}
  656. for scenario in get_scenarios():
  657. if scenario.cycle in cycles_scenarios.keys():
  658. cycles_scenarios[scenario.cycle].append(scenario)
  659. else:
  660. cycles_scenarios[scenario.cycle] = [scenario]
  661. return cycles_scenarios
  662. def get_entities_by_config_id(
  663. config_id: str,
  664. ) -> Union[List, List[Task], List[DataNode], List[Sequence], List[Scenario]]:
  665. """Get the entities by its config id.
  666. Parameters:
  667. config_id (str): The config id of the entities
  668. Returns:
  669. The list of all entities by the config id.
  670. """
  671. entities: List = []
  672. if entities := _get_manager(Scenario._MANAGER_NAME)._get_by_config_id(config_id): # type: ignore
  673. return entities
  674. if entities := _get_manager(Task._MANAGER_NAME)._get_by_config_id(config_id): # type: ignore
  675. return entities
  676. if entities := _get_manager(DataNode._MANAGER_NAME)._get_by_config_id(config_id): # type: ignore
  677. return entities
  678. return entities