Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/secfoo/web/routes/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from fastapi import APIRouter, Request

from secfoo.cost import sum_optional
from secfoo.storage.repository import RunRepository
from secfoo.web.templating import ordered_skills, severity_rows, templates
from secfoo.web.top_findings import build_top_findings
Expand Down Expand Up @@ -51,11 +52,9 @@ def overview(request: Request):
"top_findings": top_findings,
"cost_by_agent": cost_by_agent,
# None (rendered "-") until at least one run has a known cost,
# so runs that predate tracking don't read as free.
"total_cost": (
sum(row.cost_usd for row in cost_by_agent)
if any(row.unpriced_runs < row.runs for row in cost_by_agent)
else None
),
# so runs that predate tracking don't read as free. An agent
# whose runs are all unpriced has cost_usd=None; skip it rather
# than adding it, same as the `secfoo cost` Total row.
"total_cost": sum_optional(row.cost_usd for row in cost_by_agent),
},
)
4 changes: 3 additions & 1 deletion src/secfoo/web/templates/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ <h4 class="card__title">AI spend by agent</h4>
<td>{{ row.label }}</td>
<td>{{ row.runs }}</td>
{% set priced = row.unpriced_runs < row.runs %}
<td>{{ ((row.input_tokens + row.output_tokens) | fmt_tokens) if (priced or row.input_tokens or row.output_tokens) else '-' }}</td>
{# A priced run can still have no token counts (e.g. cost-only agents), so don't add None. #}
{% set tokens_known = row.input_tokens is not none or row.output_tokens is not none %}
<td>{{ (((row.input_tokens or 0) + (row.output_tokens or 0)) | fmt_tokens) if tokens_known else '-' }}</td>
<td>{{ (row.cost_usd | fmt_cost) if priced else '-' }}</td>
</tr>
{% endfor %}
Expand Down
31 changes: 31 additions & 0 deletions tests/test_web/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ def test_overview_lists_recent_assessment(client, tmp_path):
assert "org/repo" in resp.text


def test_overview_totals_cost_when_one_agent_is_unpriced(client, tmp_path):
# A priced claude run alongside an agent that never reports cost
# (devin/droid/cursor/agy) makes cost_summary() return cost_usd=None
# for that agent's group; the total must skip it, not crash on it.
repo = RunRepository()
project_id = repo.upsert_project("https://github.com/org/repo", "org/repo", "github")
for agent_id, cost in (("claude", 1.25), ("devin", None)):
run_uuid = repo.create_run(
project_id=project_id,
skill_id="sast",
skill_name="SAST",
agent_id=agent_id,
confluence_urls=[],
)
repo.complete_run(
run_uuid,
status="success",
exit_code=0,
duration_seconds=1.0,
report_path=None,
prompt_path=None,
stderr_excerpt=None,
cost_usd=cost,
)
repo.close()

resp = client.get("/")
assert resp.status_code == 200
assert "$1.25" in resp.text


def test_project_detail(client, tmp_path):
project_id, _ = _seed_run(tmp_path)
resp = client.get(f"/projects/{project_id}")
Expand Down
Loading