diff --git a/application.py b/application.py index 24f3c38c..ba4edba6 100644 --- a/application.py +++ b/application.py @@ -31,7 +31,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator: # type: ignore "cron", hour="4", minute="15", - day_of_week="wed", + # day_of_week="wed", ) scheduler.start() diff --git a/captn/captn_agents/backend/teams/_weekly_analysis_team.py b/captn/captn_agents/backend/teams/_weekly_analysis_team.py index 1cf266db..1ef5c763 100644 --- a/captn/captn_agents/backend/teams/_weekly_analysis_team.py +++ b/captn/captn_agents/backend/teams/_weekly_analysis_team.py @@ -765,6 +765,8 @@ def _guidelines(self) -> str: "Remove 'Free' keyword because it is not performing well" is specific enough. "Remove the headline 'New product' and replace it with 'Very New product' in the 'Adgroup 1'" is specific enough. +Messages within the 'proposed_user_actions' are the ONLY messages the client will see. So make sure to include all the necessary information in them. +e.g. Do NOT suggest 'Update the ad copy with the suggested headlines and descriptions for better engagement.' because the client will not know which ad copy you are talking about and what changes you want to make. 12. There is a list of commands which you are able to execute in the 'Commands' section. You can NOT execute anything else, so do not suggest changes which you can NOT perform. @@ -1029,6 +1031,28 @@ def _validate_conversation_and_send_email( ) +def _get_day_of_week(date_str: str) -> str: + # Parse the date string into a datetime object + date_obj = datetime.strptime(date_str, "%Y-%m-%d") + + # Get the day of the week as an integer (0=Monday, 6=Sunday) + day_of_week_num = date_obj.weekday() + + # Map the integer to the day name + days = [ + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday", + ] + day_of_week_name = days[day_of_week_num] + + return day_of_week_name + + def execute_weekly_analysis( send_only_to_emails: Optional[List[str]] = None, date: Optional[str] = None, @@ -1038,7 +1062,12 @@ def execute_weekly_analysis( if date is None: date = (datetime.today().date() - timedelta(1)).isoformat() print("Starting weekly analysis.") - id_email_dict = json.loads(get_user_ids_and_emails()) + if send_only_to_emails is not None: + day_of_week = None + else: + day_of_week = _get_day_of_week(date) + day_of_week = "Wednesday" + id_email_dict = json.loads(get_user_ids_and_emails(day_of_week=day_of_week)) # if send_only_to_emails is None: # send_only_to_emails = ["robert@airt.ai", "harish@airt.ai"] diff --git a/captn/google_ads/client.py b/captn/google_ads/client.py index 9735557e..a8c974cc 100644 --- a/captn/google_ads/client.py +++ b/captn/google_ads/client.py @@ -133,8 +133,13 @@ def execute_query( return str(response_json) -def get_user_ids_and_emails() -> str: - response = requests_get(f"{BASE_URL}/get-user-ids-and-emails", timeout=60) +def get_user_ids_and_emails(day_of_week: Optional[str] = None) -> str: + params = { + "day_of_week_created": day_of_week, + } + response = requests_get( + f"{BASE_URL}/get-user-ids-and-emails", params=params, timeout=60 + ) if not response.ok: raise ValueError(response.content) return response.json() # type: ignore[no-any-return] diff --git a/google_ads/application.py b/google_ads/application.py index 8ccffe34..184f2130 100644 --- a/google_ads/application.py +++ b/google_ads/application.py @@ -44,10 +44,15 @@ } -async def get_users() -> Any: +async def get_users(day_of_week_created: Optional[str] = None) -> Any: wasp_db_url = await get_wasp_db_url() + if day_of_week_created: + query = f"""SELECT * FROM "User" +WHERE TO_CHAR("createdAt", 'Day') = '{day_of_week_created}'""" # nosec: [B608] + else: + query = 'SELECT * from "User"' async with get_db_connection(db_url=wasp_db_url) as db: - users = await db.query_raw('SELECT * from "User"') + users = await db.query_raw(query) return users @@ -337,8 +342,8 @@ async def search( # Route 5: Fetch user's emails @router.get("/get-user-ids-and-emails") -async def get_user_ids_and_emails() -> str: - users = await get_users() +async def get_user_ids_and_emails(day_of_week_created: Optional[str] = None) -> str: + users = await get_users(day_of_week_created=day_of_week_created) id_email_dict = {user["id"]: user["email"] for user in users} return json.dumps(id_email_dict) diff --git a/pyproject.toml b/pyproject.toml index d60d41a4..509cef20 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,7 +93,7 @@ agents = [ "fastapi==0.111.0", "APScheduler==3.10.4", "prisma==0.13.1", - "google-ads==23.1.0", + "google-ads==24.1.0", "httpx==0.27.0", "uvicorn==0.30.1", "python-dotenv==1.0.1", diff --git a/tests/ci/captn/captn_agents/backend/teams/test_weekly_analysis_team.py b/tests/ci/captn/captn_agents/backend/teams/test_weekly_analysis_team.py index 7cfdea19..b4a18de2 100644 --- a/tests/ci/captn/captn_agents/backend/teams/test_weekly_analysis_team.py +++ b/tests/ci/captn/captn_agents/backend/teams/test_weekly_analysis_team.py @@ -28,6 +28,7 @@ _add_metrics_message, _check_if_any_campaign_exists, _create_date_query, + _get_day_of_week, _update_chat_message_and_send_email, _update_message_and_campaigns_template, calculate_metrics_change, @@ -1343,6 +1344,16 @@ def test_get_weekly_report_when_there_are_no_campaigns() -> None: ) +def test_get_day_of_week() -> None: + assert _get_day_of_week("2024-06-09") == "Sunday" + assert _get_day_of_week("2024-06-10") == "Monday" + assert _get_day_of_week("2024-06-11") == "Tuesday" + assert _get_day_of_week("2024-06-12") == "Wednesday" + assert _get_day_of_week("2024-06-13") == "Thursday" + assert _get_day_of_week("2024-06-14") == "Friday" + assert _get_day_of_week("2024-06-15") == "Saturday" + + class TestWeeklyAnalysisTeam: @pytest.fixture(autouse=True) def setup(self) -> Iterator[None]: