feat(monitor): 卡片概况与统一文件处理表
build / build (push) Successful in 1m17s

This commit is contained in:
姚章浩
2026-10-27 12:14:19 +08:00
parent d8d143eca0
commit 1b09f7d35b
13 changed files with 2238 additions and 559 deletions
+102
View File
@@ -0,0 +1,102 @@
package httpapi
import (
"context"
"errors"
postgresstore "knowledge-governance/internal/platform/postgres"
"net/http"
"strconv"
"strings"
"time"
)
type monitorRemovalRetrier interface {
RetryMonitorRemoval(context.Context, string, string, time.Time) error
}
func retryMonitorRemoval(w http.ResponseWriter, r *http.Request, config Config) {
identity, ok := requireSessionIdentity(w, r)
if !ok {
return
}
if identity.Role != roleEngineer && identity.Role != roleAdmin {
writeJSON(w, 403, map[string]string{"error": "forbidden"})
return
}
if r.Header.Get("X-Governance-Action") != "monitor-removal-retry" {
writeJSON(w, 403, map[string]string{"error": "write intent required"})
return
}
id := r.PathValue("id")
if id == "" || len(id) > 128 {
writeJSON(w, 400, map[string]string{"error": "invalid publication"})
return
}
store, ok := config.Store.(monitorRemovalRetrier)
if !ok {
writeJSON(w, 501, map[string]string{"error": "removal retry not configured"})
return
}
err := store.RetryMonitorRemoval(r.Context(), id, actorFor(r), time.Now().UTC())
if errors.Is(err, postgresstore.ErrPublicationStateConflict) {
writeJSON(w, 409, map[string]string{"error": "状态或治理规则已变化,不能重复提交"})
return
}
if err != nil {
writeJSON(w, 500, map[string]string{"error": "测试库移除重试失败"})
return
}
writeJSON(w, 202, map[string]string{"status": "accepted"})
}
type monitorTaskReader interface {
ListMonitorTasks(context.Context, postgresstore.MonitorTaskFilter) (postgresstore.MonitorTaskPage, error)
}
func listMonitorTasks(w http.ResponseWriter, r *http.Request, config Config) {
if _, ok := requireSessionIdentity(w, r); !ok {
return
}
q := r.URL.Query()
f := postgresstore.MonitorTaskFilter{Search: q.Get("q"), Source: q.Get("source"), Scope: q.Get("scope"), Action: q.Get("action"), Status: q.Get("status"), Stage: q.Get("stage"), ID: q.Get("id"), Target: q.Get("target"), Since: q.Get("since"), Until: q.Get("until"), Limit: 20}
for k, p := range map[string]*int{"limit": &f.Limit, "offset": &f.Offset} {
if q.Get(k) != "" {
n, e := strconv.Atoi(q.Get(k))
if e != nil {
writeJSON(w, 400, map[string]string{"error": "invalid pagination"})
return
}
*p = n
}
}
if f.Limit < 1 || f.Limit > 100 || f.Offset < 0 || len(f.Search) > 512 || len(f.Source) > 128 || len(f.ID) > 128 {
writeJSON(w, 400, map[string]string{"error": "invalid filter"})
return
}
for _, pair := range [][2]string{{f.Scope, "|internal|external"}, {f.Action, "|add|update|remove"}, {f.Status, "|active|waiting|running|retrying|attention|completed|cancelled"}, {f.Stage, "|waiting|fetch|upload|parsing|indexing|removal|finished"}} {
if !strings.Contains("|"+pair[1]+"|", "|"+pair[0]+"|") {
writeJSON(w, 400, map[string]string{"error": "invalid filter"})
return
}
}
for _, v := range []string{f.Since, f.Until} {
if v != "" {
if _, e := time.Parse(time.RFC3339, v); e != nil {
writeJSON(w, 400, map[string]string{"error": "invalid date"})
return
}
}
}
store, ok := config.Store.(monitorTaskReader)
if !ok {
writeJSON(w, 501, map[string]string{"error": "monitor tasks not configured"})
return
}
page, e := store.ListMonitorTasks(r.Context(), f)
if e != nil {
writeJSON(w, 500, map[string]string{"error": "无法读取文件处理状态"})
return
}
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, 200, map[string]any{"data": page})
}
+53
View File
@@ -0,0 +1,53 @@
package httpapi
import (
"context"
postgresstore "knowledge-governance/internal/platform/postgres"
"net/http/httptest"
"testing"
)
type monitorTasksTestStore struct {
fakeJobStore
filter postgresstore.MonitorTaskFilter
}
func (s *monitorTasksTestStore) ListMonitorTasks(_ context.Context, f postgresstore.MonitorTaskFilter) (postgresstore.MonitorTaskPage, error) {
s.filter = f
return postgresstore.MonitorTaskPage{Items: []postgresstore.MonitorTask{}, Total: 145, Limit: f.Limit, Offset: f.Offset}, nil
}
func TestMonitorTasksRoute(t *testing.T) {
s := &monitorTasksTestStore{}
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?status=active&limit=20&offset=40", nil)
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
w := httptest.NewRecorder()
h, err := NewHandler(Config{Store: s})
if err != nil {
t.Fatal(err)
}
h.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
if s.filter.Status != "active" || s.filter.Offset != 40 {
t.Fatalf("filter %#v", s.filter)
}
for _, query := range []string{"status=garbage", "stage=internal&scope=internal", "limit=101", "offset=-1", "since=bad"} {
r := httptest.NewRequest("GET", "/api/v1/monitor/tasks?"+query, nil).WithContext(req.Context())
rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
if rr.Code != 400 {
t.Fatalf("invalid %s: %d", query, rr.Code)
}
}
unauth := httptest.NewRecorder()
h.ServeHTTP(unauth, httptest.NewRequest("GET", "/api/v1/monitor/tasks", nil))
if unauth.Code != 401 {
t.Fatalf("unauthenticated %d", unauth.Code)
}
removal := httptest.NewRecorder()
h.ServeHTTP(removal, httptest.NewRequest("POST", "/api/v1/test-publications/example/removal-retry", nil).WithContext(req.Context()))
if removal.Code != 403 {
t.Fatalf("viewer removal %d", removal.Code)
}
}
+6
View File
@@ -398,6 +398,9 @@ func NewHandler(config Config) (http.Handler, error) {
mux.HandleFunc("GET /api/v1/monitor/knowledge-bases/{id}/files", func(w http.ResponseWriter, r *http.Request) {
monitorFiles(w, r, config.Monitor)
})
mux.HandleFunc("GET /api/v1/monitor/tasks", func(w http.ResponseWriter, r *http.Request) {
listMonitorTasks(w, r, config)
})
mux.HandleFunc("GET /api/v1/monitor/metrics", func(w http.ResponseWriter, r *http.Request) {
monitorMetrics(w, r, config.Monitor)
})
@@ -557,6 +560,9 @@ func NewHandler(config Config) (http.Handler, error) {
})
// 需下架条目的移除入口:把「被禁止导入但仍存活在测试库」的知识从测试库移除
// (置 disabling + 入队既有 test.remove 工作流)。与开始处理同级:engineer 即可。
mux.HandleFunc("POST /api/v1/test-publications/{id}/removal-retry", func(w http.ResponseWriter, r *http.Request) {
retryMonitorRemoval(w, r, config)
})
mux.HandleFunc("POST /api/v1/test-publications/{id}/removal", func(w http.ResponseWriter, r *http.Request) {
startTestPublicationRemoval(w, r, config)
})