Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Improve Core Events to support additional metadata #829

Merged
merged 6 commits into from
Nov 22, 2023

Conversation

gmarabout
Copy link
Contributor

@gmarabout gmarabout commented Nov 20, 2023

Goals

We would like to add additional data to core events so we can use it for telemetry.
The data we want to add are: the config_id of the entity (will eventually be sent as an attribute), the possibility to add extra data, especially for job events (like duration/latency).

What the PR solves

We will need to create events with different (meta)data depending on the entity type.
The problem is that the method _publish_event takes a bunch of data that the callers must provide in a non-generic way. It means that if we add one field to an event, then we have to change it everywhere _publish_event is used.
Moreover, the method being static, it is difficult for the dynamic _Reloader and _Properties classes to have a generic code.

The biggest change in this PR is the attempt to make a central piece where the logic of creating an event is delegated to a _make_event function which is "overridden" per entity type using the built-in Python singledispatch mechanism.
This function could eventually totally replace the static _publish_event.

For now, the _publish_event is still there for few cases.

Changes

  • Simplification: Class Event is now a frozen dataclass
  • Added Event.attribute_value to contain the value of a changed attribute
  • Added Event.metadata, a dict to contain any extra information we would like to transmit via the event (ex: task_config_id for job events)./
  • Introduce a "single dispatch" function _make_event to create an event for a particular entity type. The base function corresponds to the "default" case, and will raise an error if called. The overloaded functions are implemented within each entity Python file (in job.py for jobs, in scenario.py for scenarios, etc) and will implement the logic of creating an event from this specific entity. This method is intended to centralize event creation.

This argument is always given in case of an UPDATE.
**kwargs (dict[str, any]): Any extra information that would be passed to the metadata event.
Note: you should pass only simple types: str, float, double as values."""
raise Exception(f"Unexpected entity type: {type(entity)}")
Copy link
Contributor Author

@gmarabout gmarabout Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes to reviewers:
This function must be overloaded for each entity supporting event creation. This one would be called as a fallback if the interpreter does not find any for a entity type.

Copy link

github-actions bot commented Nov 20, 2023

☂️ Python Cov

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
8716 8256 95% 85% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
src/taipy/core/_entity/_entity.py 100% 🟢
src/taipy/core/_entity/_properties.py 100% 🟢
src/taipy/core/_entity/_reload.py 98% 🟢
src/taipy/core/_manager/_manager.py 99% 🟢
src/taipy/core/cycle/_cycle_manager.py 100% 🟢
src/taipy/core/cycle/cycle.py 98% 🟢
src/taipy/core/data/_data_manager.py 98% 🟢
src/taipy/core/data/data_node.py 99% 🟢
src/taipy/core/job/_job_manager.py 100% 🟢
src/taipy/core/job/job.py 98% 🟢
src/taipy/core/notification/init.py 100% 🟢
src/taipy/core/notification/event.py 98% 🟢
src/taipy/core/notification/notifier.py 100% 🟢
src/taipy/core/scenario/_scenario_manager.py 96% 🟢
src/taipy/core/scenario/scenario.py 94% 🟢
src/taipy/core/sequence/_sequence_manager.py 94% 🟢
src/taipy/core/sequence/sequence.py 98% 🟢
src/taipy/core/submission/_submission_manager.py 100% 🟢
src/taipy/core/submission/submission.py 95% 🟢
src/taipy/core/task/_task_manager.py 96% 🟢
src/taipy/core/task/task.py 94% 🟢
TOTAL 98% 🟢

updated for commit: ecf2f99 by action🐍

while self._in_context_attributes_changed_collector:
_publish_event(*self._in_context_attributes_changed_collector.pop(0))
for event in self._in_context_attributes_changed_collector:
Notifier.publish(event)
Copy link
Contributor Author

@gmarabout gmarabout Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: Adding more arguments to the Event class make the use of a arg list a bit cumbersome and very error prone (think the order being significant!).
That is why I propose to replace this list of args by a list of Event objects. Then, the logic does not change, except that it makes more sense to call Notifier.publish directly now...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's awesome!!

@@ -58,6 +60,7 @@ class EventEntityType(_ReprEnum):
}


@dataclass(frozen=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: the use of dataclass makes the definition of this (read-only) class sleaker. It also provides for free default repr and hash.

@@ -19,11 +19,38 @@

def _publish_event(
entity_type: EventEntityType,
Copy link
Contributor Author

@gmarabout gmarabout Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to reviewers: the only two mandatory arguments are the entity_type and the operation. I propose to move them at first position, and be the only positional arguments for better readability. All the others can be specified using the named argument notation (including potential extra medatata).

src/taipy/core/_manager/_manager.py Outdated Show resolved Hide resolved
src/taipy/core/notification/event.py Show resolved Hide resolved
src/taipy/core/job/_job_manager.py Outdated Show resolved Hide resolved
return Event(
entity_type=EventEntityType.JOB,
entity_id=job.id,
config_id=job._task.config_id, # OK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous comment may have an impact on this line.

return Event(
entity_type=EventEntityType.JOB,
entity_id=job.id,
config_id=job._task.config_id, # OK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove # OK



@singledispatch
def make_event(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rename the method _make_event for now. Once we want to open this as an API for users, we can name it without the _ prefix, but this will require a lot of documentation, and compatibility with future versions. I believe we are not ready for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but notice that Notifier.publish(event) is public and documented...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method has been renamed into _make_event.

@gmarabout gmarabout changed the title [DRAFT] Add additional data to Core Events Slightly improve Core Events to support additional metadata Nov 21, 2023
@gmarabout gmarabout marked this pull request as ready for review November 21, 2023 08:26
@gmarabout gmarabout requested a review from jrobinAV November 21, 2023 08:27
@gmarabout gmarabout changed the title Slightly improve Core Events to support additional metadata Improve Core Events to support additional metadata Nov 21, 2023
Copy link
Member

@toan-quach toan-quach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good to me!!! also learnt something new too! Thanks @gmarabout !

@gmarabout gmarabout merged commit 652fd79 into develop Nov 22, 2023
40 of 42 checks passed
@jrobinAV jrobinAV deleted the experimental/improve-core-events branch January 2, 2024 15:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants