updated events initializer
This commit is contained in:
84
ApiServices/TemplateService/initializer/event_clusters.py
Normal file
84
ApiServices/TemplateService/initializer/event_clusters.py
Normal file
@@ -0,0 +1,84 @@
|
||||
|
||||
class EventCluster:
|
||||
|
||||
def __init__(self, endpoint_uu_id: str):
|
||||
self.endpoint_uu_id = endpoint_uu_id
|
||||
self.events = []
|
||||
|
||||
def add_event(self, list_of_events: list["Event"]):
|
||||
"""
|
||||
Add an event to the cluster
|
||||
"""
|
||||
for event in list_of_events:
|
||||
self.events.append(event)
|
||||
self.events = list(set(self.events))
|
||||
|
||||
def get_event(self, event_key: str):
|
||||
"""
|
||||
Get an event by its key
|
||||
"""
|
||||
|
||||
for event in self.events:
|
||||
if event.key == event_key:
|
||||
return event
|
||||
return None
|
||||
|
||||
def set_events_to_database(self):
|
||||
from Schemas import Events, EndpointRestriction
|
||||
with Events.new_session() as db_session:
|
||||
|
||||
if to_save_endpoint := EndpointRestriction.filter_one(
|
||||
EndpointRestriction.uu_id == self.endpoint_uu_id,
|
||||
db=db_session,
|
||||
).data:
|
||||
for event in self.events:
|
||||
event_obj = Events.find_or_create(
|
||||
function_code=event.key,
|
||||
function_class=event.name,
|
||||
description=event.description,
|
||||
endpoint_id=to_save_endpoint.id,
|
||||
endpoint_uu_id=str(to_save_endpoint.uu_id),
|
||||
is_confirmed=True,
|
||||
active=True,
|
||||
db=db_session,
|
||||
)
|
||||
event_obj.save()
|
||||
print(f'UUID: {event_obj.uu_id} event is saved to {to_save_endpoint.uu_id}')
|
||||
|
||||
def match_event(self, event_keys: list[str]) -> "Event":
|
||||
"""
|
||||
Match an event by its key
|
||||
"""
|
||||
print('set(event_keys)', set(event_keys))
|
||||
print('event.keys', set([event.key for event in self.events]))
|
||||
intersection_of_key: set[str] = set(event_keys) & set([event.key for event in self.events])
|
||||
if not len(intersection_of_key) == 1:
|
||||
raise ValueError(
|
||||
f"Event key not found or multiple matches found: {intersection_of_key}"
|
||||
)
|
||||
return self.get_event(event_key=list(intersection_of_key)[0])
|
||||
|
||||
|
||||
class Event:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
key: str,
|
||||
request_validator: str = None,
|
||||
response_validator: str = None,
|
||||
description: str = "",
|
||||
):
|
||||
self.name = name
|
||||
self.key = key
|
||||
self.request_validator = request_validator
|
||||
self.response_validator = response_validator
|
||||
self.description = description
|
||||
|
||||
|
||||
def event_callable(self):
|
||||
"""
|
||||
Example callable method
|
||||
"""
|
||||
print(self.name)
|
||||
return {}
|
||||
Reference in New Issue
Block a user