Skip to content

Commit

Permalink
Suggested fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMwangi21 committed Dec 10, 2024
1 parent ce75d9e commit 4840a76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions server/planning/commands/flag_expired_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datetime import timedelta, datetime
from bson.objectid import ObjectId
from contextvars import ContextVar
from typing import Any

from planning.events import EventsAsyncService
from planning.planning import PlanningAsyncService
Expand Down Expand Up @@ -135,7 +136,7 @@ async def flag_expired_planning(expiry_datetime: datetime):
planning_service = PlanningAsyncService()

# Obtain the full list of Planning items that we're to process first
# As subsequent queries will change the list of returnd items
# As subsequent queries will change the list of returned items
plans = dict()
async for items in planning_service.get_expired_items(expiry_datetime):
plans.update({item[ID_FIELD]: item for item in items})
Expand All @@ -159,7 +160,7 @@ async def flag_expired_planning(expiry_datetime: datetime):
logger.info(f"{log_msg} {len(plans_expired)} Planning items expired: {list(plans_expired)}")


def set_event_plans(events):
def set_event_plans(events: dict[str, dict[str, Any]]) -> None:
for plan in get_related_planning_for_events(list(events.keys()), "primary"):
for related_event_id in get_related_event_ids_for_planning(plan, "primary"):
event = events[related_event_id]
Expand All @@ -168,7 +169,7 @@ def set_event_plans(events):
event["_plans"].append(plan)


def get_event_schedule(event):
def get_event_schedule(event: dict[str, Any]) -> datetime:
latest_scheduled = datetime.strptime(event["dates"]["end"], "%Y-%m-%dT%H:%M:%S%z")
for plan in event.get("_plans", []):
# First check the Planning item's planning date
Expand Down
12 changes: 6 additions & 6 deletions server/planning/commands/flag_expired_items_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
from .flag_expired_items import flag_expired_items_handler

now = utcnow()
yesterday = now - timedelta(hours=48)
two_days_ago = now - timedelta(hours=48)

active = {
"event": {"dates": {"start": now - timedelta(hours=1), "end": now}},
"overnightEvent": {"dates": {"start": yesterday, "end": now}},
"overnightEvent": {"dates": {"start": two_days_ago, "end": now}},
"plan": {"planning_date": now},
"coverage": {"planning": {"scheduled": now}},
}

expired = {
"event": {"dates": {"start": yesterday, "end": yesterday + timedelta(hours=1)}},
"plan": {"planning_date": yesterday},
"coverage": {"planning": {"scheduled": yesterday}},
"event": {"dates": {"start": two_days_ago, "end": two_days_ago + timedelta(hours=1)}},
"plan": {"planning_date": two_days_ago},
"coverage": {"planning": {"scheduled": two_days_ago}},
}


class FlagExpiredItemsTest(TestCase):
app_config = {
**TestCase.app_config.copy(),
# Expire items that are scheduled more than 24 hours from now
"PLANNING_EXPIRY_MINUTES": 1440,
"PLANNING_EXPIRY_MINUTES": 24 * 60,
}

async def asyncSetUp(self):
Expand Down

0 comments on commit 4840a76

Please sign in to comment.