The cost-focused Conversations analytics page (pages/ConversationsAnalytics.tsx, 1,192 lines) is replaced by the `analytics` live plugin, redesigned about usage — how many conversations and how long they work — per Gabriel's answers (form analytics-page-redesign_0h3gyc). Cost stays in the core's cost dashboards and cards; lib/costAnalysis stays. - analytics.read, a new owner-only hub capability: GET /proxy/tools/plugins/<id>/hub/analytics?account=&range=&tzOffset=&cron= returns one row per top-level conversation (spawnedBy children left out, cron / Run now / test runs only with cron=1): start, end, working time (distinct 5-minute active bins, subagents included) and per local day, wall-clock span, user turns, model, harness, projects, machine, person, origin. Never a cost (plugins/analyticsdata.py, test_plugin_analytics.py). - /dashboards/conversations, /conversation/dashboard and /conversations/dashboard redirect to /plugins/analytics/analytics; the list pages' chart button goes there; dash.conversations is "Analytics". Removed with it: useConversationsFull and its query key (no page asks for full=1 cards any more), and viz.tsx's ShareBar / ShareBars / PairedBars / Curve, which nothing else drew. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
6.2 KiB
Python
142 lines
6.2 KiB
Python
"""The ``analytics.read`` capability (plugins/analyticsdata.py) — what the
|
|
Analytics plugin reads since the cost-focused Conversations analytics page
|
|
left the core (2026-11-25) and came back about usage: counts and working
|
|
time, never cost."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
import main
|
|
from plugins import analyticsdata as ad
|
|
from plugins.manifest import capability_summary, requires_operator, validate_manifest
|
|
from plugins.store import OWNER_UID, PluginStore
|
|
from toolproxy import access
|
|
|
|
MANIFEST = {
|
|
"schema": 1, "id": "an", "name": "Analytics",
|
|
"ui": {"pages": [{"id": "analytics", "label": "Analytics", "nav": "dashboards", "html": "ui/a.html"}]},
|
|
"actions": {"conversations": {"script": "handlers/a.py"}},
|
|
"capabilities": ["analytics.read"],
|
|
}
|
|
|
|
|
|
def _write(root: pathlib.Path) -> pathlib.Path:
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
(root / "plugin.json").write_text(json.dumps(MANIFEST), encoding="utf-8")
|
|
(root / "ui").mkdir(exist_ok=True)
|
|
(root / "ui" / "a.html").write_text("<p>a</p>", encoding="utf-8")
|
|
(root / "handlers").mkdir(exist_ok=True)
|
|
(root / "handlers" / "a.py").write_text("print('{}')\n", encoding="utf-8")
|
|
return root
|
|
|
|
|
|
class _Store:
|
|
def __init__(self, summaries):
|
|
self.summaries = summaries
|
|
|
|
def all_summaries(self):
|
|
return self.summaries
|
|
|
|
|
|
# bin = 5-minute slots since the epoch; 2026-11-20 10:00Z
|
|
B0 = 1795168800 // 300
|
|
CARDS = [
|
|
{"id": "c1", "sessionId": "s1", "startedAt": "2026-11-20T10:00:00Z", "endedAt": "2026-11-20T10:30:00Z",
|
|
"userTurns": 3, "models": ["claude-opus-5-5"], "cost": 9.9,
|
|
"meta": {"account": "personal", "profile": "personal", "projects": ["brain"], "harness": "claude",
|
|
"origin": "app", "title": "Fix search"}},
|
|
{"id": "c2", "sessionId": "s2", "startedAt": "2026-11-20T11:00:00Z", "endedAt": "2026-11-20T11:05:00Z",
|
|
"userTurns": 1, "models": ["gpt-6-sol"], "meta": {"account": "work", "profile": "work", "harness": "opencode",
|
|
"worker": "gone-machine-id"}},
|
|
{"id": "c3", "sessionId": "s3", "endedAt": "2026-11-20T12:00:00Z", "meta": {"cron": {"job": "x"}}},
|
|
{"id": "c4", "sessionId": "s4", "endedAt": "2026-11-20T12:00:00Z", "meta": {"spawnedBy": {"sessionId": "s1"}}},
|
|
{"id": "c5", "sessionId": "s5", "endedAt": "2026-11-20T12:00:00Z", "meta": {"owner": "dom", "account": "personal",
|
|
"profile": "guests"}},
|
|
]
|
|
SUMMARIES = [
|
|
("a.jsonl", {"sessionId": "s1", "activeBins": {"claude-opus-5-5": [B0, B0 + 1, B0 + 2]}}),
|
|
("a/sub.jsonl", {"sessionId": "s1", "isSidechain": True, "activeBins": {"claude-haiku-4": [B0 + 2, B0 + 3]}}),
|
|
("b.jsonl", {"sessionId": "s2", "activeBins": {"gpt-6-sol": [B0 + 12]}}),
|
|
]
|
|
|
|
|
|
class TestBuild(unittest.TestCase):
|
|
def _build(self, cron=False, account="all"):
|
|
deps = main.app.state.ai
|
|
with patch("plugins.analyticsdata._cards", return_value=CARDS), \
|
|
patch.object(deps, "store", _Store(SUMMARIES)):
|
|
return ad.build(deps, account, "all", 60, cron)
|
|
|
|
def test_rows(self):
|
|
out = self._build()
|
|
rows = {r["id"]: r for r in out["conversations"]}
|
|
self.assertEqual(set(rows), {"c1", "c2", "c5"}) # no cron, no child
|
|
c1 = rows["c1"]
|
|
self.assertEqual(c1["workMin"], 20) # 4 distinct bins, subagent included
|
|
self.assertEqual(c1["wallSec"], 1800)
|
|
self.assertEqual(c1["byDay"], {"2026-11-20": 20})
|
|
self.assertEqual((c1["model"], c1["harness"], c1["projects"], c1["machine"], c1["origin"]),
|
|
("opus", "claude", ["brain"], "Homelab", "app"))
|
|
self.assertEqual(rows["c2"]["model"], "gpt-6-sol")
|
|
self.assertTrue(rows["c2"]["machine"].startswith("Unpaired machine"))
|
|
self.assertTrue(rows["c5"]["guest"])
|
|
|
|
def test_never_a_cost(self):
|
|
text = json.dumps(self._build())
|
|
self.assertNotIn("cost", text)
|
|
self.assertNotIn("9.9", text)
|
|
|
|
def test_cron_toggle(self):
|
|
self.assertIn("c3", {r["id"] for r in self._build(cron=True)["conversations"]})
|
|
|
|
def test_scope(self):
|
|
self.assertEqual({r["id"] for r in self._build(account="work")["conversations"]}, {"c2"})
|
|
|
|
|
|
class TestBridge(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tc = TestClient(main.app)
|
|
self.deps = main.app.state.ai
|
|
|
|
def _store(self, td):
|
|
store = PluginStore(pathlib.Path(td) / "data")
|
|
store.install(OWNER_UID, str(_write(pathlib.Path(td) / "src")), is_owner=True, approve=True)
|
|
return store
|
|
|
|
def test_privileged(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
mf = validate_manifest(_write(pathlib.Path(td) / "an"))
|
|
self.assertTrue(requires_operator(mf))
|
|
self.assertTrue([c for c in capability_summary(mf) if c["kind"] == "capability"][0]["privileged"])
|
|
|
|
def test_owner_reads_it(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
tok = access.mint_handler(OWNER_UID, "an", ttl=600)
|
|
with patch.object(self.deps, "plugin_store", self._store(td)), \
|
|
patch("plugins.analyticsdata.build", return_value={"conversations": []}) as b:
|
|
r = self.tc.get("/proxy/tools/plugins/an/hub/analytics?account=all&range=7d&tzOffset=9999&cron=1",
|
|
headers={"Authorization": f"Bearer {tok}"})
|
|
self.assertEqual(r.status_code, 200, r.text)
|
|
self.assertEqual(b.call_args.args[1:], ("all", "7d", 840, True))
|
|
|
|
def test_a_guest_never_does(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
tok = access.mint("cel", ttl=600)
|
|
with patch.object(self.deps, "plugin_store", self._store(td)), \
|
|
patch("plugins.analyticsdata.build") as b:
|
|
r = self.tc.get("/proxy/tools/plugins/an/hub/analytics",
|
|
headers={"Authorization": f"Bearer {tok}"})
|
|
self.assertEqual(r.status_code, 403)
|
|
b.assert_not_called()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|