AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): update and fix weekly summary email (#10343)
Nicholas Tindle21faf1b6773a
diff --git a/autogpt_platform/backend/backend/data/generate_data.py b/autogpt_platform/backend/backend/data/generate_data.pynew file mode 100644index 0000000000..edaa2772c8--- /dev/null+++ b/autogpt_platform/backend/backend/data/generate_data.py@@ -0,0 +1,109 @@+import logging+from collections import defaultdict+from datetime import datetime++from prisma.enums import AgentExecutionStatus++from backend.data.execution import get_graph_executions+from backend.data.graph import get_graph_metadata+from backend.data.model import UserExecutionSummaryStats+from backend.server.v2.store.exceptions import DatabaseError+from backend.util.logging import TruncatedLogger++logger = TruncatedLogger(logging.getLogger(__name__), prefix="[SummaryData]")+++async def get_user_execution_summary_data(+ user_id: str, start_time: datetime, end_time: datetime+) -> UserExecutionSummaryStats:+ """Gather all summary data for a user in a time range.++ This function fetches graph executions once and aggregates all required+ statistics in a single pass for efficiency.+ """+ try:+ # Fetch graph executions once+ executions = await get_graph_executions(+ user_id=user_id,+ created_time_gte=start_time,+ created_time_lte=end_time,+ )++ # Initialize aggregation variables+ total_credits_used = 0.0+ total_executions = len(executions)+ successful_runs = 0+ failed_runs = 0+ terminated_runs = 0+ execution_times = []+ agent_usage = defaultdict(int)+ cost_by_graph_id = defaultdict(float)++ # Single pass through executions to aggregate all stats+ for execution in executions:+ # Count execution statuses (including TERMINATED as failed)+ if execution.status == AgentExecutionStatus.COMPLETED:+ successful_runs += 1+ elif execution.status == AgentExecutionStatus.FAILED:+ failed_runs += 1+ elif execution.status == AgentExecutionStatus.TERMINATED:+ terminated_runs += 1++ # Aggregate costs from stats+ if execution.stats and hasattr(execution.stats, "cost"):+ cost_in_dollars = execution.stats.cost / 100+ total_credits_used += cost_in_dollars+ cost_by_graph_id[execution.graph_id] += cost_in_dollars++ # Collect execution times+ if execution.stats and hasattr(execution.stats, "duration"):+ execution_times.append(execution.stats.duration)++ # Count agent usage+ agent_usage[execution.graph_id] += 1++ # Calculate derived stats+ total_execution_time = sum(execution_times)+ average_execution_time = (+ total_execution_time / len(execution_times) if execution_times else 0+ )++ # Find most used agent+ most_used_agent = "No agents used"+ if agent_usage:+ most_used_agent_id = max(agent_usage, key=lambda k: agent_usage[k])+ try:+ graph_meta = await get_graph_metadata(graph_id=most_used_agent_id)+ most_used_agent = (+ graph_meta.name if graph_meta else f"Agent {most_used_agent_id[:8]}"+ )+ except Exception:+ logger.warning(f"Could not get metadata for graph {most_used_agent_id}")+ most_used_agent = f"Agent {most_used_agent_id[:8]}"++ # Convert graph_ids to agent names for cost breakdown+ cost_breakdown = {}+ for graph_id, cost in cost_by_graph_id.items():+ try:+ graph_meta = await get_graph_metadata(graph_id=graph_id)+ agent_name = graph_meta.name if graph_meta else f"Agent {graph_id[:8]}"+ except Exception:+ logger.warning(f"Could not get metadata for graph {graph_id}")+ agent_name = f"Agent {graph_id[:8]}"+ cost_breakdown[agent_name] = cost++ # Build the summary stats object (include terminated runs as failed)+ return UserExecutionSummaryStats(+ total_credits_used=total_credits_used,+ total_executions=total_executions,+ successful_runs=successful_runs,+ failed_runs=failed_runs + terminated_runs,+ most_used_agent=most_used_agent,+ total_execution_time=total_execution_time,+ average_execution_time=average_execution_time,+ cost_breakdown=cost_breakdown,+ )++ except Exception as e:+ logger.error(f"Failed to get user summary data: {e}")+ raise DatabaseError(f"Failed to get user summary data: {e}") from ediff --git a/autogpt_platform/backend/backend/data/model.py b/autogpt_platform/backend/backend/data/model.pyindex fa9e55ce0b..1b016a9d91 100644--- a/autogpt_platform/backend/backend/data/model.py+++ b/autogpt_platform/backend/backend/data/model.py@@ -821,3 +821,21 @@ class GraphExecutionStats(BaseModel):activity_status: Optional[str] = Field(default=None, description="AI-generated summary of what the agent did")+++class UserExecutionSummaryStats(BaseModel):+ """Summary of user statistics for a specific user."""++ model_config = ConfigDict(+ extra="allow",+ arbitrary_types_allowed=True,+ )++ total_credits_used: float = Field(default=0)+ total_executions: int = Field(default=0)+ successful_runs: int = Field(default=0)+ failed_runs: int = Field(default=0)+ most_used_agent: str = Field(default="")+ total_execution_time: float = Field(default=0)+ average_execution_time: float = Field(default=0)+ cost_breakdown: dict[str, float] = Field(default_factory=dict)diff --git a/autogpt_platform/backend/backend/executor/database.py b/autogpt_platform/backend/backend/executor/database.pyindex f02a050a63..e59d03bb5e 100644--- a/autogpt_platform/backend/backend/executor/database.py+++ b/autogpt_platform/backend/backend/executor/database.py@@ -20,6 +20,7 @@ from backend.data.execution import (upsert_execution_input,upsert_execution_output,)+from backend.data.generate_data import get_user_execution_summary_datafrom backend.data.graph import (get_connected_output_nodes,get_graph,@@ -144,6 +145,9 @@ class DatabaseManager(AppService):get_user_notification_oldest_message_in_batch)+ # Summary data - async+ get_user_execution_summary_data = _(get_user_execution_summary_data)+class DatabaseManagerClient(AppServiceClient):d = DatabaseManager@@ -169,6 +173,9 @@ class DatabaseManagerClient(AppServiceClient):spend_credits = _(d.spend_credits)get_credits = _(d.get_credits)+ # Summary data - async+ get_user_execution_summary_data = _(d.get_user_execution_summary_data)+# Block error monitoringget_block_error_stats = _(d.get_block_error_stats)@@ -215,3 +222,6 @@ class DatabaseManagerAsyncClient(AppServiceClient):get_user_notification_oldest_message_in_batch = (d.get_user_notification_oldest_message_in_batch)++ # Summary data+ get_user_execution_summary_data = d.get_user_execution_summary_datadiff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.pyindex 50b165045c..87ca0a654e 100644--- a/autogpt_platform/backend/backend/executor/scheduler.py+++ b/autogpt_platform/backend/backend/executor/scheduler.py@@ -305,9 +305,10 @@ class Scheduler(AppService):if self.register_system_tasks:# Notification PROCESS WEEKLY SUMMARY+ # Runs every Monday at 9 AM UTCself.scheduler.add_job(process_weekly_summary,- CronTrigger.from_crontab("0 * * * *"),+ CronTrigger.from_crontab("0 9 * * 1"),id="process_weekly_summary",kwargs={},replace_existing=True,diff --git a/autogpt_platform/backend/backend/notifications/notifications.py b/autogpt_platform/backend/backend/notifications/notifications.pyindex 2036891cda..5d2ae39bdf 100644--- a/autogpt_platform/backend/backend/notifications/notifications.py+++ b/autogpt_platform/backend/backend/notifications/notifications.py@@ -223,10 +223,14 @@ class NotificationManager(AppService):processed_count = 0current_time = datetime.now(tz=timezone.utc)start_time = current_time - timedelta(days=7)+ logger.info(+ f"Querying for active users between {start_time} and {current_time}"+ )users = await get_database_manager_async_client().get_active_user_ids_in_timerange(end_time=current_time.isoformat(),start_time=start_time.isoformat(),)+ logger.info(f"Found {len(users)} active users in the last 7 days")for user in users:await self._queue_scheduled_notification(SummaryParamsEventModel(@@ -384,10 +388,13 @@ class NotificationManager(AppService):async def _queue_scheduled_notification(self, event: SummaryParamsEventModel):"""Queue a scheduled notification - exposed method for other services to call"""try:- logger.debug(f"Received Request to queue scheduled notification {event=}")+ logger.info(+ f"Queueing scheduled notification type={event.type} user_id={event.user_id}"+ )exchange = "notifications"routing_key = get_routing_key(event.type)+ logger.info(f"Using routing key: {routing_key}")# Publish to RabbitMQawait self.rabbit.publish_message(@@ -395,6 +402,7 @@ class NotificationManager(AppService):message=event.model_dump_json(),exchange=next(ex for ex in EXCHANGES if ex.name == exchange),)+ logger.info(f"Successfully queued notification for user {event.user_id}")except Exception as e:logger.exception(f"Error queueing notification: {e}")@@ -416,85 +424,99 @@ class NotificationManager(AppService):# only if both are true, should we email this personreturn validated_email and preference- def _gather_summary_data(+ async def _gather_summary_data(self, user_id: str, event_type: NotificationType, params: BaseSummaryParams) -> BaseSummaryData:"""Gathers the data to build a summary notification"""logger.info(- f"Gathering summary data for {user_id} and {event_type} wiht {params=}"+ f"Gathering summary data for {user_id} and {event_type} with {params=}")- # total_credits_used = self.run_and_wait(- # get_total_credits_used(user_id, start_time, end_time)- # )-- # total_executions = self.run_and_wait(- # get_total_executions(user_id, start_time, end_time)- # )-- # most_used_agent = self.run_and_wait(- # get_most_used_agent(user_id, start_time, end_time)- # )-- # execution_times = self.run_and_wait(- # get_execution_time(user_id, start_time, end_time)- # )-- # runs = self.run_and_wait(- # get_runs(user_id, start_time, end_time)- # )- total_credits_used = 3.0- total_executions = 2- most_used_agent = {"name": "Some"}- execution_times = [1, 2, 3]- runs = [{"status": "COMPLETED"}, {"status": "FAILED"}]-- successful_runs = len([run for run in runs if run["status"] == "COMPLETED"])- failed_runs = len([run for run in runs if run["status"] != "COMPLETED"])- average_execution_time = (- sum(execution_times) / len(execution_times) if execution_times else 0- )- # cost_breakdown = self.run_and_wait(- # get_cost_breakdown(user_id, start_time, end_time)- # )-- cost_breakdown = {- "agent1": 1.0,- "agent2": 2.0,- }-- if event_type == NotificationType.DAILY_SUMMARY and isinstance(- params, DailySummaryParams- ):- return DailySummaryData(- total_credits_used=total_credits_used,- total_executions=total_executions,- most_used_agent=most_used_agent["name"],- total_execution_time=sum(execution_times),- successful_runs=successful_runs,- failed_runs=failed_runs,- average_execution_time=average_execution_time,- cost_breakdown=cost_breakdown,- date=params.date,- )- elif event_type == NotificationType.WEEKLY_SUMMARY and isinstance(- params, WeeklySummaryParams- ):- return WeeklySummaryData(- total_credits_used=total_credits_used,- total_executions=total_executions,- most_used_agent=most_used_agent["name"],- total_execution_time=sum(execution_times),- successful_runs=successful_runs,- failed_runs=failed_runs,- average_execution_time=average_execution_time,- cost_breakdown=cost_breakdown,- start_date=params.start_date,- end_date=params.end_date,+ try:+ # Get summary data from the database+ summary_data = await get_database_manager_async_client().get_user_execution_summary_data(+ user_id=user_id,+ start_time=params.start_date,+ end_time=params.end_date,)- else:- raise ValueError("Invalid event type or params")++ # Extract data from summary+ total_credits_used = summary_data.total_credits_used+ total_executions = summary_data.total_executions+ most_used_agent = summary_data.most_used_agent+ successful_runs = summary_data.successful_runs+ failed_runs = summary_data.failed_runs+ total_execution_time = summary_data.total_execution_time+ average_execution_time = summary_data.average_execution_time+ cost_breakdown = summary_data.cost_breakdown++ if event_type == NotificationType.DAILY_SUMMARY and isinstance(+ params, DailySummaryParams+ ):+ return DailySummaryData(+ total_credits_used=total_credits_used,+ total_executions=total_executions,+ most_used_agent=most_used_agent,+ total_execution_time=total_execution_time,+ successful_runs=successful_runs,+ failed_runs=failed_runs,+ average_execution_time=average_execution_time,+ cost_breakdown=cost_breakdown,+ date=params.date,+ )+ elif event_type == NotificationType.WEEKLY_SUMMARY and isinstance(+ params, WeeklySummaryParams+ ):+ return WeeklySummaryData(+ total_credits_used=total_credits_used,+ total_executions=total_executions,+ most_used_agent=most_used_agent,+ total_execution_time=total_execution_time,+ successful_runs=successful_runs,+ failed_runs=failed_runs,+ average_execution_time=average_execution_time,+ cost_breakdown=cost_breakdown,+ start_date=params.start_date,+ end_date=params.end_date,+ )+ else:+ raise ValueError("Invalid event type or params")++ except Exception as e:+ logger.error(f"Failed to gather summary data: {e}")+ # Return sensible defaults in case of error+ if event_type == NotificationType.DAILY_SUMMARY and isinstance(+ params, DailySummaryParams+ ):+ return DailySummaryData(+ total_credits_used=0.0,+ total_executions=0,+ most_used_agent="No data available",+ total_execution_time=0.0,+ successful_runs=0,+ failed_runs=0,+ average_execution_time=0.0,+ cost_breakdown={},+ date=params.date,+ )+ elif event_type == NotificationType.WEEKLY_SUMMARY and isinstance(+ params, WeeklySummaryParams+ ):+ return WeeklySummaryData(+ total_credits_used=0.0,+ total_executions=0,+ most_used_agent="No data available",+ total_execution_time=0.0,+ successful_runs=0,+ failed_runs=0,+ average_execution_time=0.0,+ cost_breakdown={},+ start_date=params.start_date,+ end_date=params.end_date,+ )+ else:+ raise ValueError("Invalid event type or params") from easync def _should_batch(self, user_id: str, event_type: NotificationType, event: NotificationEventModel@@ -764,7 +786,7 @@ class NotificationManager(AppService):)return True- summary_data = self._gather_summary_data(+ summary_data = await self._gather_summary_data(event.user_id, event.type, model.data)diff --git a/autogpt_platform/backend/backend/notifications/templates/weekly_summary.html.jinja2 b/autogpt_platform/backend/backend/notifications/templates/weekly_summary.html.jinja2index c561851a60..6de381a946 100644--- a/autogpt_platform/backend/backend/notifications/templates/weekly_summary.html.jinja2+++ b/autogpt_platform/backend/backend/notifications/templates/weekly_summary.html.jinja2@@ -5,23 +5,64 @@ data.start_date: the start date of the summarydata.end_date: the end date of the summarydata.total_credits_used: the total credits used during the summarydata.total_executions: the total number of executions during the summary-data.most_used_agent: the most used agent's nameduring the summary+data.most_used_agent: the most used agent's name during the summarydata.total_execution_time: the total execution time during the summarydata.successful_runs: the total number of successful runs during the summarydata.failed_runs: the total number of failed runs during the summarydata.average_execution_time: the average execution time during the summary-data.cost_breakdown: the cost breakdown during the summary+data.cost_breakdown: the cost breakdown during the summary (dict mapping agent names to credit amounts)#}-<h1>Weekly Summary</h1>+<h1 style="color: #5D23BB; font-size: 32px; font-weight: 600; margin-bottom: 25px; margin-top: 0;">+ Weekly Summary+</h1>-<p>Start Date: {{ data.start_date }}</p>-<p>End Date: {{ data.end_date }}</p>-<p>Total Credits Used: {{ data.total_credits_used }}</p>-<p>Total Executions: {{ data.total_executions }}</p>-<p>Most Used Agent: {{ data.most_used_agent }}</p>-<p>Total Execution Time: {{ data.total_execution_time }}</p>-<p>Successful Runs: {{ data.successful_runs }}</p>-<p>Failed Runs: {{ data.failed_runs }}</p>-<p>Average Execution Time: {{ data.average_execution_time }}</p>-<p>Cost Breakdown: {{ data.cost_breakdown }}</p>+<h2 style="color: #070629; font-size: 24px; font-weight: 500; margin-bottom: 20px;">+ Your Agent Activity: {{ data.start_date.strftime('%B %-d') }} – {{ data.end_date.strftime('%B %-d') }}+</h2>++<div style="background-color: #ffffff; border-radius: 8px; padding: 20px; margin-bottom: 25px;">+ <ul style="list-style-type: disc; padding-left: 20px; margin: 0;">+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Total Executions:</strong> {{ data.total_executions }}+ </li>+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Total Credits Used:</strong> {{ data.total_credits_used|format("%.2f") }}+ </li>+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Total Execution Time:</strong> {{ data.total_execution_time|format("%.1f") }} seconds+ </li>+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Successful Runs:</strong> {{ data.successful_runs }}+ </li>+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Failed Runs:</strong> {{ data.failed_runs }}+ </li>+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Average Execution Time:</strong> {{ data.average_execution_time|format("%.1f") }} seconds+ </li>+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Most Used Agent:</strong> {{ data.most_used_agent }}+ </li>+ {% if data.cost_breakdown %}+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 8px;">+ <strong>Cost Breakdown:</strong>+ <ul style="list-style-type: disc; padding-left: 40px; margin-top: 8px;">+ {% for agent_name, credits in data.cost_breakdown.items() %}+ <li style="font-size: 16px; line-height: 1.8; margin-bottom: 4px;">+ {{ agent_name }}: {{ credits|format("%.2f") }} credits+ </li>+ {% endfor %}+ </ul>+ </li>+ {% endif %}+ </ul>+</div>++<p style="font-size: 16px; line-height: 165%; margin-top: 20px; margin-bottom: 10px;">+ Thank you for being a part of the AutoGPT community! 🎉+</p>++<p style="font-size: 16px; line-height: 165%; margin-bottom: 0;">+ Join the conversation on <a href="https://discord.gg/autogpt" style="color: #4285F4; text-decoration: underline;">Discord here</a>.+</p>\ No newline at end of file