Skip to content

Commit

Permalink
fix createdatasetevent
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-milea committed Feb 7, 2024
1 parent 6dec214 commit 660dbb2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/syft/src/syft/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from result import Result
from typing_extensions import Self

from ..service.event.event_service import EventService

# relative
from .. import __version__
from ..abstract_node import AbstractNode
Expand Down Expand Up @@ -319,6 +321,7 @@ def __init__(
SyftWorkerImageService,
SyftWorkerPoolService,
SyftImageRegistryService,
EventService,
]
if services is None
else services
Expand Down Expand Up @@ -877,6 +880,7 @@ def _construct_services(self):
SyftWorkerImageService,
SyftWorkerPoolService,
SyftImageRegistryService,
EventService,
]

if OBLV:
Expand Down
35 changes: 35 additions & 0 deletions packages/syft/src/syft/protocol/protocol_version.json
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,41 @@
"hash": "07d84a95324d95d9c868cd7d1c33c908f77aa468671d76c144586aab672bcbb5",
"action": "add"
}
},
"Event": {
"1": {
"version": 1,
"hash": "1f3a5a19594887c11d01385352ba0244e3a57f02019e0df4a0f9da9393a840b1",
"action": "add"
}
},
"CRUDEvent": {
"1": {
"version": 1,
"hash": "5a58f86d52caaf2ae29c00a5809e5a17d91f480ea796d9107aa9a3a07881c4a1",
"action": "add"
}
},
"CreateObjectEvent": {
"1": {
"version": 1,
"hash": "58e80bd2f193c55730438468f02459cfc8ce678cbeac347548e243340a8749b0",
"action": "add"
}
},
"UpdateObjectEvent": {
"1": {
"version": 1,
"hash": "e7af4c8bcb974197235cdabea37d26a35f1066077010d1afaea33ccb4d92b8ce",
"action": "add"
}
},
"CreateDatasetEvent": {
"1": {
"version": 1,
"hash": "f1bc0d382312d5e91f86098bf561a7e0f716d82560678e69242f8dddb6604746",
"action": "add"
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/syft/src/syft/service/dataset/dataset_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import Optional
from typing import Union

from ..event.event import CreateDatasetEvent

# relative
from ...serde.serializable import serializable
from ...store.document_store import DocumentStore
Expand Down Expand Up @@ -98,6 +100,15 @@ def add(
)
if result.is_err():
return SyftError(message=str(result.err()))

event = CreateDatasetEvent(
object_id=dataset.id,
creator_user=UID(),
)
res = context.node.get_service("EventService").add(context, event)
if isinstance(res, SyftError):
return res

return SyftSuccess(
message=f"Dataset uploaded to '{context.node.name}'. "
f"To see the datasets uploaded by a client on this node, use command `[your_client].datasets`"
Expand Down
23 changes: 21 additions & 2 deletions packages/syft/src/syft/service/event/event.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, ClassVar, Dict, List, Type
from syft.serde.serializable import serializable

from syft.service.dataset.dataset import Asset, Dataset
from syft.store.linked_obj import LinkedObject
from ...types.syft_object import SyftObject
from ...types.syft_object import SYFT_OBJECT_VERSION_1, SyftObject
from ...types.uid import UID
from datetime import datetime
from pydantic import Field
Expand All @@ -16,23 +17,34 @@ def inner(method):

return inner

@serializable()
class Event(SyftObject):
__canonical_name__ = "Event"
__version__ = SYFT_OBJECT_VERSION_1
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)


@serializable()
class CRUDEvent(Event):
__canonical_name__ = "CRUDEvent"
__version__ = SYFT_OBJECT_VERSION_1
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}"


@serializable()
class CreateObjectEvent(CRUDEvent):
__canonical_name__ = "CreateObjectEvent"
__version__ = SYFT_OBJECT_VERSION_1
@property
def updated_properties(self):
return list(self.object_type.__annotations__.keys())
Expand All @@ -46,7 +58,10 @@ def update_tuples(self):
return list(self.updates.items())


@serializable()
class UpdateObjectEvent(CRUDEvent):
__canonical_name__ = "UpdateObjectEvent"
__version__ = SYFT_OBJECT_VERSION_1
updates: Dict[str, Any]

@property
Expand All @@ -57,7 +72,11 @@ def updated_properties(self):
def update_tuples(self):
return list(self.updates.items())

class CreateDatasetEvent(CRUDEvent):

@serializable()
class CreateDatasetEvent(CreateObjectEvent):
__canonical_name__ = "CreateDatasetEvent"
__version__ = SYFT_OBJECT_VERSION_1
object_type: ClassVar[Type] = Dataset

def execute(self, node):
Expand Down

0 comments on commit 660dbb2

Please sign in to comment.