-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from christoph-blessing/integrate_messagebus
Integrate message bus
- Loading branch information
Showing
22 changed files
with
553 additions
and
959 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
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
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 |
---|---|---|
@@ -1,107 +1,40 @@ | ||
"""Logic associated with presenting information about finished use-cases.""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Callable, Iterable | ||
|
||
from link.service.services import ( | ||
ListIdleEntitiesResponse, | ||
OperationResponse, | ||
) | ||
from link.domain import events | ||
|
||
from .custom_types import PrimaryKey | ||
from .identification import IdentificationTranslator | ||
|
||
|
||
@dataclass(frozen=True) | ||
class OperationRecord: | ||
"""Record of a finished operation.""" | ||
|
||
requests: list[Request] | ||
successes: list[Sucess] | ||
failures: list[Failure] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Request: | ||
"""Record of a request to perform a certain operation on a particular entity.""" | ||
|
||
primary_key: PrimaryKey | ||
operation: str | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Sucess: | ||
"""Record of a successful operation on a particular entity.""" | ||
|
||
primary_key: PrimaryKey | ||
operation: str | ||
transition: Transition | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Transition: | ||
"""Record of a transition between two states.""" | ||
|
||
old: str | ||
new: str | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Failure: | ||
"""Record of a failed operation on a particular entity.""" | ||
|
||
primary_key: PrimaryKey | ||
operation: str | ||
state: str | ||
|
||
|
||
def create_operation_response_presenter( | ||
translator: IdentificationTranslator, show: Callable[[OperationRecord], None] | ||
) -> Callable[[OperationResponse], None]: | ||
"""Create a callable that when called presents information about a finished operation.""" | ||
|
||
def get_class_name(obj: type) -> str: | ||
return obj.__name__ | ||
|
||
def present_operation_response(response: OperationResponse) -> None: | ||
show( | ||
OperationRecord( | ||
[ | ||
Request(translator.to_primary_key(identifier), response.operation.name) | ||
for identifier in response.requested | ||
], | ||
[ | ||
Sucess( | ||
translator.to_primary_key(update.identifier), | ||
operation=response.operation.name, | ||
transition=Transition( | ||
get_class_name(update.transition.current).upper(), | ||
get_class_name(update.transition.new).upper(), | ||
), | ||
) | ||
for update in response.updates | ||
], | ||
[ | ||
Failure( | ||
translator.to_primary_key(error.identifier), | ||
error.operation.name, | ||
get_class_name(error.state).upper(), | ||
) | ||
for error in response.errors | ||
], | ||
) | ||
) | ||
|
||
return present_operation_response | ||
|
||
|
||
def create_idle_entities_updater( | ||
translator: IdentificationTranslator, update: Callable[[Iterable[PrimaryKey]], None] | ||
) -> Callable[[ListIdleEntitiesResponse], None]: | ||
) -> Callable[[events.IdleEntitiesListed], None]: | ||
"""Create a callable that when called updates the list of idle entities.""" | ||
|
||
def update_idle_entities(response: ListIdleEntitiesResponse) -> None: | ||
def update_idle_entities(response: events.IdleEntitiesListed) -> None: | ||
update(translator.to_primary_key(identifier) for identifier in response.identifiers) | ||
|
||
return update_idle_entities | ||
|
||
|
||
def create_state_change_logger( | ||
translator: IdentificationTranslator, log: Callable[[str], None] | ||
) -> Callable[[events.StateChanged], None]: | ||
"""Create a logger that logs state changes of entities.""" | ||
|
||
def log_state_change(state_change: events.StateChanged) -> None: | ||
context = { | ||
"identifier": translator.to_primary_key(state_change.identifier), | ||
"operation": state_change.operation.name, | ||
"transition": { | ||
"old": state_change.transition.current.__name__, | ||
"new": state_change.transition.new.__name__, | ||
}, | ||
"command": state_change.command.name, | ||
} | ||
log(f"Entity state changed {context}") | ||
|
||
return log_state_change |
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 @@ | ||
"""Contains all domain commands.""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from .custom_types import Identifier | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Command: | ||
"""Base class for all commands.""" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class PullEntities(Command): | ||
"""Pull the requested entities.""" | ||
|
||
requested: frozenset[Identifier] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DeleteEntities(Command): | ||
"""Delete the requested entities.""" | ||
|
||
requested: frozenset[Identifier] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ListIdleEntities(Command): | ||
"""Start the delete process for the requested entities.""" |
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,45 @@ | ||
"""Contains all domain events.""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING | ||
|
||
from .custom_types import Identifier | ||
|
||
if TYPE_CHECKING: | ||
from .state import Commands, Operations, State, Transition | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Event: | ||
"""Base class for all events.""" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class OperationApplied(Event): | ||
"""An operation was applied to an entity.""" | ||
|
||
operation: Operations | ||
identifier: Identifier | ||
|
||
|
||
@dataclass(frozen=True) | ||
class InvalidOperationRequested(OperationApplied): | ||
"""An operation that is invalid given the entities current state was requested.""" | ||
|
||
state: type[State] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class StateChanged(OperationApplied): | ||
"""The state of an entity changed during the application of an operation.""" | ||
|
||
transition: Transition | ||
command: Commands | ||
|
||
|
||
@dataclass(frozen=True) | ||
class IdleEntitiesListed(Event): | ||
"""Idle entities in a link have been listed.""" | ||
|
||
identifiers: frozenset[Identifier] |
Oops, something went wrong.