From e5d5d82b2c9f2625f8a01911f207fe00b7659694 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Wed, 22 Jan 2025 10:19:58 +0330 Subject: [PATCH 1/6] fix: remove redundant timezone conversion for analyzer_period in TCAnalyzer --- tc_analyzer_lib/tc_analyzer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tc_analyzer_lib/tc_analyzer.py b/tc_analyzer_lib/tc_analyzer.py index d1a2d46..f43a596 100644 --- a/tc_analyzer_lib/tc_analyzer.py +++ b/tc_analyzer_lib/tc_analyzer.py @@ -50,7 +50,7 @@ def __init__( self.platform_id = platform_id self.resources = resources - self.period = period + self.period = period.replace(tzinfo=timezone.utc) self.action = action self.window = window self.analyzer_config = analyzer_config @@ -105,7 +105,7 @@ async def run_once(self): action_config=self.action, window_config=self.window, analyzer_config=self.analyzer_config, - analyzer_period=self.period.replace(tzinfo=timezone.utc), + analyzer_period=self.period, ) ( member_activities_data, @@ -190,7 +190,7 @@ async def recompute(self): action_config=self.action, window_config=self.window, analyzer_config=self.analyzer_config, - analyzer_period=self.period.replace(tzinfo=timezone.utc), + analyzer_period=self.period, ) ( member_activities_data, From e78e9f98be7e911e50d0791947c5fc85bdd63dd9 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Wed, 22 Jan 2025 10:20:35 +0330 Subject: [PATCH 2/6] feat: bump lib version! --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fa24cbb..1b8d1a4 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="tc-analyzer-lib", - version="1.4.15", + version="1.4.16", author="Mohammad Amin Dadgar, TogetherCrew", maintainer="Mohammad Amin Dadgar", maintainer_email="dadgaramin96@gmail.com", From 6c89a011e27770b0f08161c96e491e649b70c342 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Wed, 22 Jan 2025 10:28:30 +0330 Subject: [PATCH 3/6] feat: add timezone support to analytics date calculations in Heatmaps --- tc_analyzer_lib/metrics/heatmaps/heatmaps.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tc_analyzer_lib/metrics/heatmaps/heatmaps.py b/tc_analyzer_lib/metrics/heatmaps/heatmaps.py index a0c554f..256a0c4 100644 --- a/tc_analyzer_lib/metrics/heatmaps/heatmaps.py +++ b/tc_analyzer_lib/metrics/heatmaps/heatmaps.py @@ -1,5 +1,5 @@ import logging -from datetime import date, datetime, timedelta +from datetime import date, datetime, timedelta, timezone from typing import Any from tc_analyzer_lib.metrics.heatmaps import AnalyticsHourly, AnalyticsRaw @@ -69,7 +69,7 @@ async def start( if last_date is None or from_start: analytics_date = self.period else: - analytics_date = last_date + timedelta(days=1) + analytics_date = last_date.replace(tzinfo=timezone.utc) + timedelta(days=1) # in order to skip bots bot_ids = [] @@ -81,10 +81,11 @@ async def start( heatmaps_results = [] index = 0 - max_index = (analytics_date - datetime.now()).days - while analytics_date.date() < datetime.now().date(): + today = datetime.now(tz=timezone.utc) + max_index = (analytics_date - today).days + while analytics_date.date() < today.date(): start_day = analytics_date.replace( - hour=0, minute=0, second=0, microsecond=0 + hour=0, minute=0, second=0, microsecond=0, tzinfo=timezone.utc ) end_day = start_day + timedelta(days=1) logging.info( From 462bc32de0e9b8922b9a6ce4631bd7982fd533bc Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Wed, 22 Jan 2025 10:33:12 +0330 Subject: [PATCH 4/6] fix: update timezone handling for db_analysis_end_date and period in TCAnalyzer --- tc_analyzer_lib/algorithms/member_activity_history.py | 2 +- tc_analyzer_lib/tc_analyzer.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tc_analyzer_lib/algorithms/member_activity_history.py b/tc_analyzer_lib/algorithms/member_activity_history.py index 72505d2..eb4941e 100644 --- a/tc_analyzer_lib/algorithms/member_activity_history.py +++ b/tc_analyzer_lib/algorithms/member_activity_history.py @@ -107,7 +107,7 @@ def check_past_history( db_analysis_end_date = None if db_analysis_end_date: - db_analysis_end_date = db_analysis_end_date.replace(tzinfo=timezone.utc) + db_analysis_end_date = db_analysis_end_date.astimezone(tz=timezone.utc) new_date_range: list[datetime] # if for the requested date_range, its results were available in db diff --git a/tc_analyzer_lib/tc_analyzer.py b/tc_analyzer_lib/tc_analyzer.py index f43a596..73d3f34 100644 --- a/tc_analyzer_lib/tc_analyzer.py +++ b/tc_analyzer_lib/tc_analyzer.py @@ -50,7 +50,7 @@ def __init__( self.platform_id = platform_id self.resources = resources - self.period = period.replace(tzinfo=timezone.utc) + self.period = period.astimezone(tz=timezone.utc) self.action = action self.window = window self.analyzer_config = analyzer_config From 9d26e07dbc63ffbeba52dbf42da5ab0367c99701 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Wed, 22 Jan 2025 10:48:17 +0330 Subject: [PATCH 5/6] fix: ensure timezone awareness for analytics date calculations in Heatmaps --- tc_analyzer_lib/metrics/heatmaps/heatmaps.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tc_analyzer_lib/metrics/heatmaps/heatmaps.py b/tc_analyzer_lib/metrics/heatmaps/heatmaps.py index 256a0c4..3b6e598 100644 --- a/tc_analyzer_lib/metrics/heatmaps/heatmaps.py +++ b/tc_analyzer_lib/metrics/heatmaps/heatmaps.py @@ -67,9 +67,11 @@ async def start( analytics_date: datetime if last_date is None or from_start: - analytics_date = self.period + # Ensure self.period is offset-aware + analytics_date = self.period.replace(tzinfo=timezone.utc) if self.period.tzinfo is None else self.period else: - analytics_date = last_date.replace(tzinfo=timezone.utc) + timedelta(days=1) + # Ensure last_date is offset-aware and add a day + analytics_date = last_date.astimezone(timezone.utc) + timedelta(days=1) # in order to skip bots bot_ids = [] From 54bdf469f66c649d3b543cb60e3b294a58bd2b69 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Wed, 22 Jan 2025 10:55:54 +0330 Subject: [PATCH 6/6] fix: black linter issue! --- tc_analyzer_lib/metrics/heatmaps/heatmaps.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tc_analyzer_lib/metrics/heatmaps/heatmaps.py b/tc_analyzer_lib/metrics/heatmaps/heatmaps.py index 3b6e598..7889d9d 100644 --- a/tc_analyzer_lib/metrics/heatmaps/heatmaps.py +++ b/tc_analyzer_lib/metrics/heatmaps/heatmaps.py @@ -68,7 +68,11 @@ async def start( analytics_date: datetime if last_date is None or from_start: # Ensure self.period is offset-aware - analytics_date = self.period.replace(tzinfo=timezone.utc) if self.period.tzinfo is None else self.period + analytics_date = ( + self.period.replace(tzinfo=timezone.utc) + if self.period.tzinfo is None + else self.period + ) else: # Ensure last_date is offset-aware and add a day analytics_date = last_date.astimezone(timezone.utc) + timedelta(days=1)