mirror of
https://github.com/Retropex/custom-ocean.xyz-dashboard.git
synced 2025-05-12 19:20:45 +02:00
Refactor daily stats posting logic
Updated the `_should_post_daily_stats` method to clarify that it checks for posting once per day at 12 PM. Simplified the logic to focus solely on this target time, requiring it to be a different day and within the first 5 minutes of 12 PM for posting. Adjusted the first-time posting condition to specifically check for 12 PM.
This commit is contained in:
parent
1dec2ba35b
commit
5664e44cd9
@ -282,38 +282,34 @@ class NotificationService:
|
|||||||
return [error_notification]
|
return [error_notification]
|
||||||
|
|
||||||
def _should_post_daily_stats(self):
|
def _should_post_daily_stats(self):
|
||||||
"""Check if it's time to post daily stats based on a time window approach."""
|
"""Check if it's time to post daily stats (once per day at 12 PM)."""
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
|
|
||||||
# Target hours for daily stats
|
# Target time is 12 PM (noon)
|
||||||
target_hours = [0, 6, 12, 18]
|
target_hour = 12
|
||||||
current_hour = now.hour
|
current_hour = now.hour
|
||||||
current_minute = now.minute
|
current_minute = now.minute
|
||||||
|
|
||||||
# If we have a last_daily_stats timestamp
|
# If we have a last_daily_stats timestamp
|
||||||
if self.last_daily_stats:
|
if self.last_daily_stats:
|
||||||
# Check time elapsed since last stats
|
# Check if it's a different day
|
||||||
time_since_last_stats = now - self.last_daily_stats
|
is_different_day = now.date() > self.last_daily_stats.date()
|
||||||
|
|
||||||
# Require at least 5 hours between posts (to prevent duplicates)
|
# Only post if:
|
||||||
if time_since_last_stats.total_seconds() < 5 * 3600:
|
# 1. It's a different day AND
|
||||||
return False
|
# 2. It's the target hour (12 PM) AND
|
||||||
|
# 3. It's within the first 5 minutes of that hour
|
||||||
# Check if we're in a target hour and within the first 5 minutes of that hour
|
if is_different_day and current_hour == target_hour and current_minute < 5:
|
||||||
if current_hour in target_hours and current_minute < 5:
|
logging.info(f"Posting daily stats at {current_hour}:{current_minute}")
|
||||||
# Ensure we haven't already posted for this window
|
self.last_daily_stats = now
|
||||||
last_stats_hour = self.last_daily_stats.hour
|
return True
|
||||||
if current_hour != last_stats_hour or (now.date() > self.last_daily_stats.date()):
|
|
||||||
logging.info(f"Posting daily stats at {current_hour}:{current_minute}")
|
|
||||||
self.last_daily_stats = now
|
|
||||||
return True
|
|
||||||
else:
|
else:
|
||||||
# First time - post if we're near one of our target hours
|
# First time - post if we're at the target hour
|
||||||
if current_hour in target_hours and current_minute < 5:
|
if current_hour == target_hour and current_minute < 5:
|
||||||
logging.info(f"First time posting daily stats at {current_hour}:{current_minute}")
|
logging.info(f"First time posting daily stats at {current_hour}:{current_minute}")
|
||||||
self.last_daily_stats = now
|
self.last_daily_stats = now
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _generate_daily_stats(self, metrics):
|
def _generate_daily_stats(self, metrics):
|
||||||
|
Loading…
Reference in New Issue
Block a user