test_events_published.py 16 KB

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