2021-07-12 22:17:49 +02:00
|
|
|
import logging
|
2021-05-05 14:29:13 +02:00
|
|
|
from abc import ABC, abstractmethod
|
2021-05-04 22:58:00 +02:00
|
|
|
from typing import List, Optional
|
2021-07-12 22:17:49 +02:00
|
|
|
|
2021-05-30 21:47:36 +02:00
|
|
|
import arrow
|
2021-05-04 22:58:00 +02:00
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.config.config import get_settings
|
2022-12-11 14:15:04 +01:00
|
|
|
from mobilizon_reshare.dataclasses import MobilizonEvent
|
2021-05-04 22:58:00 +02:00
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-05-04 22:58:00 +02:00
|
|
|
|
|
|
|
class EventSelectionStrategy(ABC):
|
|
|
|
def select(
|
|
|
|
self,
|
|
|
|
published_events: List[MobilizonEvent],
|
|
|
|
unpublished_events: List[MobilizonEvent],
|
2021-07-07 11:19:37 +02:00
|
|
|
) -> Optional[MobilizonEvent]:
|
2021-11-11 15:18:04 +01:00
|
|
|
selected = self._select(published_events, unpublished_events)
|
|
|
|
if selected:
|
|
|
|
return selected[0]
|
2022-03-06 10:41:02 +01:00
|
|
|
return None
|
2021-07-07 11:19:37 +02:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _select(
|
|
|
|
self,
|
|
|
|
published_events: List[MobilizonEvent],
|
|
|
|
unpublished_events: List[MobilizonEvent],
|
2021-11-11 15:18:04 +01:00
|
|
|
) -> Optional[List[MobilizonEvent]]:
|
2021-05-04 22:58:00 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SelectNextEventStrategy(EventSelectionStrategy):
|
2021-07-07 11:19:37 +02:00
|
|
|
def _select(
|
2021-05-04 22:58:00 +02:00
|
|
|
self,
|
|
|
|
published_events: List[MobilizonEvent],
|
|
|
|
unpublished_events: List[MobilizonEvent],
|
2021-11-11 15:18:04 +01:00
|
|
|
) -> Optional[List[MobilizonEvent]]:
|
2021-05-04 22:58:00 +02:00
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
# if there are no unpublished events, there's nothing I can do
|
|
|
|
if not unpublished_events:
|
|
|
|
logger.debug("No event to publish.")
|
2021-11-11 15:18:04 +01:00
|
|
|
return []
|
2021-07-12 22:17:49 +02:00
|
|
|
|
|
|
|
# if there's no published event (first execution) I return the next in queue
|
|
|
|
if not published_events:
|
|
|
|
logger.debug(
|
|
|
|
"First Execution with an available event. Picking next event in the queue."
|
|
|
|
)
|
2021-11-11 15:18:04 +01:00
|
|
|
return unpublished_events
|
2021-07-12 22:17:49 +02:00
|
|
|
|
|
|
|
last_published_event = published_events[-1]
|
2021-05-30 21:47:36 +02:00
|
|
|
now = arrow.now()
|
2021-07-12 22:17:49 +02:00
|
|
|
last_published_event_most_recent_publication_time = max(
|
|
|
|
last_published_event.publication_time.values()
|
|
|
|
)
|
|
|
|
|
|
|
|
assert last_published_event_most_recent_publication_time < now, (
|
2021-05-05 14:29:13 +02:00
|
|
|
f"Last published event has been published in the future\n"
|
2021-07-12 22:17:49 +02:00
|
|
|
f"{last_published_event_most_recent_publication_time}\n"
|
2021-05-30 21:47:36 +02:00
|
|
|
f"{now}"
|
2021-05-05 14:29:13 +02:00
|
|
|
)
|
2021-05-04 22:58:00 +02:00
|
|
|
if (
|
2021-07-12 22:17:49 +02:00
|
|
|
last_published_event_most_recent_publication_time.shift(
|
|
|
|
minutes=get_settings()[
|
2021-07-07 11:41:13 +02:00
|
|
|
"selection.strategy_options.break_between_events_in_minutes"
|
|
|
|
]
|
2021-05-30 21:47:36 +02:00
|
|
|
)
|
|
|
|
> now
|
2021-05-04 22:58:00 +02:00
|
|
|
):
|
2021-07-12 22:17:49 +02:00
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
"Last event was published recently. No event is going to be published."
|
|
|
|
)
|
2021-11-11 15:18:04 +01:00
|
|
|
return []
|
2021-05-04 22:58:00 +02:00
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
return unpublished_events
|
2021-05-04 22:58:00 +02:00
|
|
|
|
|
|
|
|
2021-07-07 11:41:13 +02:00
|
|
|
STRATEGY_NAME_TO_STRATEGY_CLASS = {"next_event": SelectNextEventStrategy}
|
|
|
|
|
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
def select_unpublished_events(
|
2022-01-08 00:54:27 +01:00
|
|
|
published_events: List[MobilizonEvent], unpublished_events: List[MobilizonEvent],
|
2021-11-11 15:18:04 +01:00
|
|
|
):
|
|
|
|
|
|
|
|
strategy = STRATEGY_NAME_TO_STRATEGY_CLASS[
|
|
|
|
get_settings()["selection"]["strategy"]
|
|
|
|
]()
|
|
|
|
|
|
|
|
return strategy._select(published_events, unpublished_events)
|
|
|
|
|
|
|
|
|
2021-07-07 11:41:13 +02:00
|
|
|
def select_event_to_publish(
|
2022-01-08 00:54:27 +01:00
|
|
|
published_events: List[MobilizonEvent], unpublished_events: List[MobilizonEvent],
|
2022-05-17 23:14:52 +02:00
|
|
|
) -> Optional[MobilizonEvent]:
|
2021-07-07 11:41:13 +02:00
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
strategy = STRATEGY_NAME_TO_STRATEGY_CLASS[
|
|
|
|
get_settings()["selection"]["strategy"]
|
|
|
|
]()
|
|
|
|
|
2021-07-07 11:41:13 +02:00
|
|
|
return strategy.select(published_events, unpublished_events)
|