Updated computed_timstamp. Changed approach
This commit is contained in:
@@ -201,30 +201,28 @@ class ScheduledInsightsGrabber:
|
||||
|
||||
def _compute_timestamp(
|
||||
self,
|
||||
date_preset: str,
|
||||
date_start_str: Optional[str],
|
||||
account_timezone: str
|
||||
) -> datetime:
|
||||
"""
|
||||
Compute the appropriate timestamp for storing insights data.
|
||||
|
||||
For 'today': Use current time (data is live, constantly updating)
|
||||
For historical presets (yesterday, etc.): Use noon of that date in the account's timezone,
|
||||
then convert to UTC. This ensures the data point falls on the correct day when plotted.
|
||||
Always uses noon of the data's date in the account's timezone, then converts to UTC.
|
||||
This ensures consistent timestamps regardless of when data was fetched:
|
||||
- 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:
|
||||
date_preset: The date preset used ('today', 'yesterday', etc.)
|
||||
date_start_str: The date_start from Meta API (ISO format: 'YYYY-MM-DD')
|
||||
account_timezone: Account timezone name (e.g., 'America/Los_Angeles')
|
||||
|
||||
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:
|
||||
try:
|
||||
# Parse the date
|
||||
@@ -445,8 +443,8 @@ class ScheduledInsightsGrabber:
|
||||
if date_start_str and date_start_value is None:
|
||||
date_start_value = date.fromisoformat(date_start_str)
|
||||
|
||||
# Compute appropriate timestamp based on date_preset and account timezone
|
||||
timestamp = self._compute_timestamp(date_preset, date_start_str, account_timezone)
|
||||
# Compute appropriate timestamp based on date_start and account timezone
|
||||
timestamp = self._compute_timestamp(date_start_str, account_timezone)
|
||||
|
||||
await self.db.insert_account_insights(
|
||||
time=timestamp,
|
||||
|
||||
Reference in New Issue
Block a user