From 794599a45452d9597ef48fe3259556db7d105080 Mon Sep 17 00:00:00 2001 From: Jonas Linter Date: Tue, 21 Oct 2025 18:16:31 +0200 Subject: [PATCH] Updated computed_timstamp. Changed approach --- src/meta_api_grabber/scheduled_grabber.py | 24 +++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/meta_api_grabber/scheduled_grabber.py b/src/meta_api_grabber/scheduled_grabber.py index 08b34bc..4132120 100644 --- a/src/meta_api_grabber/scheduled_grabber.py +++ b/src/meta_api_grabber/scheduled_grabber.py @@ -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,