Skip to content

Commit

Permalink
Publish Results automatically once all atheletes have been checked
Browse files Browse the repository at this point in the history
  • Loading branch information
lachiemurray committed Feb 11, 2025
1 parent 105f45a commit 9115d93
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
17 changes: 14 additions & 3 deletions phx/results/jobs/hourly/update_performances.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def execute(self):
midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)

athletes = Athlete.objects.filter(
Q(last_checked__lt=six_months_ago, active=False)
| Q(last_checked__lt=midnight, active=True)
| Q(last_checked__isnull=True))
self.athletes_query(six_months_ago, midnight))

num_to_check = math.ceil(
min(self.MAX_ATHLETES_PER_HOUR,
Expand All @@ -53,11 +51,24 @@ def execute(self):

performances, events, inactives = scraper.save()

remaining = Athlete.objects.filter(
self.athletes_query(six_months_ago, midnight)).count()

if remaining == 0:
logger.info("All athletes checked for today, publishing results")
scraper.publish_results()

logger.info(f"{inactives} inactive athletes found")
logger.info(f"{performances} performances updated")
logger.info(f"{events} events updated")
logger.info("Job complete")

@staticmethod
def athletes_query(six_months_ago: datetime, midnight: datetime) -> Q:
return Q(last_checked__lt=six_months_ago, active=False) | \
Q(last_checked__lt=midnight, active=True) | \
Q(last_checked__isnull=True)

@staticmethod
def fraction_to_check(time: datetime) -> float:
if time.hour < 6 or time.hour >= 22:
Expand Down
20 changes: 20 additions & 0 deletions phx/results/performances_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,26 @@ def save(self):

return len(performances), len(events), inactive_athletes

def publish_results(self):
today = datetime.now(tz=timezone.utc).replace(hour=0,
minute=0,
second=0,
microsecond=0)

# Find all Events that were created today and are
# associated with a draft Result
events = Event.objects.filter(result__draft=True,
created_date__gt=today,
result__created_date__gt=today)

# Publish the Results of all Events created today
for event in events:
if event.result:
event.result.draft = False
event.result.save()

return len(events)

@staticmethod
def remove_conflicts(performances):
seen = set()
Expand Down
36 changes: 36 additions & 0 deletions phx/results/tests/test_performances_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,42 @@ def test_dont_crash_when_profile_missing(self):

scraper.find_performances(athlete, datetime.date(2024, 5, 1))

def test_publishes_results_for_events_created_today(self):
result = Result.objects.create(title="parkrun - Preston Park",
event_date=datetime.datetime.now(),
draft=True)

Event.objects.create(name='parkrun',
location='Preston Park',
power_of_10_meeting_id='5678',
result=result)

self.assertEqual(1, PerformancesScraper().publish_results())

result.refresh_from_db()

self.assertFalse(result.draft)

def test_doesnt_publish_results_for_events_not_created_today(self):
past = datetime.datetime(2024, 5, 1, tzinfo=datetime.timezone.utc)
result = Result.objects.create(title="parkrun - Preston Park",
event_date=past,
draft=True)

event = Event.objects.create(name='parkrun',
location='Preston Park',
power_of_10_meeting_id='5678',
result=result)

event.created_date = past
event.save()

self.assertEqual(0, PerformancesScraper().publish_results())

result.refresh_from_db()

self.assertTrue(result.draft)

def setup_profile_page(self,
performances,
current_club="Brighton Phoenix"):
Expand Down

0 comments on commit 9115d93

Please sign in to comment.