test_events_published.py 16 KB

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