mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-08-18 03:57:06 +08:00
56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package historymetrics
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
type usageFileDocument struct {
|
|
Totals struct {
|
|
ProviderCalls int64 `json:"provider_calls"`
|
|
TurnsTotal int64 `json:"turns_total"`
|
|
ValidTurnsTotal int64 `json:"valid_turns_total"`
|
|
InvalidTurnsTotal int64 `json:"invalid_turns_total"`
|
|
InputTokens int64 `json:"input_tokens"`
|
|
OutputTokens int64 `json:"output_tokens"`
|
|
CacheReadTokens int64 `json:"cache_read_tokens"`
|
|
CacheWriteTokens int64 `json:"cache_write_tokens"`
|
|
TotalTokens int64 `json:"total_tokens"`
|
|
} `json:"totals"`
|
|
}
|
|
|
|
func LoadUsageSummary(path string) (Summary, error) {
|
|
body, err := os.ReadFile(path)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return Summary{}, nil
|
|
}
|
|
return Summary{}, fmt.Errorf("read usage file: %w", err)
|
|
}
|
|
var doc usageFileDocument
|
|
if err := json.Unmarshal(body, &doc); err != nil {
|
|
return Summary{}, fmt.Errorf("decode usage file: %w", err)
|
|
}
|
|
totals := Totals{
|
|
InputTokens: doc.Totals.InputTokens,
|
|
OutputTokens: doc.Totals.OutputTokens,
|
|
CacheReadTokens: doc.Totals.CacheReadTokens,
|
|
CacheWriteTokens: doc.Totals.CacheWriteTokens,
|
|
PromptTokensTotal: doc.Totals.InputTokens + doc.Totals.CacheReadTokens + doc.Totals.CacheWriteTokens,
|
|
RequestTokensTotal: doc.Totals.TotalTokens,
|
|
}
|
|
return Summary{
|
|
ProviderCallsTotal: int(doc.Totals.ProviderCalls),
|
|
TurnsTotal: int(doc.Totals.TurnsTotal),
|
|
ValidTurnsTotal: int(doc.Totals.ValidTurnsTotal),
|
|
InvalidTurnsTotal: int(doc.Totals.InvalidTurnsTotal),
|
|
RequestTokensTotal: totals.RequestTokensTotal,
|
|
PromptTokensTotal: totals.PromptTokensTotal,
|
|
CacheReadTokens: totals.CacheReadTokens,
|
|
CacheWriteTokens: totals.CacheWriteTokens,
|
|
CacheHitRate: cacheHitRateFromTotals(totals),
|
|
}, nil
|
|
}
|