Skip to content

fl_server_ai.notification

Modules:

Name Description
notification
notification_type
serializable
training

Classes:

Name Description
Notification

Abstract base class for notifications.

NotificationType

Notification types including a short description for each type.

TrainingFinishedNotification

Notification that a training has finished.

TrainingRoundStartNotification

Notification for the start of a training round.

Attributes

__all__ module-attribute

__all__ = ['NotificationType', 'Notification', 'TrainingRoundStartNotification', 'TrainingFinishedNotification']

Classes

Notification dataclass

Bases: Generic[TBody], Serializable


              flowchart TD
              fl_server_ai.notification.Notification[Notification]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.Notification
                


              click fl_server_ai.notification.Notification href "" "fl_server_ai.notification.Notification"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Abstract base class for notifications.

Classes:

Name Description
Body

Inner class for the body of the notification.

Methods:

Name Description
__init__
send

Send notification to the receivers asynchronously.

serialize

Serialize the notification into a dictionary.

Attributes:

Name Type Description
body TBody

The body of the notification.

callback_error Signature | None

The callback to be called on error. By default, this is None.

callback_success Signature | None

The callback to be called on success. By default, this is None.

receivers list[NotificationReceiver]

The receivers of the notification.

type NotificationType

The type of the notification.

Source code in fl_server_ai/notification/notification.py
@dataclass
class Notification(Generic[TBody], Serializable, metaclass=ABCMeta):
    """
    Abstract base class for notifications.
    """

    receivers: List[NotificationReceiver]
    """The receivers of the notification."""
    body: TBody
    """The body of the notification."""
    type: NotificationType = field(init=False)
    """The type of the notification."""

    @dataclass
    class Body(Serializable):
        """
        Inner class for the body of the notification.
        """
        pass

    @property
    def callback_success(self) -> Optional[Signature]:
        """
        The callback to be called on success. By default, this is None.

        Returns:
            Optional[Signature]: The callback to be called on success, or None if no such callback is set.
        """
        return None

    @property
    def callback_error(self) -> Optional[Signature]:
        """
        The callback to be called on error. By default, this is None.

        Returns:
            Optional[Signature]: The callback to be called on error, or None if no such callback is set.
        """
        return None

    def send(
        self,
        callback_success: Optional[Signature] = None,
        callback_error: Optional[Signature] = None
    ) -> AsyncResult:
        """
        Send notification to the receivers asynchronously.

        Args:
            callback_success (Optional[Signature], optional): The callback to be called on success. Defaults to None.
            callback_error (Optional[Signature], optional): The callback to be called on error. Defaults to None.

        Returns:
            AsyncResult: The result of the asynchronous operation.
        """
        callback_success = callback_success or self.callback_success
        callback_error = callback_error or self.callback_error
        return send_notifications.s(
            notification=self, callback_success=callback_success, callback_error=callback_error
        ).apply_async(retry=False)

    def serialize(self) -> dict[str, Any]:
        """
        Serialize the notification into a dictionary.

        Returns:
            dict[str, Any]: The serialized notification.
        """
        return {
            "notification_type": self.type.value,
            "body": self.body.serialize()
        }

Attributes

body instance-attribute
body: TBody

The body of the notification.

callback_error property
callback_error: Signature | None

The callback to be called on error. By default, this is None.

Returns:

Type Description
Signature | None

Optional[Signature]: The callback to be called on error, or None if no such callback is set.

callback_success property
callback_success: Signature | None

The callback to be called on success. By default, this is None.

Returns:

Type Description
Signature | None

Optional[Signature]: The callback to be called on success, or None if no such callback is set.

receivers instance-attribute

The receivers of the notification.

type class-attribute instance-attribute
type: NotificationType = field(init=False)

The type of the notification.

Classes

Body dataclass

Bases: Serializable


              flowchart TD
              fl_server_ai.notification.Notification.Body[Body]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.Notification.Body
                


              click fl_server_ai.notification.Notification.Body href "" "fl_server_ai.notification.Notification.Body"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Inner class for the body of the notification.

Methods:

Name Description
__init__
Source code in fl_server_ai/notification/notification.py
@dataclass
class Body(Serializable):
    """
    Inner class for the body of the notification.
    """
    pass
Functions
__init__
__init__() -> None

Functions

__init__
__init__(receivers: list[NotificationReceiver], body: TBody) -> None
send
send(callback_success: Signature | None = None, callback_error: Signature | None = None) -> AsyncResult

Send notification to the receivers asynchronously.

Parameters:

Name Type Description Default
callback_success
Signature | None

The callback to be called on success. Defaults to None.

None
callback_error
Signature | None

The callback to be called on error. Defaults to None.

None

Returns:

Name Type Description
AsyncResult AsyncResult

The result of the asynchronous operation.

Source code in fl_server_ai/notification/notification.py
def send(
    self,
    callback_success: Optional[Signature] = None,
    callback_error: Optional[Signature] = None
) -> AsyncResult:
    """
    Send notification to the receivers asynchronously.

    Args:
        callback_success (Optional[Signature], optional): The callback to be called on success. Defaults to None.
        callback_error (Optional[Signature], optional): The callback to be called on error. Defaults to None.

    Returns:
        AsyncResult: The result of the asynchronous operation.
    """
    callback_success = callback_success or self.callback_success
    callback_error = callback_error or self.callback_error
    return send_notifications.s(
        notification=self, callback_success=callback_success, callback_error=callback_error
    ).apply_async(retry=False)
serialize
serialize() -> dict[str, Any]

Serialize the notification into a dictionary.

Returns:

Type Description
dict[str, Any]

dict[str, Any]: The serialized notification.

Source code in fl_server_ai/notification/notification.py
def serialize(self) -> dict[str, Any]:
    """
    Serialize the notification into a dictionary.

    Returns:
        dict[str, Any]: The serialized notification.
    """
    return {
        "notification_type": self.type.value,
        "body": self.body.serialize()
    }

NotificationType

Bases: Enum


              flowchart TD
              fl_server_ai.notification.NotificationType[NotificationType]

              

              click fl_server_ai.notification.NotificationType href "" "fl_server_ai.notification.NotificationType"
            

Notification types including a short description for each type.

Methods:

Name Description
__init__

Initialize the enum member.

__new__

Override the __new__ method to set the value of the enum member.

Attributes:

Name Type Description
CLIENT_REMOVED
MODEL_TEST_ROUND
SWAG_ROUND_START
TRAINING_FINISHED
TRAINING_START
UPDATE_ROUND_START
description

Property to get the description of the enum member.

Source code in fl_server_ai/notification/notification_type.py
class NotificationType(Enum):
    """
    Notification types including a short description for each type.
    """

    UPDATE_ROUND_START = "UPDATE_ROUND_START", _("Start update round")
    SWAG_ROUND_START = "SWAG_ROUND_START", _("Start swag round")
    TRAINING_START = "TRAINING_START", _("Training start")
    TRAINING_FINISHED = "TRAINING_FINISHED", _("Training finished")
    CLIENT_REMOVED = "CLIENT_REMOVED", _("Client removed")
    MODEL_TEST_ROUND = "MODEL_TEST_ROUND", _("Start model round")

    def __new__(cls, *args, **kwargs):
        """
        Override the `__new__` method to set the value of the enum member.

        Args:
            *args: Variable length argument list where the first value is the value of the enum member.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            object: New instance of the class.
        """
        obj = object.__new__(cls)
        obj._value_ = args[0]
        return obj

    def __init__(self, _: str, description: Optional[str] = None):
        """
        Initialize the enum member.

        Args:
            _ (str): The value of the enum member. Ignored in this method as it's already set by __new__.
            description (Optional[str], optional): The description of the enum member. Defaults to None.
        """
        # ignore the first param since it's already set by __new__
        self._description_ = description

    @property
    def description(self):
        """
        Property to get the description of the enum member.

        Returns:
            str: The description of the enum member.
        """
        return self._description_

Attributes

CLIENT_REMOVED class-attribute instance-attribute
CLIENT_REMOVED = ('CLIENT_REMOVED', gettext_lazy('Client removed'))
MODEL_TEST_ROUND class-attribute instance-attribute
MODEL_TEST_ROUND = ('MODEL_TEST_ROUND', gettext_lazy('Start model round'))
SWAG_ROUND_START class-attribute instance-attribute
SWAG_ROUND_START = ('SWAG_ROUND_START', gettext_lazy('Start swag round'))
TRAINING_FINISHED class-attribute instance-attribute
TRAINING_FINISHED = ('TRAINING_FINISHED', gettext_lazy('Training finished'))
TRAINING_START class-attribute instance-attribute
TRAINING_START = ('TRAINING_START', gettext_lazy('Training start'))
UPDATE_ROUND_START class-attribute instance-attribute
UPDATE_ROUND_START = ('UPDATE_ROUND_START', gettext_lazy('Start update round'))
description property
description

Property to get the description of the enum member.

Returns:

Name Type Description
str

The description of the enum member.

Functions

__init__
__init__(_: str, description: str | None = None)

Initialize the enum member.

Parameters:

Name Type Description Default
_
str

The value of the enum member. Ignored in this method as it's already set by new.

required
description
str | None

The description of the enum member. Defaults to None.

None
Source code in fl_server_ai/notification/notification_type.py
def __init__(self, _: str, description: Optional[str] = None):
    """
    Initialize the enum member.

    Args:
        _ (str): The value of the enum member. Ignored in this method as it's already set by __new__.
        description (Optional[str], optional): The description of the enum member. Defaults to None.
    """
    # ignore the first param since it's already set by __new__
    self._description_ = description
__new__
__new__(*args, **kwargs)

Override the __new__ method to set the value of the enum member.

Parameters:

Name Type Description Default
*args

Variable length argument list where the first value is the value of the enum member.

()
**kwargs

Arbitrary keyword arguments.

{}

Returns:

Name Type Description
object

New instance of the class.

Source code in fl_server_ai/notification/notification_type.py
def __new__(cls, *args, **kwargs):
    """
    Override the `__new__` method to set the value of the enum member.

    Args:
        *args: Variable length argument list where the first value is the value of the enum member.
        **kwargs: Arbitrary keyword arguments.

    Returns:
        object: New instance of the class.
    """
    obj = object.__new__(cls)
    obj._value_ = args[0]
    return obj

TrainingFinishedNotification dataclass

Bases: TrainingNotification['TrainingFinishedNotification.Body']


              flowchart TD
              fl_server_ai.notification.TrainingFinishedNotification[TrainingFinishedNotification]
              fl_server_ai.notification.training.training.TrainingNotification[TrainingNotification]
              fl_server_ai.notification.notification.Notification[Notification]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.training.training.TrainingNotification --> fl_server_ai.notification.TrainingFinishedNotification
                                fl_server_ai.notification.notification.Notification --> fl_server_ai.notification.training.training.TrainingNotification
                                fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.notification.Notification
                




              click fl_server_ai.notification.TrainingFinishedNotification href "" "fl_server_ai.notification.TrainingFinishedNotification"
              click fl_server_ai.notification.training.training.TrainingNotification href "" "fl_server_ai.notification.training.training.TrainingNotification"
              click fl_server_ai.notification.notification.Notification href "" "fl_server_ai.notification.notification.Notification"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Notification that a training has finished.

Classes:

Name Description
Body

Inner class representing the body of the notification.

Methods:

Name Description
from_training

Create a TrainingFinishedNotification instance from a training object.

Attributes:

Name Type Description
type NotificationType

The type of the notification.

Source code in fl_server_ai/notification/training/finished.py
class TrainingFinishedNotification(TrainingNotification["TrainingFinishedNotification.Body"]):
    """
    Notification that a training has finished.
    """

    type: NotificationType = NotificationType.TRAINING_FINISHED
    """The type of the notification."""

    @dataclass
    class Body(Serializable):
        """
        Inner class representing the body of the notification.
        """
        global_model_uuid: UUID
        """The UUID of the global model."""

    @classmethod
    def from_training(cls, training: TrainingDB):
        """
        Create a `TrainingFinishedNotification` instance from a training object.

        Args:
            training (TrainingDB): The training object to create the notification from.

        Returns:
            TrainingFinishedNotification: The created notification.
        """
        receivers = list(training.participants.all())
        if not receivers.__contains__(training.actor):
            receivers.append(training.actor)
        return cls(
            receivers=receivers,
            body=cls.Body(
                global_model_uuid=training.model.id
            ),
            training_uuid=training.id
        )

Attributes

type class-attribute instance-attribute

The type of the notification.

Classes

Body dataclass

Bases: Serializable


              flowchart TD
              fl_server_ai.notification.TrainingFinishedNotification.Body[Body]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.TrainingFinishedNotification.Body
                


              click fl_server_ai.notification.TrainingFinishedNotification.Body href "" "fl_server_ai.notification.TrainingFinishedNotification.Body"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Inner class representing the body of the notification.

Methods:

Name Description
__init__

Attributes:

Name Type Description
global_model_uuid UUID

The UUID of the global model.

Source code in fl_server_ai/notification/training/finished.py
@dataclass
class Body(Serializable):
    """
    Inner class representing the body of the notification.
    """
    global_model_uuid: UUID
    """The UUID of the global model."""
Attributes
global_model_uuid instance-attribute
global_model_uuid: UUID

The UUID of the global model.

Functions
__init__
__init__(global_model_uuid: UUID) -> None

Functions

from_training classmethod
from_training(training: Training)

Create a TrainingFinishedNotification instance from a training object.

Parameters:

Name Type Description Default
training
Training

The training object to create the notification from.

required

Returns:

Name Type Description
TrainingFinishedNotification

The created notification.

Source code in fl_server_ai/notification/training/finished.py
@classmethod
def from_training(cls, training: TrainingDB):
    """
    Create a `TrainingFinishedNotification` instance from a training object.

    Args:
        training (TrainingDB): The training object to create the notification from.

    Returns:
        TrainingFinishedNotification: The created notification.
    """
    receivers = list(training.participants.all())
    if not receivers.__contains__(training.actor):
        receivers.append(training.actor)
    return cls(
        receivers=receivers,
        body=cls.Body(
            global_model_uuid=training.model.id
        ),
        training_uuid=training.id
    )

TrainingRoundStartNotification dataclass

Bases: TrainingNotification['TrainingRoundStartNotification.Body']


              flowchart TD
              fl_server_ai.notification.TrainingRoundStartNotification[TrainingRoundStartNotification]
              fl_server_ai.notification.training.training.TrainingNotification[TrainingNotification]
              fl_server_ai.notification.notification.Notification[Notification]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.training.training.TrainingNotification --> fl_server_ai.notification.TrainingRoundStartNotification
                                fl_server_ai.notification.notification.Notification --> fl_server_ai.notification.training.training.TrainingNotification
                                fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.notification.Notification
                




              click fl_server_ai.notification.TrainingRoundStartNotification href "" "fl_server_ai.notification.TrainingRoundStartNotification"
              click fl_server_ai.notification.training.training.TrainingNotification href "" "fl_server_ai.notification.training.training.TrainingNotification"
              click fl_server_ai.notification.notification.Notification href "" "fl_server_ai.notification.notification.Notification"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Notification for the start of a training round.

Classes:

Name Description
Body

Inner class representing the body of the notification.

Methods:

Name Description
from_training

Create a TrainingRoundStartNotification instance from a training object.

Attributes:

Name Type Description
callback_error Signature | None
callback_success Signature | None
type NotificationType

The type of the notification.

Source code in fl_server_ai/notification/training/round_start.py
class TrainingRoundStartNotification(TrainingNotification["TrainingRoundStartNotification.Body"]):
    """
    Notification for the start of a training round.
    """

    type: NotificationType = NotificationType.UPDATE_ROUND_START
    """The type of the notification."""

    @property
    def callback_success(self) -> Optional[Signature]:
        return training_notification_callback_success.s(training_uuid=self.training_uuid)

    @property
    def callback_error(self) -> Optional[Signature]:
        return training_notification_callback_failure.s(training_uuid=self.training_uuid)

    @dataclass
    class Body(Serializable):
        """
        Inner class representing the body of the notification.
        """
        round: int
        """The round number."""
        global_model_uuid: UUID
        """The UUID of the global model."""

    @classmethod
    def from_training(cls, training: TrainingDB):
        """
        Create a `TrainingRoundStartNotification` instance from a training object.

        Args:
            training (TrainingDB): The training object to create the notification from.

        Returns:
            TrainingRoundStartNotification: The created notification.
        """
        return cls(
            receivers=training.participants.all(),
            body=cls.Body(
                round=training.model.round,
                global_model_uuid=training.model.id
            ),
            training_uuid=training.id
        )

Attributes

callback_error property
callback_error: Signature | None
callback_success property
callback_success: Signature | None
type class-attribute instance-attribute

The type of the notification.

Classes

Body dataclass

Bases: Serializable


              flowchart TD
              fl_server_ai.notification.TrainingRoundStartNotification.Body[Body]
              fl_server_ai.notification.serializable.Serializable[Serializable]

                              fl_server_ai.notification.serializable.Serializable --> fl_server_ai.notification.TrainingRoundStartNotification.Body
                


              click fl_server_ai.notification.TrainingRoundStartNotification.Body href "" "fl_server_ai.notification.TrainingRoundStartNotification.Body"
              click fl_server_ai.notification.serializable.Serializable href "" "fl_server_ai.notification.serializable.Serializable"
            

Inner class representing the body of the notification.

Methods:

Name Description
__init__

Attributes:

Name Type Description
global_model_uuid UUID

The UUID of the global model.

round int

The round number.

Source code in fl_server_ai/notification/training/round_start.py
@dataclass
class Body(Serializable):
    """
    Inner class representing the body of the notification.
    """
    round: int
    """The round number."""
    global_model_uuid: UUID
    """The UUID of the global model."""
Attributes
global_model_uuid instance-attribute
global_model_uuid: UUID

The UUID of the global model.

round instance-attribute
round: int

The round number.

Functions
__init__
__init__(round: int, global_model_uuid: UUID) -> None

Functions

from_training classmethod
from_training(training: Training)

Create a TrainingRoundStartNotification instance from a training object.

Parameters:

Name Type Description Default
training
Training

The training object to create the notification from.

required

Returns:

Name Type Description
TrainingRoundStartNotification

The created notification.

Source code in fl_server_ai/notification/training/round_start.py
@classmethod
def from_training(cls, training: TrainingDB):
    """
    Create a `TrainingRoundStartNotification` instance from a training object.

    Args:
        training (TrainingDB): The training object to create the notification from.

    Returns:
        TrainingRoundStartNotification: The created notification.
    """
    return cls(
        receivers=training.participants.all(),
        body=cls.Body(
            round=training.model.round,
            global_model_uuid=training.model.id
        ),
        training_uuid=training.id
    )