AnyBot/docs/restructure/notes/b04h-stats-usage.md

24 lines
2.1 KiB
Markdown
Raw Normal View History

# b04h:stats.rs(303 行)+ usage.rs(209 行)— 用量賬
## usage.rs(每筆模型呼叫落盤)
- `Entry{15}`:at(ms)、provider、model、session(請求的 session 名)、`usage`(#[serde(flatten)],providers::Usage 原欄,含 cost when the provider says it)。
- `Entry::whose()`(29)=session 前綴判「誰的呼叫」:`[("anybot-bot-","回合"),("anybot-check-","發布前檢查"),("anybot-goal-","目標檢查"),("anybot-btw-","支線")]` → (bot id, 用途)。測試鎖(174–177)逐條。
- 落盤:`data/usage/YYYY-MM.jsonl`,一行一筆;`note(req, u)`(49)=**usage 非空才寫**。
- `since(since)`(62)/`since_any(since)`(80)=讀回(後者含非 bot 的?實讀區分)。
- `summary(entries, now, name)`(122)=「/usage」答案:最近 24 小時+最近 7 天,分 bot 分模型;開頭「用量(模型服務回報的 token)」逐字;**超過一週的列不入**(測試 194 鎖)。
- `command(text)`(161)=trim=="/usage"(「/usage x」/「/usages」拒——測試 207)。
- 紅線(模組頭註逐字):**不猜價格**——cost 只在 provider 回報時出現。
## stats.rs(設定頁用量頁)
- 常數:`DAYS = 53 * 7`(热力图一年 53 週,止於今天)、`DAY_MS = 86_400_000`。
- `Inputs{17}`:messages、names、timezone;`inputs(st)`(32)從 State 抽。
- `build(inp, now)`(47)→ `compute(inp, entries, now)`(57)=_heat map 逐日、tokens 分 kind/provider/model/bot、7/30/90 天窗。
- **沒有 cached token 欄**(實讀:不计缓存命中)。
- `wan(n)`(204)=「萬」字格式化;`cost_words(micros, unpriced, calls)`(222)=**未定價的呼叫計數單獨說「未定價」,絕不報 $0**(模組頭註逐字)。
## 測試鎖(節選)
usage:whose 四前綴、day+week 加總、一週外不入、command 嚴格、`note` 寫檔(200:Anthropic req session=anybot-bot-42)。
## 迁移
usage.rs+stats.rs → **core::usage**(兩檔相依:stats 吃 usage::Entry;同 module 不拆)。落盤目錄 data/usage 走 core::paths。