让 /admin/ops 错误详情正确归因 API key 并补全早退场景字段,合并三项改动: - 鉴权早退补全用户/分组/平台字段:引入 ops fallback key(ContextKeyOpsFallbackAPIKey), apiKey 一加载成功即写入,覆盖分组停用/删除、Key 停用/过期/额度、用户停用、IP 限制等早退 路径;ops 错误日志改用 getOpsAPIKey(正式 key 优先、回退键兜底),不改「已鉴权」语义。 - 已删除 key 归因(迁移 145):删除 key 时同一事务写 deleted_api_key_audits 映射,认证失败 时用明文反查命中原所有者,错误详情展示「已删除 Key 所有者」「尝试的 Key 前缀」。 - 有效 key 报错快照前缀(迁移 147):对绑定有效 key 的错误,落库时快照明文前 8 位到 api_key_prefix(与 attempted_key_prefix 互斥),key 之后被删仍保留报错当时真实前缀。 均仅对上线后新产生的错误/删除生效。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
201 lines
7.2 KiB
Go
201 lines
7.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// opsRepoMock is a test-only OpsRepository implementation with optional function hooks.
|
|
type opsRepoMock struct {
|
|
InsertErrorLogFn func(ctx context.Context, input *OpsInsertErrorLogInput) (int64, error)
|
|
BatchInsertErrorLogsFn func(ctx context.Context, inputs []*OpsInsertErrorLogInput) (int64, error)
|
|
BatchInsertSystemLogsFn func(ctx context.Context, inputs []*OpsInsertSystemLogInput) (int64, error)
|
|
ListSystemLogsFn func(ctx context.Context, filter *OpsSystemLogFilter) (*OpsSystemLogList, error)
|
|
DeleteSystemLogsFn func(ctx context.Context, filter *OpsSystemLogCleanupFilter) (int64, error)
|
|
InsertSystemLogCleanupAuditFn func(ctx context.Context, input *OpsSystemLogCleanupAudit) error
|
|
LookupDeletedKeyAuditFn func(ctx context.Context, key string) (*DeletedKeyAuditResult, error)
|
|
}
|
|
|
|
func (m *opsRepoMock) InsertErrorLog(ctx context.Context, input *OpsInsertErrorLogInput) (int64, error) {
|
|
if m.InsertErrorLogFn != nil {
|
|
return m.InsertErrorLogFn(ctx, input)
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) BatchInsertErrorLogs(ctx context.Context, inputs []*OpsInsertErrorLogInput) (int64, error) {
|
|
if m.BatchInsertErrorLogsFn != nil {
|
|
return m.BatchInsertErrorLogsFn(ctx, inputs)
|
|
}
|
|
return int64(len(inputs)), nil
|
|
}
|
|
|
|
func (m *opsRepoMock) ListErrorLogs(ctx context.Context, filter *OpsErrorLogFilter) (*OpsErrorLogList, error) {
|
|
return &OpsErrorLogList{Errors: []*OpsErrorLog{}, Page: 1, PageSize: 20}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetErrorLogByID(ctx context.Context, id int64) (*OpsErrorLogDetail, error) {
|
|
return &OpsErrorLogDetail{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) ListRequestDetails(ctx context.Context, filter *OpsRequestDetailFilter) ([]*OpsRequestDetail, int64, error) {
|
|
return []*OpsRequestDetail{}, 0, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) BatchInsertSystemLogs(ctx context.Context, inputs []*OpsInsertSystemLogInput) (int64, error) {
|
|
if m.BatchInsertSystemLogsFn != nil {
|
|
return m.BatchInsertSystemLogsFn(ctx, inputs)
|
|
}
|
|
return int64(len(inputs)), nil
|
|
}
|
|
|
|
func (m *opsRepoMock) ListSystemLogs(ctx context.Context, filter *OpsSystemLogFilter) (*OpsSystemLogList, error) {
|
|
if m.ListSystemLogsFn != nil {
|
|
return m.ListSystemLogsFn(ctx, filter)
|
|
}
|
|
return &OpsSystemLogList{Logs: []*OpsSystemLog{}, Total: 0, Page: 1, PageSize: 50}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) DeleteSystemLogs(ctx context.Context, filter *OpsSystemLogCleanupFilter) (int64, error) {
|
|
if m.DeleteSystemLogsFn != nil {
|
|
return m.DeleteSystemLogsFn(ctx, filter)
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) InsertSystemLogCleanupAudit(ctx context.Context, input *OpsSystemLogCleanupAudit) error {
|
|
if m.InsertSystemLogCleanupAuditFn != nil {
|
|
return m.InsertSystemLogCleanupAuditFn(ctx, input)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) UpdateErrorResolution(ctx context.Context, errorID int64, resolved bool, resolvedByUserID *int64, resolvedAt *time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetWindowStats(ctx context.Context, filter *OpsDashboardFilter) (*OpsWindowStats, error) {
|
|
return &OpsWindowStats{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetRealtimeTrafficSummary(ctx context.Context, filter *OpsDashboardFilter) (*OpsRealtimeTrafficSummary, error) {
|
|
return &OpsRealtimeTrafficSummary{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetDashboardOverview(ctx context.Context, filter *OpsDashboardFilter) (*OpsDashboardOverview, error) {
|
|
return &OpsDashboardOverview{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetThroughputTrend(ctx context.Context, filter *OpsDashboardFilter, bucketSeconds int) (*OpsThroughputTrendResponse, error) {
|
|
return &OpsThroughputTrendResponse{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetLatencyHistogram(ctx context.Context, filter *OpsDashboardFilter) (*OpsLatencyHistogramResponse, error) {
|
|
return &OpsLatencyHistogramResponse{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetErrorTrend(ctx context.Context, filter *OpsDashboardFilter, bucketSeconds int) (*OpsErrorTrendResponse, error) {
|
|
return &OpsErrorTrendResponse{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetErrorDistribution(ctx context.Context, filter *OpsDashboardFilter) (*OpsErrorDistributionResponse, error) {
|
|
return &OpsErrorDistributionResponse{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetOpenAITokenStats(ctx context.Context, filter *OpsOpenAITokenStatsFilter) (*OpsOpenAITokenStatsResponse, error) {
|
|
return &OpsOpenAITokenStatsResponse{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) InsertSystemMetrics(ctx context.Context, input *OpsInsertSystemMetricsInput) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetLatestSystemMetrics(ctx context.Context, windowMinutes int) (*OpsSystemMetricsSnapshot, error) {
|
|
return &OpsSystemMetricsSnapshot{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) UpsertJobHeartbeat(ctx context.Context, input *OpsUpsertJobHeartbeatInput) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) ListJobHeartbeats(ctx context.Context) ([]*OpsJobHeartbeat, error) {
|
|
return []*OpsJobHeartbeat{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) ListAlertRules(ctx context.Context) ([]*OpsAlertRule, error) {
|
|
return []*OpsAlertRule{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) CreateAlertRule(ctx context.Context, input *OpsAlertRule) (*OpsAlertRule, error) {
|
|
return input, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) UpdateAlertRule(ctx context.Context, input *OpsAlertRule) (*OpsAlertRule, error) {
|
|
return input, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) DeleteAlertRule(ctx context.Context, id int64) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) ListAlertEvents(ctx context.Context, filter *OpsAlertEventFilter) ([]*OpsAlertEvent, error) {
|
|
return []*OpsAlertEvent{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetAlertEventByID(ctx context.Context, eventID int64) (*OpsAlertEvent, error) {
|
|
return &OpsAlertEvent{}, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetActiveAlertEvent(ctx context.Context, ruleID int64) (*OpsAlertEvent, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetLatestAlertEvent(ctx context.Context, ruleID int64) (*OpsAlertEvent, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) CreateAlertEvent(ctx context.Context, event *OpsAlertEvent) (*OpsAlertEvent, error) {
|
|
return event, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) UpdateAlertEventStatus(ctx context.Context, eventID int64, status string, resolvedAt *time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) UpdateAlertEventEmailSent(ctx context.Context, eventID int64, emailSent bool) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) CreateAlertSilence(ctx context.Context, input *OpsAlertSilence) (*OpsAlertSilence, error) {
|
|
return input, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) IsAlertSilenced(ctx context.Context, ruleID int64, platform string, groupID *int64, region *string, now time.Time) (bool, error) {
|
|
return false, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) UpsertHourlyMetrics(ctx context.Context, startTime, endTime time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) UpsertDailyMetrics(ctx context.Context, startTime, endTime time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetLatestHourlyBucketStart(ctx context.Context) (time.Time, bool, error) {
|
|
return time.Time{}, false, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) GetLatestDailyBucketDate(ctx context.Context) (time.Time, bool, error) {
|
|
return time.Time{}, false, nil
|
|
}
|
|
|
|
func (m *opsRepoMock) LookupDeletedKeyAudit(ctx context.Context, key string) (*DeletedKeyAuditResult, error) {
|
|
if m.LookupDeletedKeyAuditFn != nil {
|
|
return m.LookupDeletedKeyAuditFn(ctx, key)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
var _ OpsRepository = (*opsRepoMock)(nil)
|