Skip to content

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.

feat(backend): status counts and duration totals in execution cost summary (#13869)

Abhimanyu Yadav1559f9ab74f2
diff --git a/autogpt_platform/backend/backend/data/execution_cost_summary.py b/autogpt_platform/backend/backend/data/execution_cost_summary.py
index 3526430fa4..948140c98e 100644
--- a/autogpt_platform/backend/backend/data/execution_cost_summary.py
+++ b/autogpt_platform/backend/backend/data/execution_cost_summary.py
@@ -29,6 +29,9 @@ class UserDailyCost(BaseModel):
date: date
cost_cents: int
run_count: int
+ success_count: int = 0
+ failed_count: int = 0
+ review_count: int = 0
class UserExecutionCostSummary(BaseModel):
@@ -36,6 +39,14 @@ class UserExecutionCostSummary(BaseModel):
run_count: int
billable_run_count: int
failed_cost_cents: int
+ success_run_count: int = 0
+ failed_run_count: int = 0
+ review_run_count: int = 0
+ total_duration_seconds: float = 0
+ # Denominator for "avg run duration": `run_count` also covers QUEUED /
+ # RUNNING rows and rows whose stats never got a `walltime`, which SUM
+ # silently skips — dividing by it biases the average downward.
+ duration_run_count: int = 0
by_agent: list[UserAgentCostRollup]
top_runs: list[UserTopRun]
daily: list[UserDailyCost]
@@ -44,6 +55,14 @@ class UserExecutionCostSummary(BaseModel):
_MAX_BY_AGENT_ROWS = 50
_MAX_TOP_RUNS = 50
+_BASE_WHERE = (
+ '"userId" = $1'
+ ' AND "isDeleted" = false'
+ ' AND "createdAt" >= $2::timestamp'
+ ' AND "createdAt" <= $3::timestamp'
+ " AND COALESCE((stats->>'is_dry_run')::boolean, false) = false"
+)
+
async def get_user_cost_summary(
*,
@@ -73,103 +92,143 @@ async def get_user_cost_summary(
if until is None:
until = now
- base_where = (
- '"userId" = $1'
- ' AND "isDeleted" = false'
- ' AND "createdAt" >= $2::timestamp'
- ' AND "createdAt" <= $3::timestamp'
- " AND COALESCE((stats->>'is_dry_run')::boolean, false) = false"
- )
params = (user_id, since, until)
-
- totals_rows, by_agent_rows, top_runs_rows, daily_rows = await asyncio.gather(
- query_raw_with_schema(
- "SELECT"
- " COALESCE(SUM((stats->>'cost')::numeric), 0)::bigint AS total_cents,"
- " COUNT(*)::bigint AS run_count,"
- " COUNT(*) FILTER (WHERE COALESCE((stats->>'cost')::numeric, 0) > 0)::bigint"
- " AS billable_run_count,"
- " COALESCE(SUM(CASE WHEN \"executionStatus\" IN ('FAILED', 'TERMINATED')"
- " THEN (stats->>'cost')::numeric ELSE 0 END), 0)::bigint"
- " AS failed_cost_cents"
- ' FROM {schema_prefix}"AgentGraphExecution"'
- f" WHERE {base_where}",
- *params,
- ),
- query_raw_with_schema(
- "SELECT"
- ' "agentGraphId" AS graph_id,'
- " COALESCE(SUM((stats->>'cost')::numeric), 0)::bigint AS cost_cents,"
- " COUNT(*)::bigint AS run_count"
- ' FROM {schema_prefix}"AgentGraphExecution"'
- f" WHERE {base_where}"
- ' GROUP BY "agentGraphId"'
- " ORDER BY cost_cents DESC"
- f" LIMIT {_MAX_BY_AGENT_ROWS}",
- *params,
- ),
- query_raw_with_schema(
- "SELECT"
- " id AS execution_id,"
- ' "agentGraphId" AS graph_id,'
- " COALESCE((stats->>'cost')::numeric, 0)::bigint AS cost_cents,"
- ' COALESCE("startedAt", "createdAt") AS started_at,'
- ' "executionStatus" AS status,'
- " COALESCE((stats->>'duration')::numeric, 0) AS duration_seconds,"
- " COALESCE((stats->>'node_error_count')::int, 0) AS node_error_count"
- ' FROM {schema_prefix}"AgentGraphExecution"'
- f" WHERE {base_where}"
- " AND COALESCE((stats->>'cost')::numeric, 0) > 0"
- ' ORDER BY cost_cents DESC, "createdAt" DESC'
- " LIMIT $4",
- *params,
- top_runs_limit,
- ),
- query_raw_with_schema(
- "SELECT"
- ' ("createdAt" AT TIME ZONE \'UTC\')::date AS "date",'
- " COALESCE(SUM((stats->>'cost')::numeric), 0)::bigint AS cost_cents,"
- " COUNT(*)::bigint AS run_count"
- ' FROM {schema_prefix}"AgentGraphExecution"'
- f" WHERE {base_where}"
- " GROUP BY (\"createdAt\" AT TIME ZONE 'UTC')::date"
- ' ORDER BY "date" ASC',
- *params,
- ),
+ totals, by_agent, top_runs, daily = await asyncio.gather(
+ _fetch_totals(params),
+ _fetch_by_agent(params),
+ _fetch_top_runs(params, top_runs_limit),
+ _fetch_daily(params),
)
-
- totals = totals_rows[0] if totals_rows else {}
return UserExecutionCostSummary(
total_cents=int(totals.get("total_cents") or 0),
run_count=int(totals.get("run_count") or 0),
billable_run_count=int(totals.get("billable_run_count") or 0),
failed_cost_cents=int(totals.get("failed_cost_cents") or 0),
- by_agent=[
- UserAgentCostRollup(
- graph_id=r["graph_id"],
- cost_cents=int(r.get("cost_cents") or 0),
- run_count=int(r.get("run_count") or 0),
- )
- for r in by_agent_rows
- ],
- top_runs=[
- UserTopRun(
- execution_id=r["execution_id"],
- graph_id=r["graph_id"],
- cost_cents=int(r.get("cost_cents") or 0),
- started_at=r["started_at"],
- status=AgentExecutionStatus(r["status"]),
- duration_seconds=float(r.get("duration_seconds") or 0),
- node_error_count=int(r.get("node_error_count") or 0),
- )
- for r in top_runs_rows
- ],
- daily=[
- UserDailyCost(
- date=r["date"],
- cost_cents=int(r.get("cost_cents") or 0),
- run_count=int(r.get("run_count") or 0),
- )
- for r in daily_rows
- ],
+ success_run_count=int(totals.get("success_run_count") or 0),
+ failed_run_count=int(totals.get("failed_run_count") or 0),
+ review_run_count=int(totals.get("review_run_count") or 0),
+ total_duration_seconds=float(totals.get("total_duration_seconds") or 0),
+ duration_run_count=int(totals.get("duration_run_count") or 0),
+ by_agent=by_agent,
+ top_runs=top_runs,
+ daily=daily,
+ )
+
+
+async def _fetch_totals(params: tuple[str, datetime, datetime]) -> dict:
+ rows = await query_raw_with_schema(
+ "SELECT"
+ " COALESCE(SUM((stats->>'cost')::numeric), 0)::bigint AS total_cents,"
+ " COUNT(*)::bigint AS run_count,"
+ " COUNT(*) FILTER (WHERE COALESCE((stats->>'cost')::numeric, 0) > 0)::bigint"
+ " AS billable_run_count,"
+ " COALESCE(SUM(CASE WHEN \"executionStatus\" IN ('FAILED', 'TERMINATED')"
+ " THEN (stats->>'cost')::numeric ELSE 0 END), 0)::bigint"
+ " AS failed_cost_cents,"
+ " COUNT(*) FILTER (WHERE \"executionStatus\" = 'COMPLETED')::bigint"
+ " AS success_run_count,"
+ " COUNT(*) FILTER (WHERE \"executionStatus\" IN ('FAILED', 'TERMINATED'))::bigint"
+ " AS failed_run_count,"
+ " COUNT(*) FILTER (WHERE \"executionStatus\" = 'REVIEW')::bigint"
+ " AS review_run_count,"
+ " COALESCE(SUM((stats->>'walltime')::numeric), 0)::float"
+ " AS total_duration_seconds,"
+ " COUNT(*) FILTER (WHERE (stats->>'walltime') IS NOT NULL)::bigint"
+ " AS duration_run_count"
+ ' FROM {schema_prefix}"AgentGraphExecution"'
+ f" WHERE {_BASE_WHERE}",
+ *params,
+ )
+ return rows[0] if rows else {}
+
+
+async def _fetch_by_agent(
+ params: tuple[str, datetime, datetime],
+) -> list[UserAgentCostRollup]:
+ rows = await query_raw_with_schema(
+ "SELECT"
+ ' "agentGraphId" AS graph_id,'
+ " COALESCE(SUM((stats->>'cost')::numeric), 0)::bigint AS cost_cents,"
+ " COUNT(*)::bigint AS run_count"
+ ' FROM {schema_prefix}"AgentGraphExecution"'
+ f" WHERE {_BASE_WHERE}"
+ ' GROUP BY "agentGraphId"'
+ " ORDER BY cost_cents DESC"
+ f" LIMIT {_MAX_BY_AGENT_ROWS}",
+ *params,
+ )
+ return [
+ UserAgentCostRollup(
+ graph_id=r["graph_id"],
+ cost_cents=int(r.get("cost_cents") or 0),
+ run_count=int(r.get("run_count") or 0),
+ )
+ for r in rows
+ ]
+
+
+async def _fetch_top_runs(
+ params: tuple[str, datetime, datetime], limit: int
+) -> list[UserTopRun]:
+ rows = await query_raw_with_schema(
+ "SELECT"
+ " id AS execution_id,"
+ ' "agentGraphId" AS graph_id,'
+ " COALESCE((stats->>'cost')::numeric, 0)::bigint AS cost_cents,"
+ ' COALESCE("startedAt", "createdAt") AS started_at,'
+ ' "executionStatus" AS status,'
+ " COALESCE((stats->>'walltime')::numeric, 0) AS duration_seconds,"
+ " COALESCE((stats->>'node_error_count')::int, 0) AS node_error_count"
+ ' FROM {schema_prefix}"AgentGraphExecution"'
+ f" WHERE {_BASE_WHERE}"
+ " AND COALESCE((stats->>'cost')::numeric, 0) > 0"
+ ' ORDER BY cost_cents DESC, "createdAt" DESC'
+ " LIMIT $4",
+ *params,
+ limit,
+ )
+ return [
+ UserTopRun(
+ execution_id=r["execution_id"],
+ graph_id=r["graph_id"],
+ cost_cents=int(r.get("cost_cents") or 0),
+ started_at=r["started_at"],
+ status=AgentExecutionStatus(r["status"]),
+ duration_seconds=float(r.get("duration_seconds") or 0),
+ node_error_count=int(r.get("node_error_count") or 0),
+ )
+ for r in rows
+ ]
+
+
+async def _fetch_daily(
+ params: tuple[str, datetime, datetime],
+) -> list[UserDailyCost]:
+ rows = await query_raw_with_schema(
+ "SELECT"
+ ' ("createdAt" AT TIME ZONE \'UTC\')::date AS "date",'
+ " COALESCE(SUM((stats->>'cost')::numeric), 0)::bigint AS cost_cents,"
+ " COUNT(*)::bigint AS run_count,"
+ " COUNT(*) FILTER (WHERE \"executionStatus\" = 'COMPLETED')::bigint"
+ " AS success_count,"
+ " COUNT(*) FILTER (WHERE \"executionStatus\" IN ('FAILED', 'TERMINATED'))::bigint"
+ " AS failed_count,"
+ " COUNT(*) FILTER (WHERE \"executionStatus\" = 'REVIEW')::bigint"
+ " AS review_count"
+ ' FROM {schema_prefix}"AgentGraphExecution"'
+ f" WHERE {_BASE_WHERE}"
+ " GROUP BY (\"createdAt\" AT TIME ZONE 'UTC')::date"
+ ' ORDER BY "date" ASC',
+ *params,
)
+ return [
+ UserDailyCost(
+ date=r["date"],
+ cost_cents=int(r.get("cost_cents") or 0),
+ run_count=int(r.get("run_count") or 0),
+ success_count=int(r.get("success_count") or 0),
+ failed_count=int(r.get("failed_count") or 0),
+ review_count=int(r.get("review_count") or 0),
+ )
+ for r in rows
+ ]
diff --git a/autogpt_platform/backend/backend/data/execution_cost_summary_test.py b/autogpt_platform/backend/backend/data/execution_cost_summary_test.py
index 0377b5be24..6e0670ec4f 100644
--- a/autogpt_platform/backend/backend/data/execution_cost_summary_test.py
+++ b/autogpt_platform/backend/backend/data/execution_cost_summary_test.py
@@ -53,16 +53,21 @@ async def _create_run(
status: AgentExecutionStatus,
cost_cents: int,
started_at: datetime,
- duration: float = 1.0,
+ duration: float | None = 1.0,
node_error_count: int = 0,
is_dry_run: bool = False,
created_at: datetime | None = None,
) -> None:
- stats = {
+ # `walltime` is the key the executor actually persists — it comes from
+ # GraphExecutionStats.model_dump() in update_graph_execution_stats().
+ # `duration is None` models an in-flight/never-finalized run whose stats
+ # carry no walltime at all.
+ stats: dict[str, object] = {
"cost": cost_cents,
- "duration": duration,
"node_error_count": node_error_count,
}
+ if duration is not None:
+ stats["walltime"] = duration
if is_dry_run:
stats["is_dry_run"] = True
await AgentGraphExecution.prisma().create(
@@ -74,7 +79,7 @@ async def _create_run(
"userId": user_id,
"createdAt": created_at if created_at is not None else started_at,
"startedAt": started_at,
- "endedAt": started_at + timedelta(seconds=duration),
+ "endedAt": started_at + timedelta(seconds=duration or 0),
"stats": SafeJson(stats),
}
)
@@ -105,6 +110,11 @@ async def test_empty_window_returns_zeroed_summary(server: SpinTestServer):
assert summary.run_count == 0
assert summary.billable_run_count == 0
assert summary.failed_cost_cents == 0
+ assert summary.success_run_count == 0
+ assert summary.failed_run_count == 0
+ assert summary.review_run_count == 0
+ assert summary.total_duration_seconds == 0
+ assert summary.duration_run_count == 0
assert summary.by_agent == []
assert summary.top_runs == []
assert summary.daily == []
@@ -180,6 +190,26 @@ async def test_aggregates_by_agent_and_top_runs(server: SpinTestServer):
cost_cents=0,
started_at=now - timedelta(minutes=5),
)
+ # A run parked on human review counts toward review_run_count only.
+ await _create_run(
+ run_id=f"e7-{uuid4()}",
+ user_id=user_id,
+ graph_id=graph_a,
+ status=AgentExecutionStatus.REVIEW,
+ cost_cents=0,
+ started_at=now - timedelta(minutes=3),
+ )
+ # Still running, so its stats carry no walltime yet: it inflates
+ # run_count but must stay out of the duration denominator.
+ await _create_run(
+ run_id=f"e8-{uuid4()}",
+ user_id=user_id,
+ graph_id=graph_a,
+ status=AgentExecutionStatus.RUNNING,
+ cost_cents=0,
+ started_at=now - timedelta(minutes=2),
+ duration=None,
+ )
summary = await get_user_cost_summary(
user_id=user_id,
@@ -189,15 +219,23 @@ async def test_aggregates_by_agent_and_top_runs(server: SpinTestServer):
)
assert summary.total_cents == 1570
- assert summary.run_count == 6
+ assert summary.run_count == 8
assert summary.billable_run_count == 5
# 50 FAILED + 20 TERMINATED
assert summary.failed_cost_cents == 70
+ # 4 COMPLETED vs 1 FAILED + 1 TERMINATED + 1 REVIEW + 1 RUNNING
+ assert summary.success_run_count == 4
+ assert summary.failed_run_count == 2
+ assert summary.review_run_count == 1
+ # 7 finished runs at the default 1.0s walltime; the RUNNING run has
+ # none, so it is absent from both the sum and the denominator.
+ assert summary.total_duration_seconds == 7.0
+ assert summary.duration_run_count == 7
by_agent = {row.graph_id: row for row in summary.by_agent}
assert by_agent[graph_a].cost_cents == 500
- # 2 paid runs + 1 zero-cost run on graph_a
- assert by_agent[graph_a].run_count == 3
+ # 2 paid + 1 zero-cost + 1 review + 1 running run on graph_a
+ assert by_agent[graph_a].run_count == 5
assert by_agent[graph_b].cost_cents == 1070
assert by_agent[graph_b].run_count == 3
# graph_b leads on total spend so it sorts first
@@ -206,6 +244,8 @@ async def test_aggregates_by_agent_and_top_runs(server: SpinTestServer):
# Top runs ordered by cost desc; limit honored
assert summary.top_runs[0].execution_id == big_run_id
assert summary.top_runs[0].cost_cents == 1000
+ # Projected off the persisted `walltime` key, not a synthetic one
+ assert summary.top_runs[0].duration_seconds == 1.0
assert len(summary.top_runs) == 3
finally:
await _cleanup(user_id, [graph_a, graph_b])
@@ -295,6 +335,22 @@ async def test_daily_buckets_group_by_utc_date(server: SpinTestServer):
cost_cents=400,
started_at=day2,
)
+ await _create_run(
+ run_id=f"d2f-{uuid4()}",
+ user_id=user_id,
+ graph_id=graph_id,
+ status=AgentExecutionStatus.FAILED,
+ cost_cents=0,
+ started_at=day2,
+ )
+ await _create_run(
+ run_id=f"d2r-{uuid4()}",
+ user_id=user_id,
+ graph_id=graph_id,
+ status=AgentExecutionStatus.REVIEW,
+ cost_cents=0,
+ started_at=day2,
+ )
summary = await get_user_cost_summary(
user_id=user_id,
@@ -308,8 +364,14 @@ async def test_daily_buckets_group_by_utc_date(server: SpinTestServer):
]
assert summary.daily[0].cost_cents == 350
assert summary.daily[0].run_count == 2
+ assert summary.daily[0].success_count == 2
+ assert summary.daily[0].failed_count == 0
+ assert summary.daily[0].review_count == 0
assert summary.daily[1].cost_cents == 400
- assert summary.daily[1].run_count == 1
+ assert summary.daily[1].run_count == 3
+ assert summary.daily[1].success_count == 1
+ assert summary.daily[1].failed_count == 1
+ assert summary.daily[1].review_count == 1
finally:
await _cleanup(user_id, [graph_id])
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index d1f1d170ef..028f229aa8 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -25950,7 +25950,22 @@
"properties": {
"date": { "type": "string", "format": "date", "title": "Date" },
"cost_cents": { "type": "integer", "title": "Cost Cents" },
- "run_count": { "type": "integer", "title": "Run Count" }
+ "run_count": { "type": "integer", "title": "Run Count" },
+ "success_count": {
+ "type": "integer",
+ "title": "Success Count",
+ "default": 0
+ },
+ "failed_count": {
+ "type": "integer",
+ "title": "Failed Count",
+ "default": 0
+ },
+ "review_count": {
+ "type": "integer",
+ "title": "Review Count",
+ "default": 0
+ }
},
"type": "object",
"required": ["date", "cost_cents", "run_count"],
@@ -25968,6 +25983,31 @@
"type": "integer",
"title": "Failed Cost Cents"
},
+ "success_run_count": {
+ "type": "integer",
+ "title": "Success Run Count",
+ "default": 0
+ },
+ "failed_run_count": {
+ "type": "integer",
+ "title": "Failed Run Count",
+ "default": 0
+ },
+ "review_run_count": {
+ "type": "integer",
+ "title": "Review Run Count",
+ "default": 0
+ },
+ "total_duration_seconds": {
+ "type": "number",
+ "title": "Total Duration Seconds",
+ "default": 0
+ },
+ "duration_run_count": {
+ "type": "integer",
+ "title": "Duration Run Count",
+ "default": 0
+ },
"by_agent": {
"items": { "$ref": "#/components/schemas/UserAgentCostRollup" },
"type": "array",