feat(ops): add api key filter to system logs

This commit is contained in:
Bestony committed 2026-06-27 14:35:19 +08:00
1 parent c275422251
commit bad87ff533
15 files changed
+134 -5

No files matched your search

@@ -101,6 +101,10 @@ func TestMigrationsRunner_IsIdempotent_AndSchemaIsUpToDate(t *testing.T) {
requireColumn(t, tx, "scheduler_outbox", "dedup_key", "text", 0, true)
requireIndex(t, tx, "scheduler_outbox", "idx_scheduler_outbox_pending_dedup_key")
// ops_system_logs: API key id index for operational log triage
requireColumn(t, tx, "ops_system_logs", "api_key_id", "bigint", 0, true)
requireIndex(t, tx, "ops_system_logs", "idx_ops_system_logs_api_key_id_created_at")
// user_allowed_groups table should exist
var uagRegclass sql.NullString
require.NoError(t, tx.QueryRowContext(context.Background(), "SELECT to_regclass('public.user_allowed_groups')").Scan(&uagRegclass))
+15
View File
@@ -678,6 +678,7 @@ func (r *opsRepository) BatchInsertSystemLogs(ctx context.Context, inputs []*ser
"request_id",
"client_request_id",
"user_id",
"api_key_id",
"account_id",
"platform",
"model",
@@ -719,6 +720,7 @@ func (r *opsRepository) BatchInsertSystemLogs(ctx context.Context, inputs []*ser
opsNullString(input.RequestID),
opsNullString(input.ClientRequestID),
opsNullInt64(input.UserID),
opsNullInt64(input.APIKeyID),
opsNullInt64(input.AccountID),
opsNullString(input.Platform),
opsNullString(input.Model),
@@ -785,6 +787,7 @@ SELECT
COALESCE(l.request_id, ''),
COALESCE(l.client_request_id, ''),
l.user_id,
l.api_key_id,
l.account_id,
COALESCE(l.platform, ''),
COALESCE(l.model, ''),
@@ -804,6 +807,7 @@ LIMIT $` + itoa(len(args)+1) + ` OFFSET $` + itoa(len(args)+2)
for rows.Next() {
item := &service.OpsSystemLog{}
var userID sql.NullInt64
var apiKeyID sql.NullInt64
var accountID sql.NullInt64
var extraRaw string
if err := rows.Scan(
@@ -815,6 +819,7 @@ LIMIT $` + itoa(len(args)+1) + ` OFFSET $` + itoa(len(args)+2)
&item.RequestID,
&item.ClientRequestID,
&userID,
&apiKeyID,
&accountID,
&item.Platform,
&item.Model,
@@ -826,6 +831,10 @@ LIMIT $` + itoa(len(args)+1) + ` OFFSET $` + itoa(len(args)+2)
v := userID.Int64
item.UserID = &v
}
if apiKeyID.Valid {
v := apiKeyID.Int64
item.APIKeyID = &v
}
if accountID.Valid {
v := accountID.Int64
item.AccountID = &v
@@ -1098,6 +1107,11 @@ func buildOpsSystemLogsWhere(filter *service.OpsSystemLogFilter) (string, []any,
clauses = append(clauses, "l.user_id = $"+itoa(len(args)))
hasConstraint = true
}
if filter.APIKeyID != nil && *filter.APIKeyID > 0 {
args = append(args, *filter.APIKeyID)
clauses = append(clauses, "l.api_key_id = $"+itoa(len(args)))
hasConstraint = true
}
if filter.AccountID != nil && *filter.AccountID > 0 {
args = append(args, *filter.AccountID)
clauses = append(clauses, "l.account_id = $"+itoa(len(args)))
@@ -1137,6 +1151,7 @@ func buildOpsSystemLogsCleanupWhere(filter *service.OpsSystemLogCleanupFilter) (
RequestID: filter.RequestID,
ClientRequestID: filter.ClientRequestID,
UserID: filter.UserID,
APIKeyID: filter.APIKeyID,
AccountID: filter.AccountID,
Platform: filter.Platform,
Model: filter.Model,
@@ -12,6 +12,7 @@ func TestBuildOpsSystemLogsWhere_WithClientRequestIDAndUserID(t *testing.T) {
start := time.Date(2026, 2, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2026, 2, 2, 0, 0, 0, 0, time.UTC)
userID := int64(12)
apiKeyID := int64(56)
accountID := int64(34)
filter := &service.OpsSystemLogFilter{
@@ -22,6 +23,7 @@ func TestBuildOpsSystemLogsWhere_WithClientRequestIDAndUserID(t *testing.T) {
RequestID: "req-1",
ClientRequestID: "creq-1",
UserID: &userID,
APIKeyID: &apiKeyID,
AccountID: &accountID,
Platform: "openai",
Model: "gpt-5",
@@ -35,8 +37,8 @@ func TestBuildOpsSystemLogsWhere_WithClientRequestIDAndUserID(t *testing.T) {
if where == "" {
t.Fatalf("where should not be empty")
}
if len(args) != 11 {
t.Fatalf("args len = %d, want 11", len(args))
if len(args) != 12 {
t.Fatalf("args len = %d, want 12", len(args))
}
if !contains(where, "COALESCE(l.client_request_id,'') = $") {
t.Fatalf("where should include client_request_id condition: %s", where)
@@ -44,6 +46,9 @@ func TestBuildOpsSystemLogsWhere_WithClientRequestIDAndUserID(t *testing.T) {
if !contains(where, "l.user_id = $") {
t.Fatalf("where should include user_id condition: %s", where)
}
if !contains(where, "l.api_key_id = $") {
t.Fatalf("where should include api_key_id condition: %s", where)
}
}
func TestBuildOpsSystemLogsCleanupWhere_RequireConstraint(t *testing.T) {
@@ -61,17 +66,19 @@ func TestBuildOpsSystemLogsCleanupWhere_RequireConstraint(t *testing.T) {
func TestBuildOpsSystemLogsCleanupWhere_WithClientRequestIDAndUserID(t *testing.T) {
userID := int64(9)
apiKeyID := int64(10)
filter := &service.OpsSystemLogCleanupFilter{
ClientRequestID: "creq-9",
UserID: &userID,
APIKeyID: &apiKeyID,
}
where, args, hasConstraint := buildOpsSystemLogsCleanupWhere(filter)
if !hasConstraint {
t.Fatalf("expected hasConstraint=true")
}
if len(args) != 2 {
t.Fatalf("args len = %d, want 2", len(args))
if len(args) != 3 {
t.Fatalf("args len = %d, want 3", len(args))
}
if !contains(where, "COALESCE(l.client_request_id,'') = $") {
t.Fatalf("where should include client_request_id condition: %s", where)
@@ -79,6 +86,9 @@ func TestBuildOpsSystemLogsCleanupWhere_WithClientRequestIDAndUserID(t *testing.
if !contains(where, "l.user_id = $") {
t.Fatalf("where should include user_id condition: %s", where)
}
if !contains(where, "l.api_key_id = $") {
t.Fatalf("where should include api_key_id condition: %s", where)
}
}
func contains(s string, sub string) bool {