updated event and router stacks
This commit is contained in:
@@ -11,11 +11,11 @@ from config import api_config
|
||||
|
||||
|
||||
def create_events_if_any_cluster_set():
|
||||
import events
|
||||
from events import retrieve_all_clusters
|
||||
|
||||
for event_str in events.__all__:
|
||||
if to_set_events := getattr(events, event_str, None):
|
||||
to_set_events.set_events_to_database()
|
||||
for event_cluster in retrieve_all_clusters():
|
||||
for event in event_cluster.retrieve_all_event_clusters:
|
||||
event.set_events_to_database()
|
||||
|
||||
|
||||
def create_app():
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
from .template.event import template_event_cluster
|
||||
from .template.cluster import (
|
||||
TemplateEventClusterSet
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"template_event_cluster",
|
||||
"TemplateEventClusterSet",
|
||||
]
|
||||
|
||||
def retrieve_all_clusters():
|
||||
return [TemplateEventClusterSet]
|
||||
15
ApiServices/TemplateService/events/template/cluster.py
Normal file
15
ApiServices/TemplateService/events/template/cluster.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from ApiServices.TemplateService.initializer.event_clusters import EventCluster, SetEventCluster
|
||||
|
||||
|
||||
TemplateEventCluster = EventCluster(
|
||||
endpoint_uu_id="bb20c8c6-a289-4cab-9da7-34ca8a36c8e5"
|
||||
)
|
||||
|
||||
OtherTemplateEventCluster = EventCluster(
|
||||
endpoint_uu_id="ecb82b7a-317f-469d-a682-ff431f152453"
|
||||
)
|
||||
|
||||
TemplateEventClusterSet = SetEventCluster()
|
||||
TemplateEventClusterSet.add_event_cluster(TemplateEventCluster)
|
||||
TemplateEventClusterSet.add_event_cluster(OtherTemplateEventCluster)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
from ApiServices.TemplateService.initializer.event_clusters import EventCluster, Event
|
||||
|
||||
from ApiServices.TemplateService.initializer.event_clusters import Event
|
||||
from .cluster import (
|
||||
template_event_cluster,
|
||||
other_template_event_cluster,
|
||||
)
|
||||
|
||||
single_event = Event(
|
||||
name="example_event",
|
||||
@@ -25,10 +28,11 @@ def example_callable():
|
||||
|
||||
|
||||
single_event.event_callable = example_callable
|
||||
template_event_cluster.add_event([single_event])
|
||||
|
||||
other_event = Event(
|
||||
name="example_event-2",
|
||||
key="176b829c-7622-4cf2-b474-421e5acb637c",
|
||||
key="36b26d7c-2a9e-4006-a213-f54bc66e5455",
|
||||
request_validator=None, # TODO: Add request validator
|
||||
response_validator=None, # TODO: Add response validator
|
||||
description="Example event 2 description",
|
||||
@@ -50,17 +54,4 @@ def example_callable_other():
|
||||
|
||||
|
||||
other_event.event_callable = example_callable_other
|
||||
|
||||
tokens_in_redis = [
|
||||
"3f510dcf-9f84-4eb9-b919-f582f30adab1",
|
||||
"9f403034-deba-4e1f-b43e-b25d3c808d39",
|
||||
"b8ec6e64-286a-4f60-8554-7a3865454944",
|
||||
"176b829c-7622-4cf2-b474-421e5acb637c",
|
||||
]
|
||||
template_event_cluster = EventCluster(
|
||||
endpoint_uu_id="bb20c8c6-a289-4cab-9da7-34ca8a36c8e5"
|
||||
)
|
||||
template_event_cluster.add_event([single_event, other_event])
|
||||
# matched_event = template_event_cluster.match_event(event_keys=tokens_in_redis)
|
||||
|
||||
# print('event_callable', matched_event.event_callable())
|
||||
other_template_event_cluster.add_event([other_event])
|
||||
@@ -1,5 +1,11 @@
|
||||
class EventCluster:
|
||||
from typing import Optional, Type
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class EventCluster:
|
||||
"""
|
||||
EventCluster
|
||||
"""
|
||||
def __init__(self, endpoint_uu_id: str):
|
||||
self.endpoint_uu_id = endpoint_uu_id
|
||||
self.events = []
|
||||
@@ -48,20 +54,18 @@ class EventCluster:
|
||||
f"UUID: {event_to_save_database.uu_id} event is saved to {to_save_endpoint.uu_id}"
|
||||
)
|
||||
|
||||
def match_event(self, event_keys: list[str]) -> "Event":
|
||||
def match_event(self, event_key: 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])
|
||||
# intersection_of_key: set[str] = set(event_key) & 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=event_key)
|
||||
|
||||
|
||||
class Event:
|
||||
@@ -70,8 +74,8 @@ class Event:
|
||||
self,
|
||||
name: str,
|
||||
key: str,
|
||||
request_validator: str = None,
|
||||
response_validator: str = None,
|
||||
request_validator: Optional[Type[BaseModel]] = None,
|
||||
response_validator: Optional[Type[BaseModel]] = None,
|
||||
description: str = "",
|
||||
):
|
||||
self.name = name
|
||||
@@ -86,3 +90,25 @@ class Event:
|
||||
"""
|
||||
print(self.name)
|
||||
return {}
|
||||
|
||||
|
||||
class SetEventCluster:
|
||||
"""
|
||||
SetEventCluster
|
||||
"""
|
||||
list_of_event_clusters: list[EventCluster] = []
|
||||
|
||||
def add_event_cluster(self, event_cluster: EventCluster):
|
||||
"""
|
||||
Add an event cluster to the set
|
||||
"""
|
||||
endpoint_uu_id_list = [event_cluster_uuid.endpoint_uu_id for event_cluster_uuid in self.list_of_event_clusters]
|
||||
if event_cluster.endpoint_uu_id not in endpoint_uu_id_list:
|
||||
self.list_of_event_clusters.append(event_cluster)
|
||||
|
||||
@property
|
||||
def retrieve_all_event_clusters(self) -> list[EventCluster]:
|
||||
"""
|
||||
Retrieve all event clusters
|
||||
"""
|
||||
return self.list_of_event_clusters
|
||||
|
||||
Reference in New Issue
Block a user