test_events_published.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 queue import SimpleQueue
  12. from typing import Dict, List
  13. from taipy.config import Config, Frequency
  14. from taipy.core import taipy as tp
  15. from taipy.core.job.status import Status
  16. from taipy.core.notification.core_event_consumer import CoreEventConsumerBase
  17. from taipy.core.notification.event import Event, EventEntityType, EventOperation
  18. from taipy.core.notification.notifier import Notifier
  19. from taipy.core.scenario._scenario_manager_factory import _ScenarioManagerFactory
  20. class Snapshot:
  21. """
  22. A captured snapshot of the recording core events consumer.
  23. """
  24. def __init__(self) -> None:
  25. self.collected_events: List[Event] = []
  26. self.entity_type_collected: Dict[EventEntityType, int] = {}
  27. self.operation_collected: Dict[EventEntityType, int] = {}
  28. self.attr_name_collected: Dict[EventEntityType, int] = {}
  29. def capture_event(self, event):
  30. self.collected_events.append(event)
  31. self.entity_type_collected[event.entity_type] = self.entity_type_collected.get(event.entity_type, 0) + 1
  32. self.operation_collected[event.operation] = self.operation_collected.get(event.operation, 0) + 1
  33. if event.attribute_name:
  34. self.attr_name_collected[event.attribute_name] = self.attr_name_collected.get(event.attribute_name, 0) + 1
  35. class RecordingConsumer(CoreEventConsumerBase):
  36. """
  37. A straightforward and no-thread core events consumer that allows to
  38. capture snapshots of received events.
  39. """
  40. def __init__(self, registration_id: str, queue: SimpleQueue):
  41. super().__init__(registration_id, queue)
  42. def capture(self) -> Snapshot:
  43. """
  44. Capture a snapshot of events received between the previous snapshot
  45. (or from the start of this consumer).
  46. """
  47. snapshot = Snapshot()
  48. while not self.queue.empty():
  49. event = self.queue.get()
  50. snapshot.capture_event(event)
  51. return snapshot
  52. def process_event(self, event: Event):
  53. # Nothing to do
  54. pass
  55. def start(self):
  56. # Nothing to do here
  57. pass
  58. def stop(self):
  59. # Nothing to do here either
  60. pass
  61. def identity(x):
  62. return x
  63. def test_events_published_for_scenario_creation():
  64. input_config = Config.configure_data_node("the_input")
  65. output_config = Config.configure_data_node("the_output")
  66. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  67. sc_config = Config.configure_scenario(
  68. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  69. )
  70. register_id_0, register_queue_0 = Notifier.register()
  71. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  72. all_evts.start()
  73. # Create a scenario via the manager
  74. # should only trigger 6 creation events (for cycle, data node(x2), task, sequence and scenario)
  75. _ScenarioManagerFactory._build_manager()._create(sc_config)
  76. snapshot = all_evts.capture()
  77. assert len(snapshot.collected_events) == 6
  78. assert snapshot.entity_type_collected.get(EventEntityType.CYCLE, 0) == 1
  79. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 2
  80. assert snapshot.entity_type_collected.get(EventEntityType.TASK, 0) == 1
  81. assert snapshot.entity_type_collected.get(EventEntityType.SEQUENCE, 0) == 1
  82. assert snapshot.entity_type_collected.get(EventEntityType.SCENARIO, 0) == 1
  83. assert snapshot.operation_collected.get(EventOperation.CREATION, 0) == 6
  84. all_evts.stop()
  85. def test_no_event_published_for_getting_scenario():
  86. input_config = Config.configure_data_node("the_input")
  87. output_config = Config.configure_data_node("the_output")
  88. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  89. sc_config = Config.configure_scenario(
  90. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  91. )
  92. scenario = tp.create_scenario(sc_config)
  93. register_id_0, register_queue_0 = Notifier.register()
  94. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  95. all_evts.start()
  96. # Get all scenarios does not trigger any event
  97. tp.get_scenarios()
  98. snapshot = all_evts.capture()
  99. assert len(snapshot.collected_events) == 0
  100. # Get one scenario does not trigger any event
  101. tp.get(scenario.id)
  102. snapshot = all_evts.capture()
  103. assert len(snapshot.collected_events) == 0
  104. all_evts.stop()
  105. def test_events_published_for_writing_dn():
  106. input_config = Config.configure_data_node("the_input")
  107. output_config = Config.configure_data_node("the_output")
  108. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  109. sc_config = Config.configure_scenario(
  110. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  111. )
  112. scenario = tp.create_scenario(sc_config)
  113. register_id_0, register_queue_0 = Notifier.register()
  114. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  115. all_evts.start()
  116. # Write input manually trigger 4 data node update events
  117. # for last_edit_date, editor_id, editor_expiration_date and edit_in_progress
  118. scenario.the_input.write("test")
  119. snapshot = all_evts.capture()
  120. assert len(snapshot.collected_events) == 4
  121. assert snapshot.entity_type_collected.get(EventEntityType.CYCLE, 0) == 0
  122. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 4
  123. assert snapshot.entity_type_collected.get(EventEntityType.TASK, 0) == 0
  124. assert snapshot.entity_type_collected.get(EventEntityType.SEQUENCE, 0) == 0
  125. assert snapshot.entity_type_collected.get(EventEntityType.SCENARIO, 0) == 0
  126. assert snapshot.operation_collected.get(EventOperation.CREATION, 0) == 0
  127. assert snapshot.operation_collected.get(EventOperation.UPDATE, 0) == 4
  128. all_evts.stop()
  129. def test_events_published_for_scenario_submission():
  130. input_config = Config.configure_pickle_data_node("the_input")
  131. output_config = Config.configure_pickle_data_node("the_output")
  132. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  133. sc_config = Config.configure_scenario(
  134. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  135. )
  136. scenario = tp.create_scenario(sc_config)
  137. scenario.the_input.write("test")
  138. register_id_0, register_queue_0 = Notifier.register()
  139. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  140. all_evts.start()
  141. # Before and after writing value to the unwritten data node trigger:
  142. # 3 is_submittable update events for the scenario, sequence and task being not submittable
  143. # 3 is_submittable update events for the scenario, sequence and task being submittable
  144. # Submit a scenario triggers:
  145. # 1 scenario submission event
  146. # 7 dn update events (for last_edit_date, editor_id(x2), editor_expiration_date(x2) and edit_in_progress(x2))
  147. # 1 job creation event
  148. # 3 job update events (for status: PENDING, RUNNING and COMPLETED)
  149. # 1 submission creation event
  150. # 1 submission update event for jobs
  151. # 3 submission update events (for status: PENDING, RUNNING and COMPLETED)
  152. # 1 submission update event for is_completed
  153. scenario.submit()
  154. snapshot = all_evts.capture()
  155. assert len(snapshot.collected_events) == 23
  156. assert snapshot.entity_type_collected.get(EventEntityType.CYCLE, 0) == 0
  157. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 7
  158. assert snapshot.entity_type_collected.get(EventEntityType.TASK, 0) == 2
  159. assert snapshot.entity_type_collected.get(EventEntityType.SEQUENCE, 0) == 2
  160. assert snapshot.entity_type_collected.get(EventEntityType.SCENARIO, 0) == 3
  161. assert snapshot.entity_type_collected.get(EventEntityType.JOB, 0) == 4
  162. assert snapshot.entity_type_collected.get(EventEntityType.SUBMISSION, 0) == 5
  163. assert snapshot.operation_collected.get(EventOperation.CREATION, 0) == 2
  164. assert snapshot.operation_collected.get(EventOperation.UPDATE, 0) == 20
  165. assert snapshot.operation_collected.get(EventOperation.SUBMISSION, 0) == 1
  166. assert snapshot.attr_name_collected["last_edit_date"] == 1
  167. assert snapshot.attr_name_collected["editor_id"] == 2
  168. assert snapshot.attr_name_collected["editor_expiration_date"] == 2
  169. assert snapshot.attr_name_collected["edit_in_progress"] == 2
  170. assert snapshot.attr_name_collected["status"] == 3
  171. assert snapshot.attr_name_collected["jobs"] == 1
  172. assert snapshot.attr_name_collected["submission_status"] == 3
  173. assert snapshot.attr_name_collected["is_submittable"] == 6
  174. all_evts.stop()
  175. def test_events_published_for_scenario_deletion():
  176. input_config = Config.configure_data_node("the_input")
  177. output_config = Config.configure_data_node("the_output")
  178. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  179. sc_config = Config.configure_scenario(
  180. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  181. )
  182. scenario = tp.create_scenario(sc_config)
  183. scenario.the_input.write("test")
  184. scenario.submit()
  185. register_id_0, register_queue_0 = Notifier.register()
  186. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  187. all_evts.start()
  188. # Delete a scenario trigger 8 deletion events
  189. # 1 scenario deletion event
  190. # 1 cycle deletion event
  191. # 2 dn deletion events (for input and output)
  192. # 1 task deletion event
  193. # 1 sequence deletion event
  194. # 1 job deletion event
  195. # 1 submission deletion event
  196. tp.delete(scenario.id)
  197. snapshot = all_evts.capture()
  198. assert len(snapshot.collected_events) == 8
  199. assert snapshot.entity_type_collected.get(EventEntityType.CYCLE, 0) == 1
  200. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 2
  201. assert snapshot.entity_type_collected.get(EventEntityType.TASK, 0) == 1
  202. assert snapshot.entity_type_collected.get(EventEntityType.SEQUENCE, 0) == 1
  203. assert snapshot.entity_type_collected.get(EventEntityType.SCENARIO, 0) == 1
  204. assert snapshot.entity_type_collected.get(EventEntityType.SUBMISSION, 0) == 1
  205. assert snapshot.entity_type_collected.get(EventEntityType.JOB, 0) == 1
  206. assert snapshot.operation_collected.get(EventOperation.UPDATE, 0) == 0
  207. assert snapshot.operation_collected.get(EventOperation.SUBMISSION, 0) == 0
  208. assert snapshot.operation_collected.get(EventOperation.DELETION, 0) == 8
  209. all_evts.stop()
  210. def test_job_events():
  211. input_config = Config.configure_data_node("the_input")
  212. output_config = Config.configure_data_node("the_output")
  213. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  214. sc_config = Config.configure_scenario(
  215. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  216. )
  217. register_id, register_queue = Notifier.register(entity_type=EventEntityType.JOB)
  218. consumer = RecordingConsumer(register_id, register_queue)
  219. consumer.start()
  220. # Create scenario
  221. scenario = _ScenarioManagerFactory._build_manager()._create(sc_config)
  222. snapshot = consumer.capture()
  223. assert len(snapshot.collected_events) == 0
  224. # Submit scenario
  225. scenario.submit()
  226. snapshot = consumer.capture()
  227. # 2 events expected: one for creation, another for status update
  228. assert len(snapshot.collected_events) == 2
  229. assert snapshot.collected_events[0].operation == EventOperation.CREATION
  230. assert snapshot.collected_events[0].entity_type == EventEntityType.JOB
  231. assert snapshot.collected_events[0].metadata.get("task_config_id") == task_config.id
  232. assert snapshot.collected_events[1].operation == EventOperation.UPDATE
  233. assert snapshot.collected_events[1].entity_type == EventEntityType.JOB
  234. assert snapshot.collected_events[1].metadata.get("task_config_id") == task_config.id
  235. assert snapshot.collected_events[1].attribute_name == "status"
  236. assert snapshot.collected_events[1].attribute_value == Status.BLOCKED
  237. job = tp.get_jobs()[0]
  238. tp.cancel_job(job)
  239. snapshot = consumer.capture()
  240. assert len(snapshot.collected_events) == 1
  241. event = snapshot.collected_events[0]
  242. assert event.metadata.get("task_config_id") == task_config.id
  243. assert event.attribute_name == "status"
  244. assert event.attribute_value == Status.CANCELED
  245. consumer.stop()
  246. def test_scenario_events():
  247. input_config = Config.configure_data_node("the_input")
  248. output_config = Config.configure_data_node("the_output")
  249. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  250. sc_config = Config.configure_scenario(
  251. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  252. )
  253. register_id, register_queue = Notifier.register(entity_type=EventEntityType.SCENARIO)
  254. consumer = RecordingConsumer(register_id, register_queue)
  255. consumer.start()
  256. scenario = tp.create_scenario(sc_config)
  257. snapshot = consumer.capture()
  258. assert len(snapshot.collected_events) == 1
  259. assert snapshot.collected_events[0].operation == EventOperation.CREATION
  260. assert snapshot.collected_events[0].entity_type == EventEntityType.SCENARIO
  261. assert snapshot.collected_events[0].metadata.get("config_id") == scenario.config_id
  262. scenario.submit()
  263. snapshot = consumer.capture()
  264. assert len(snapshot.collected_events) == 2
  265. assert snapshot.collected_events[0].operation == EventOperation.UPDATE
  266. assert snapshot.collected_events[0].entity_type == EventEntityType.SCENARIO
  267. assert snapshot.collected_events[0].metadata.get("config_id") == scenario.config_id
  268. assert snapshot.collected_events[1].operation == EventOperation.SUBMISSION
  269. assert snapshot.collected_events[1].entity_type == EventEntityType.SCENARIO
  270. assert snapshot.collected_events[1].metadata.get("config_id") == scenario.config_id
  271. # Delete scenario
  272. tp.delete(scenario.id)
  273. snapshot = consumer.capture()
  274. assert len(snapshot.collected_events) == 1
  275. assert snapshot.collected_events[0].operation == EventOperation.DELETION
  276. assert snapshot.collected_events[0].entity_type == EventEntityType.SCENARIO
  277. consumer.stop()
  278. def test_data_node_events():
  279. input_config = Config.configure_data_node("the_input")
  280. output_config = Config.configure_data_node("the_output")
  281. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  282. sc_config = Config.configure_scenario(
  283. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  284. )
  285. register_id, register_queue = Notifier.register(entity_type=EventEntityType.DATA_NODE)
  286. consumer = RecordingConsumer(register_id, register_queue)
  287. consumer.start()
  288. scenario = _ScenarioManagerFactory._build_manager()._create(sc_config)
  289. snapshot = consumer.capture()
  290. # We expect two creation events since we have two data nodes:
  291. assert len(snapshot.collected_events) == 2
  292. assert snapshot.collected_events[0].operation == EventOperation.CREATION
  293. assert snapshot.collected_events[0].entity_type == EventEntityType.DATA_NODE
  294. assert snapshot.collected_events[0].metadata.get("config_id") in [output_config.id, input_config.id]
  295. assert snapshot.collected_events[1].operation == EventOperation.CREATION
  296. assert snapshot.collected_events[1].entity_type == EventEntityType.DATA_NODE
  297. assert snapshot.collected_events[1].metadata.get("config_id") in [output_config.id, input_config.id]
  298. # Delete scenario
  299. tp.delete(scenario.id)
  300. snapshot = consumer.capture()
  301. assert len(snapshot.collected_events) == 2
  302. assert snapshot.collected_events[0].operation == EventOperation.DELETION
  303. assert snapshot.collected_events[0].entity_type == EventEntityType.DATA_NODE
  304. assert snapshot.collected_events[1].operation == EventOperation.DELETION
  305. assert snapshot.collected_events[1].entity_type == EventEntityType.DATA_NODE
  306. consumer.stop()