Updated computed_timstamp. Changed approach

This commit is contained in:
Jonas Linter
2025-10-21 18:16:31 +02:00
parent ec10ca51e0
commit 794599a454

View File

@@ -201,30 +201,28 @@ class ScheduledInsightsGrabber:
def _compute_timestamp( def _compute_timestamp(
self, self,
date_preset: str,
date_start_str: Optional[str], date_start_str: Optional[str],
account_timezone: str account_timezone: str
) -> datetime: ) -> datetime:
""" """
Compute the appropriate timestamp for storing insights data. Compute the appropriate timestamp for storing insights data.
For 'today': Use current time (data is live, constantly updating) Always uses noon of the data's date in the account's timezone, then converts to UTC.
For historical presets (yesterday, etc.): Use noon of that date in the account's timezone, This ensures consistent timestamps regardless of when data was fetched:
then convert to UTC. This ensures the data point falls on the correct day when plotted. - Today's data (fetched multiple times): all share same timestamp (today at noon)
- Yesterday's data: timestamp at yesterday noon
- Use fetched_at column to track when data was actually retrieved
This design makes time-series queries simple (one timestamp per date) while
preserving intraday evolution via fetched_at.
Args: Args:
date_preset: The date preset used ('today', 'yesterday', etc.)
date_start_str: The date_start from Meta API (ISO format: 'YYYY-MM-DD') date_start_str: The date_start from Meta API (ISO format: 'YYYY-MM-DD')
account_timezone: Account timezone name (e.g., 'America/Los_Angeles') account_timezone: Account timezone name (e.g., 'America/Los_Angeles')
Returns: Returns:
Timestamp in UTC Timestamp in UTC (noon of the data date)
""" """
# For 'today', use current time since data is live
if date_preset == "today":
return datetime.now(timezone.utc)
# For historical data, use noon of that date in the account's timezone
if date_start_str: if date_start_str:
try: try:
# Parse the date # Parse the date
@@ -445,8 +443,8 @@ class ScheduledInsightsGrabber:
if date_start_str and date_start_value is None: if date_start_str and date_start_value is None:
date_start_value = date.fromisoformat(date_start_str) date_start_value = date.fromisoformat(date_start_str)
# Compute appropriate timestamp based on date_preset and account timezone # Compute appropriate timestamp based on date_start and account timezone
timestamp = self._compute_timestamp(date_preset, date_start_str, account_timezone) timestamp = self._compute_timestamp(date_start_str, account_timezone)
await self.db.insert_account_insights( await self.db.insert_account_insights(
time=timestamp, time=timestamp,