-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added initial event service implementation
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import Any, ClassVar, Dict, List, Type | ||
|
||
from syft.service.dataset.dataset import Asset, Dataset | ||
from syft.store.linked_obj import LinkedObject | ||
from ...types.syft_object import SyftObject | ||
from ...types.uid import UID | ||
from datetime import datetime | ||
from pydantic import Field | ||
|
||
event_handler_registry = {} | ||
|
||
def register_event_handler(event_type): | ||
def inner(method): | ||
event_handler_registry[event_type.__name__] = method.__name__ | ||
return method | ||
|
||
return inner | ||
|
||
class Event(SyftObject): | ||
creator_user: UID | ||
creation_date: datetime = Field(default_factory=lambda: datetime.now()) | ||
|
||
def handler(self, node): | ||
method_name = event_handler_registry[self.__class__.__name__] | ||
return getattr(node, method_name) | ||
|
||
class CRUDEvent(Event): | ||
object_type: ClassVar[Type] = Type | ||
object_id: UID | ||
|
||
@property | ||
def merge_updates_repr(self): | ||
return f"{self.updates} for object {self.object_id} by {self.creator}" | ||
|
||
class CreateObjectEvent(CRUDEvent): | ||
@property | ||
def updated_properties(self): | ||
return list(self.object_type.__annotations__.keys()) | ||
|
||
@property | ||
def updates(self): | ||
return {p: getattr(self, p) for p in self.updated_properties} | ||
|
||
@property | ||
def update_tuples(self): | ||
return list(self.updates.items()) | ||
|
||
|
||
class UpdateObjectEvent(CRUDEvent): | ||
updates: Dict[str, Any] | ||
|
||
@property | ||
def updated_properties(self): | ||
return list(self.updates.keys()) | ||
|
||
@property | ||
def update_tuples(self): | ||
return list(self.updates.items()) | ||
|
||
class CreateDatasetEvent(CRUDEvent): | ||
object_type: ClassVar[Type] = Dataset | ||
|
||
def execute(self, node): | ||
handler = self.handler(node) | ||
handler( | ||
object_id=self.real.obj_id, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from syft.serde.serializable import serializable | ||
from syft.service.context import AuthedServiceContext | ||
from syft.service.event.event_stash import EventStash | ||
from syft.service.response import SyftError, SyftSuccess | ||
from syft.service.service import AbstractService, service_method | ||
from syft.service.user.user_roles import DATA_OWNER_ROLE_LEVEL | ||
from syft.store.document_store import DocumentStore | ||
from syft.types.uid import UID | ||
from syft.util.trace_decorator import instrument | ||
from .event import Event | ||
|
||
@instrument | ||
@serializable() | ||
class EventService(AbstractService): | ||
store: DocumentStore | ||
stash: EventStash | ||
|
||
def __init__(self, store: DocumentStore) -> None: | ||
self.store = store | ||
self.stash = EventStash(store=store) | ||
|
||
@service_method( | ||
path="event.add", | ||
name="add", | ||
roles=DATA_OWNER_ROLE_LEVEL, | ||
) | ||
def add( | ||
self, context: AuthedServiceContext, event: Event, | ||
): | ||
result = self.stash.set(context.credentials, event) | ||
if result.is_err(): | ||
return SyftError(message=str(result.err())) | ||
|
||
return SyftSuccess(message=f'Great Success!') | ||
|
||
|
||
@service_method( | ||
path="event.get_by_uid", | ||
name="get_by_uid", | ||
roles=DATA_OWNER_ROLE_LEVEL, | ||
) | ||
def get_by_uid( | ||
self, context: AuthedServiceContext, uid: UID, | ||
): | ||
result = self.stash.get_by_uid(context.credentials, uid=uid) | ||
if result.is_err(): | ||
return SyftError(message=str(result.err())) | ||
return result.ok() | ||
|
||
|
||
@service_method( | ||
path="event.get_all", | ||
name="get_all", | ||
roles=DATA_OWNER_ROLE_LEVEL, | ||
) | ||
def get_all( | ||
self, context: AuthedServiceContext | ||
): | ||
result = self.stash.get_all(context.credentials) | ||
if result.is_err(): | ||
return SyftError(message=str(result.err())) | ||
|
||
return result.ok() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# stdlib | ||
from typing import List | ||
from typing import Optional | ||
|
||
# third party | ||
from result import Result | ||
|
||
# relative | ||
from ...node.credentials import SyftVerifyKey | ||
from ...serde.serializable import serializable | ||
from ...store.document_store import BaseUIDStoreStash | ||
from ...store.document_store import DocumentStore | ||
from ...store.document_store import PartitionKey | ||
from ...store.document_store import PartitionSettings | ||
from ...store.document_store import QueryKeys | ||
from ...types.uid import UID | ||
from ...util.telemetry import instrument | ||
from .event import Event | ||
|
||
|
||
@instrument | ||
@serializable() | ||
class EventStash(BaseUIDStoreStash): | ||
object_type = Event | ||
settings: PartitionSettings = PartitionSettings( | ||
name=Event.__canonical_name__, object_type=Event | ||
) | ||
|
||
def __init__(self, store: DocumentStore) -> None: | ||
super().__init__(store=store) |