-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* --custom_batch_size N * adds --ntfy argument * sends a notification on start * notification function * status_notification prep - brain.Ponderation -> models.Ponderation - ntfy.py -> notification.py - prep notification.status_notification function * Your collected {rep} unique posts over the last 24h * persist * improve persist.py , test concrent writes and abprut cancel * custom serializer * PersistedDict class * once_per_day uses persist * notify_at * source_type is set from process_batch with a static social list * ipfs schema updated after previous change * last_notification, renamed status_notification -> statistics_notification * docker_version_notifier * add a nice message about ntfy when using it * embarassing typo fix : embarassement -> embarassment --------- Co-authored-by: Mathias Dail <93382891+MathiasExorde@users.noreply.github.com>
- Loading branch information
1 parent
139cd43
commit 613decc
Showing
16 changed files
with
959 additions
and
144 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,82 @@ | ||
""" | ||
at.py | ||
Calls function at specified time. It mimics a cron like feature using a throttle | ||
logic. | ||
IT DOES NOT: | ||
- loop forever : the function is still expeced to be called periodicly | ||
IT DOES: | ||
- throttle the parameter function to be called only onced at a specified | ||
date | ||
- persist that it has called the action | ||
- delete the actions of previous day | ||
EXPECT: | ||
- list of times | ||
- the path where to persist the data | ||
- the async action to be called | ||
MISC: | ||
- persist.py | ||
- throttle.py | ||
""" | ||
from datetime import datetime | ||
from typing import Callable | ||
|
||
from datetime import datetime, time, timedelta, date | ||
from collections import deque | ||
|
||
from exorde.persist import PersistedDict | ||
|
||
|
||
def at(hours: list[time], path: str, action: Callable): | ||
def assert_integrity(persisted: PersistedDict, index): # index is a _Date | ||
if not index in persisted or not isinstance(persisted[index], list): | ||
persisted[index] = [] | ||
current_date = date.today() | ||
previous_day = (current_date - timedelta(days=1)).strftime("%Y-%m-%d") | ||
try: | ||
del persisted[previous_day] | ||
except: | ||
pass | ||
|
||
def custom_serializer(obj): | ||
if isinstance(obj, time): # Serialize datetime.time objects to strings | ||
time_str = obj.strftime("%H:%M:%S") | ||
return {"__time__": True, "value": time_str} | ||
return obj | ||
|
||
def custom_object_hook(obj): | ||
if ( | ||
"__time__" in obj | ||
): # Handle deserialization of datetime.time objects | ||
time_str = obj["value"] | ||
hour, minute, second = map(int, time_str.split(":")) | ||
time_obj = time(hour, minute, second) | ||
return time_obj | ||
return obj | ||
|
||
persisted = PersistedDict( | ||
path, | ||
serializer=custom_serializer, | ||
custom_object_hook=custom_object_hook, | ||
) | ||
result = None | ||
|
||
async def wrapper(*args, **kwargs): | ||
nonlocal result, persisted | ||
current_date = datetime.now().date().strftime("%Y-%m-%d") | ||
assert_integrity(persisted, current_date) | ||
print(persisted) | ||
for hour in hours: | ||
hour_as_datetime = datetime.combine(date.today(), hour) | ||
if ( | ||
datetime.now() >= hour_as_datetime | ||
and hour not in persisted[current_date] | ||
): | ||
persisted[current_date] = persisted[current_date] + [hour] | ||
result = await action(*args, **kwargs) | ||
|
||
# returned result is a non-feature | ||
return result | ||
|
||
return wrapper |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""notify user of new docker image version""" | ||
|
||
import os | ||
import logging | ||
import argparse | ||
|
||
from typing import Callable | ||
|
||
from exorde.persist import PersistedDict | ||
from exorde.notification import send_notification | ||
from exorde.models import LiveConfiguration | ||
|
||
|
||
def build_docker_version_notifier() -> Callable: | ||
last_notified_version = PersistedDict( | ||
"/tmp/exorde/docker_version_notification.json" | ||
) | ||
|
||
async def docker_version_notifier( | ||
live_configuration: LiveConfiguration, | ||
command_line_arguments: argparse.Namespace, | ||
) -> None: | ||
current_img_version = os.environ.get("EXORDE_DOCKER_IMG_VERSION", None) | ||
"""set during build time""" | ||
|
||
if not current_img_version: | ||
"""does nothing if no image version is specified in env""" | ||
return | ||
|
||
"""else check the version and notify the user""" | ||
|
||
nonlocal last_notified_version | ||
"""last version the user has been notified for""" | ||
|
||
live_version = live_configuration.get("docker_version", None) | ||
"""and docker version is specified by the network""" | ||
if not live_version: | ||
logging.warning("no docker version specified in LiveConfiguration") | ||
return | ||
|
||
if live_version != current_img_version: | ||
"""notify""" | ||
if ( | ||
last_notified_version["last_notification"] == None | ||
or last_notified_version != live_version | ||
): | ||
await send_notification( | ||
command_line_arguments, "A new exorde image is available" | ||
) | ||
last_notified_version["last_notification"] = live_version | ||
|
||
return docker_version_notifier | ||
|
||
|
||
docker_version_notifier = build_docker_version_notifier() | ||
|
||
__all__ = ["docker_version_notifier"] |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""last_notification is a string contained in LiveConfiguration""" | ||
|
||
import logging | ||
import argparse | ||
from typing import Callable | ||
|
||
from exorde.persist import PersistedDict | ||
from exorde.notification import send_notification | ||
from exorde.models import LiveConfiguration | ||
|
||
|
||
def build_last_notification() -> Callable: | ||
persisted_last_notification = PersistedDict( | ||
"/tmp/exorde/last_notification.json" | ||
) | ||
|
||
async def last_notification( | ||
live_configuration: LiveConfiguration, | ||
command_line_arguments: argparse.Namespace, | ||
) -> None: | ||
last_notification = live_configuration.get("last_notification", None) | ||
if not last_notification: | ||
logging.warning("no last_notification found in LiveConfiguration") | ||
return | ||
nonlocal persisted_last_notification | ||
if ( | ||
persisted_last_notification["last_notification"] == None | ||
or persisted_last_notification["last_notification"] | ||
!= last_notification | ||
): | ||
await send_notification(command_line_arguments, last_notification) | ||
persisted_last_notification[ | ||
"last_notification" | ||
] = last_notification | ||
|
||
return last_notification | ||
|
||
|
||
last_notification = build_last_notification() | ||
|
||
__all__ = ["last_notification"] |
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
Oops, something went wrong.