mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-25 05:38:42 +08:00
* feat: 新增团队管理Dashboard接口 * chore: 更新团队管理Dashboard接口定义 * feat: 新增团队管理Dashboard页面 * fix: 修复Dashboard平均耗时PostgreSQL兼容性 * fix: 修复Dashboard Token消耗统计口径 * feat: 新增模型消耗ClickHouse表和读写接口 * feat: 增加模型缓存Token解析和消耗记录器 * feat: Dashboard消耗统计改读ClickHouse * feat: 增加模型消耗历史回填能力 * refactor: 移除模型消耗历史回填逻辑 * refactor: 拆分ClickHouse迁移DDL目录
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package modelusage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/chaitin/MonkeyCode/backend/pkg/clickhouse"
|
|
)
|
|
|
|
type clickhouseStub struct {
|
|
event clickhouse.ModelUsageEvent
|
|
err error
|
|
}
|
|
|
|
func (s *clickhouseStub) InsertModelUsageEvent(ctx context.Context, event clickhouse.ModelUsageEvent) error {
|
|
s.event = event
|
|
return s.err
|
|
}
|
|
|
|
type contextRepoStub struct {
|
|
ctx UsageContext
|
|
err error
|
|
}
|
|
|
|
func (s *contextRepoStub) Resolve(ctx context.Context, taskID, userID uuid.UUID) (UsageContext, error) {
|
|
return s.ctx, s.err
|
|
}
|
|
|
|
func TestRecorderMapsCachedTokens(t *testing.T) {
|
|
ch := &clickhouseStub{}
|
|
taskID := uuid.New()
|
|
userID := uuid.New()
|
|
teamID := uuid.New()
|
|
projectID := uuid.New()
|
|
rec := NewRecorder(ch, &contextRepoStub{ctx: UsageContext{TeamID: teamID, ProjectID: projectID}}, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
|
|
|
err := rec.Record(context.Background(), Event{
|
|
EventTime: time.Date(2026, 6, 4, 19, 0, 0, 0, time.UTC),
|
|
TaskID: taskID,
|
|
UserID: userID,
|
|
Provider: "openai",
|
|
ModelID: "model-1",
|
|
ModelName: "gpt-4o",
|
|
InputTokens: 100,
|
|
OutputTokens: 20,
|
|
CachedTokens: 30,
|
|
TotalTokens: 120,
|
|
Success: true,
|
|
RequestID: "request-1",
|
|
Source: "runtime",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ch.event.TeamID != teamID.String() || ch.event.ProjectID != projectID.String() {
|
|
t.Fatalf("event context = %#v", ch.event)
|
|
}
|
|
if ch.event.CachedTokens != 30 {
|
|
t.Fatalf("cached_tokens = %d, want 30", ch.event.CachedTokens)
|
|
}
|
|
}
|
|
|
|
func TestRecorderIgnoresClickHouseWriteFailure(t *testing.T) {
|
|
ch := &clickhouseStub{err: errors.New("clickhouse down")}
|
|
rec := NewRecorder(ch, &contextRepoStub{}, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
|
|
|
err := rec.Record(context.Background(), Event{TaskID: uuid.New(), UserID: uuid.New(), Source: "runtime"})
|
|
if err != nil {
|
|
t.Fatalf("record error = %v, want nil", err)
|
|
}
|
|
}
|