diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3cb756f..ec40fe3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,8 @@
### Added
+- 新增解析监控统一工作台:顶部运行指标与依赖卡片、下方单一文件处理表;来源文件与目标测试库归并最新版本,动作、阶段、状态分列,后端统一筛选、计数及分页。支持单条/跨页勾选重试和版本、失败、重试详情;测试库移除重试独立核验治理规则,不误走上传流程。取消六分页和手动开始入口,生产授权仍在发布管理;本次不改变后台自动调度策略,自动处理闭环需另行端到端验收。
+
- 问答记录新增“本周”快捷查询,默认展示北京时间本周数据;所选范围自动读取全部记录,校验完整数量后展示,不再需要分页或加载更多。固定展示完整字段,空列不再隐藏,中文原文直接展示、外语未翻译明确标记;保留源端有界传输、取消和失败保护。
- 问答记录新增年份与自然周选择,周次展示起止日期,支持跨年及每年实际周数;按北京时间周一至周日填入查询范围,默认本周且仅查询截至当前的数据。保留自定义时间与原查询按钮,浏览器时区不改变查询边界。
diff --git a/api/openapi.yaml b/api/openapi.yaml
index ef4e0eb..2f83c5f 100644
--- a/api/openapi.yaml
+++ b/api/openapi.yaml
@@ -3386,6 +3386,323 @@
"operationId": "cancelPublicationsBatch"
}
},
+ "/api/v1/monitor/tasks": {
+ "get": {
+ "operationId": "listMonitorTasks",
+ "summary": "统一文件处理表(来源文件×目标测试库)",
+ "description": "按最新未替代版本归并后在数据库内筛选、分页和计数。id筛选附带最近50条版本状态、失败和重试记录。只读,不触发处理。",
+ "parameters": [
+ {
+ "name": "q",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "source",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "scope",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "target",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "action",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "status",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "stage",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "since",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "until",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "id",
+ "in": "query",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "limit",
+ "in": "query",
+ "schema": {
+ "type": "integer",
+ "minimum": 1,
+ "maximum": 100,
+ "default": 20
+ }
+ },
+ {
+ "name": "offset",
+ "in": "query",
+ "schema": {
+ "type": "integer",
+ "minimum": 0
+ }
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "筛选参数错误"
+ },
+ "401": {
+ "description": "需要登录"
+ },
+ "403": {
+ "description": "权限不足"
+ },
+ "500": {
+ "description": "读取或提交失败"
+ },
+ "501": {
+ "description": "存储未配置"
+ },
+ "200": {
+ "description": "同一快照的筛选结果、总数、可重试数及全局筛选项",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "data": {
+ "type": "object",
+ "required": [
+ "items",
+ "total",
+ "limit",
+ "offset",
+ "retryable",
+ "sources",
+ "targets"
+ ],
+ "properties": {
+ "items": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "required": [
+ "id",
+ "title",
+ "path",
+ "source_id",
+ "source_name",
+ "target_scope",
+ "target_kb",
+ "action",
+ "stage",
+ "status",
+ "detail",
+ "updated_at",
+ "retry_kind",
+ "failure_id",
+ "version_id",
+ "retry_count",
+ "history"
+ ],
+ "properties": {
+ "id": {
+ "type": "string"
+ },
+ "title": {
+ "type": "string"
+ },
+ "path": {
+ "type": "string"
+ },
+ "source_id": {
+ "type": "string"
+ },
+ "source_name": {
+ "type": "string"
+ },
+ "target_scope": {
+ "type": "string"
+ },
+ "target_kb": {
+ "type": "string"
+ },
+ "action": {
+ "type": "string"
+ },
+ "stage": {
+ "type": "string"
+ },
+ "status": {
+ "type": "string"
+ },
+ "detail": {
+ "type": "string"
+ },
+ "updated_at": {
+ "type": "string"
+ },
+ "retry_kind": {
+ "type": "string"
+ },
+ "failure_id": {
+ "type": "string"
+ },
+ "version_id": {
+ "type": "string"
+ },
+ "retry_count": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "history": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "at": {
+ "type": "string"
+ },
+ "kind": {
+ "type": "string"
+ },
+ "detail": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "total": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "limit": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "offset": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "retryable": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "sources": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "string"
+ },
+ "name": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "targets": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "string"
+ },
+ "name": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "/api/v1/test-publications/{id}/removal-retry": {
+ "post": {
+ "operationId": "retryMonitorRemoval",
+ "summary": "重试失败的测试库移除任务",
+ "description": "仅工程师或管理员;事务内核验禁止导入规则、未被替代及无运行任务,恢复原移除任务并登记审计。重复调用冲突不重复入队,不上传知识、不操作来源及生产。",
+ "parameters": [
+ {
+ "name": "id",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "type": "string"
+ }
+ }
+ ],
+ "responses": {
+ "400": {
+ "description": "筛选参数错误"
+ },
+ "401": {
+ "description": "需要登录"
+ },
+ "403": {
+ "description": "权限不足"
+ },
+ "500": {
+ "description": "读取或提交失败"
+ },
+ "501": {
+ "description": "存储未配置"
+ },
+ "202": {
+ "description": "已受理,不代表移除已完成"
+ },
+ "409": {
+ "description": "规则或状态已变化,不能重试"
+ }
+ }
+ }
+ },
"/api/v1/monitor/metrics": {
"get": {
"operationId": "getMonitorMetrics",
diff --git a/internal/httpapi/monitor_tasks.go b/internal/httpapi/monitor_tasks.go
new file mode 100644
index 0000000..99adad7
--- /dev/null
+++ b/internal/httpapi/monitor_tasks.go
@@ -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})
+}
diff --git a/internal/httpapi/monitor_tasks_test.go b/internal/httpapi/monitor_tasks_test.go
new file mode 100644
index 0000000..38623a0
--- /dev/null
+++ b/internal/httpapi/monitor_tasks_test.go
@@ -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)
+ }
+}
diff --git a/internal/httpapi/server.go b/internal/httpapi/server.go
index 550cdd5..541f479 100644
--- a/internal/httpapi/server.go
+++ b/internal/httpapi/server.go
@@ -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)
})
diff --git a/internal/platform/postgres/monitor_removal_retry.go b/internal/platform/postgres/monitor_removal_retry.go
new file mode 100644
index 0000000..2675784
--- /dev/null
+++ b/internal/platform/postgres/monitor_removal_retry.go
@@ -0,0 +1,37 @@
+package postgres
+
+import (
+ "context"
+ "time"
+)
+
+func (s *Store) RetryMonitorRemoval(ctx context.Context, id, actor string, now time.Time) error {
+ var count int
+ err := s.db.QueryRow(ctx, `WITH candidate AS (
+ SELECT p.id,j.id AS job_id FROM test_publications p
+ JOIN source_item_versions v ON v.id=p.source_item_version_id
+ JOIN source_items si ON si.id=v.source_item_id
+ JOIN jobs j ON j.payload->>'publication_id'=p.id AND j.type='test.remove' AND j.state='failed'
+ WHERE p.id=$1 AND p.superseded_at IS NULL AND p.knowledge_id<>'' AND p.retrieval_state IN ('failed','disabling') AND si.policy_state='blocked'
+ AND NOT EXISTS(SELECT 1 FROM jobs live WHERE live.payload->>'publication_id'=p.id AND live.state IN ('pending','running','retry'))
+ ORDER BY j.updated_at DESC,j.id LIMIT 1 FOR UPDATE OF p,si,j
+ ), retried AS (
+ UPDATE jobs SET state='pending',attempt=0,available_at=$2,lease_owner='',lease_until=NULL,last_error='',last_error_code='',updated_at=$2
+ FROM candidate c WHERE jobs.id=c.job_id AND jobs.state='failed' RETURNING c.id,jobs.id AS job_id
+ ), changed AS (
+ UPDATE test_publications SET retrieval_state='disabling',last_error='',retry_count=retry_count+1,updated_at=$2
+ FROM retried r WHERE test_publications.id=r.id RETURNING test_publications.id,r.job_id
+ ), closed AS (
+ UPDATE job_failures SET state='requeued',resolved_at=$2,resolution_actor_ref=$4,resolution_reason='测试库移除重试' FROM changed c WHERE job_failures.job_id=c.job_id AND job_failures.state='open'
+ ), audited AS (
+ INSERT INTO job_retry_operations(id,batch_key,publication_id,actor_ref,reason,outcome,created_at)
+ SELECT $3,$3,c.id,$4,'测试库移除重试','started',$2 FROM changed c
+ ) SELECT count(*) FROM changed`, id, now, orchestrationJobID("monitor-removal-retry", id, now.String()), actor).Scan(&count)
+ if err != nil {
+ return err
+ }
+ if count != 1 {
+ return ErrPublicationStateConflict
+ }
+ return nil
+}
diff --git a/internal/platform/postgres/monitor_tasks.go b/internal/platform/postgres/monitor_tasks.go
new file mode 100644
index 0000000..23c52b6
--- /dev/null
+++ b/internal/platform/postgres/monitor_tasks.go
@@ -0,0 +1,140 @@
+package postgres
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+)
+
+type MonitorTaskFilter struct {
+ Search, Source, Scope, Action, Status, Stage, ID, Target, Since, Until string
+ Limit, Offset int
+}
+type MonitorTaskEvent struct {
+ At string `json:"at"`
+ Kind string `json:"kind"`
+ Detail string `json:"detail"`
+}
+type MonitorTask struct {
+ History []MonitorTaskEvent `json:"history"`
+ ID string `json:"id"`
+ Title string `json:"title"`
+ Path string `json:"path"`
+ SourceID string `json:"source_id"`
+ SourceName string `json:"source_name"`
+ TargetScope string `json:"target_scope"`
+ TargetKB string `json:"target_kb"`
+ Action string `json:"action"`
+ Stage string `json:"stage"`
+ Status string `json:"status"`
+ Detail string `json:"detail"`
+ UpdatedAt string `json:"updated_at"`
+ RetryKind string `json:"retry_kind"`
+ FailureID string `json:"failure_id"`
+ RetryCount int `json:"retry_count"`
+ VersionID string `json:"version_id"`
+}
+type MonitorTaskPage struct {
+ Items []MonitorTask `json:"items"`
+ Total int `json:"total"`
+ Limit int `json:"limit"`
+ Offset int `json:"offset"`
+ Sources []MonitorOption `json:"sources"`
+ Targets []MonitorOption `json:"targets"`
+ Retryable int `json:"retryable"`
+}
+type MonitorOption struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+}
+
+// Reduce publication versions before filtering; totals and pages share one snapshot.
+const monitorTasksCTE = `WITH history_publications AS (
+ SELECT hp.* FROM test_publications hp JOIN source_item_versions hv ON hv.id=hp.source_item_version_id
+ WHERE $7<>'' AND (hv.source_item_id,hp.test_kb_id)=(SELECT v.source_item_id,p.test_kb_id FROM test_publications p JOIN source_item_versions v ON v.id=p.source_item_version_id WHERE p.id=$7)
+), history_events AS (
+ SELECT updated_at AS at,'版本状态' AS kind,source_item_version_id || ' · ' || processing_state || ' / ' || retrieval_state AS detail FROM history_publications
+ UNION ALL SELECT f.created_at,'失败记录',f.job_type || ' · ' || f.error_code || ' · 尝试 ' || f.attempt FROM job_failures f WHERE f.publication_id IN(SELECT id FROM history_publications) OR (f.job_type='artifact.fetch' AND f.business_version_id IN(SELECT source_item_version_id FROM history_publications))
+ UNION ALL SELECT r.created_at,'重试记录',r.reason || ' · ' || r.outcome FROM job_retry_operations r WHERE r.publication_id IN(SELECT id FROM history_publications)
+), latest AS (
+ SELECT DISTINCT ON(v.source_item_id,p.test_kb_id) p.*,v.source_item_id,v.content_object_id
+ FROM test_publications p JOIN source_item_versions v ON v.id=p.source_item_version_id
+ WHERE p.superseded_at IS NULL
+ ORDER BY v.source_item_id,p.test_kb_id,p.created_at DESC,p.id DESC
+), base AS (
+ SELECT p.id,si.title,si.normalized_path AS path,c.id AS source_id,c.name AS source_name,
+ p.target_scope,p.test_kb_id AS target_kb,p.source_item_version_id AS version_id,p.retry_count,
+ GREATEST(p.updated_at,j.updated_at,f.created_at) AS updated_at,
+ p.processing_state,p.ingest_state,p.retrieval_state,p.last_error,p.knowledge_id,
+ co.content_state,co.local_spool_ref,si.policy_state,
+ CASE WHEN p.retrieval_state IN ('disabling','disabled') OR (p.knowledge_id<>'' AND si.policy_state='blocked') OR j.type='test.remove' THEN 'remove'
+ WHEN EXISTS(SELECT 1 FROM test_publications prev JOIN source_item_versions pv ON pv.id=prev.source_item_version_id WHERE pv.source_item_id=si.id AND prev.test_kb_id=p.test_kb_id AND prev.id<>p.id AND prev.created_at
>'publication_id'=p.id OR (j.type='artifact.fetch' AND j.payload->>'source_item_version_id'=p.source_item_version_id))
+ AND j.type IN ('artifact.fetch','test.upload','test.poll','test.remove')
+ ORDER BY CASE WHEN j.state IN ('pending','running','retry') THEN 0 ELSE 1 END,j.updated_at DESC,j.id DESC LIMIT 1) j ON TRUE
+ LEFT JOIN LATERAL(SELECT f.id,f.job_type,f.error_code,f.created_at FROM job_failures f WHERE f.state='open' AND (f.publication_id=p.id OR (f.job_type='artifact.fetch' AND f.business_version_id=p.source_item_version_id)) ORDER BY f.created_at DESC,f.id DESC LIMIT 1) f ON TRUE
+), classified AS (
+ SELECT *,
+ CASE WHEN action='remove' AND retrieval_state='disabled' THEN 'completed'
+ WHEN action<>'remove' AND processing_state='completed' THEN 'completed'
+ WHEN job_state IN ('pending','retry') AND job_attempt>0 THEN 'retrying'
+ WHEN job_state='running' THEN 'running'
+ WHEN job_state IN ('pending','retry') THEN 'waiting'
+ WHEN failure_id<>'' OR processing_state IN ('failed','stalled') OR ingest_state='failed' OR retrieval_state='failed' THEN 'attention'
+ WHEN processing_state='cancelled' THEN 'cancelled'
+ WHEN action='remove' THEN 'waiting'
+ WHEN processing_state IN ('processing','finalizing') THEN 'running'
+ ELSE 'waiting' END AS status,
+ CASE WHEN action='remove' AND retrieval_state='disabled' THEN 'finished'
+ WHEN action='remove' THEN 'removal'
+ WHEN processing_state IN ('completed','cancelled') THEN 'finished'
+ WHEN job_type='artifact.fetch' AND job_state IN ('pending','running','retry') THEN 'fetch'
+ WHEN processing_state='finalizing' THEN 'indexing'
+ WHEN processing_state='processing' OR job_type='test.poll' THEN 'parsing'
+ WHEN ingest_state='uploading' OR job_type='test.upload' THEN 'upload'
+ WHEN failure_type='artifact.fetch' OR COALESCE(content_state,'')<>'verified' THEN 'fetch'
+ ELSE 'waiting' END AS stage
+ FROM base
+), projected AS (
+ SELECT id,title,path,source_id,source_name,target_scope,target_kb,version_id,retry_count,updated_at,action,stage,status,failure_id,
+ CASE WHEN status<>'attention' THEN ''
+ WHEN action='remove' THEN CASE WHEN job_type='test.remove' AND job_state='failed' AND retrieval_state IN ('failed','disabling') AND policy_state='blocked' THEN 'remove' ELSE '' END
+ WHEN failure_type='artifact.fetch' THEN 'fetch'
+ WHEN content_state='verified' AND local_spool_ref<>'' AND processing_state IN ('failed','stalled','pending') THEN 'upload' ELSE '' END AS retry_kind,
+ CASE WHEN status='completed' THEN CASE WHEN action='remove' THEN '已从测试库移除;来源文件保留' ELSE '测试库处理完成;未代表已发布生产' END
+ WHEN status='cancelled' THEN '处理已取消,记录保留'
+ WHEN status='retrying' THEN '等待自动重试;下次调度 ' || COALESCE(to_char(available_at AT TIME ZONE 'Asia/Shanghai','YYYY-MM-DD HH24:MI:SS'),'未提供')
+ WHEN status='attention' THEN COALESCE(NULLIF(last_error,''),NULLIF(failure_error,''),'处理异常,请查看详情')
+ WHEN stage='removal' THEN CASE WHEN status='running' THEN '正在移除测试库知识' ELSE '等待测试库移除任务' END
+ WHEN stage='fetch' THEN '等待或正在获取源文件'
+ WHEN stage='waiting' THEN '文件已就绪,等待后台处理调度'
+ WHEN stage='upload' THEN '等待或正在上传测试知识库'
+ WHEN stage='indexing' THEN '正在索引与增强'
+ ELSE '正在解析处理' END AS detail
+ FROM classified
+), filtered AS (
+ SELECT * FROM projected WHERE ($1='' OR strpos(lower(title || ' ' || path),lower($1))>0)
+ AND ($2='' OR source_id=$2) AND ($3='' OR target_scope=$3) AND ($4='' OR action=$4)
+ AND ($5='' OR ($5='active' AND status NOT IN ('completed','cancelled')) OR status=$5)
+ AND ($6='' OR stage=$6) AND ($7='' OR id=$7) AND ($10='' OR target_kb=$10)
+ AND ($11='' OR updated_at>=NULLIF($11,'')::timestamptz) AND ($12='' OR updated_at<=NULLIF($12,'')::timestamptz)
+) `
+
+func (s *Store) ListMonitorTasks(ctx context.Context, f MonitorTaskFilter) (MonitorTaskPage, error) {
+ if f.Limit < 1 || f.Limit > 100 || f.Offset < 0 {
+ return MonitorTaskPage{}, errors.New("invalid monitor pagination")
+ }
+ var raw []byte
+ err := s.db.QueryRow(ctx, monitorTasksCTE+`SELECT jsonb_build_object('items',COALESCE((SELECT jsonb_agg(to_jsonb(x)||jsonb_build_object('history',COALESCE((SELECT jsonb_agg(e ORDER BY e.at DESC,e.kind,e.detail) FROM (SELECT * FROM history_events ORDER BY at DESC,kind,detail LIMIT 50)e),'[]'::jsonb)) ORDER BY x.updated_at DESC,x.id) FROM (SELECT * FROM filtered ORDER BY updated_at DESC,id LIMIT $8 OFFSET $9)x),'[]'::jsonb),'total',(SELECT count(*) FROM filtered),'limit',$8::int,'offset',$9::int,'retryable',(SELECT count(*) FROM filtered WHERE retry_kind<>''),'sources',COALESCE((SELECT jsonb_agg(x ORDER BY x.name) FROM (SELECT DISTINCT source_id AS id,source_name AS name FROM projected)x),'[]'::jsonb),'targets',COALESCE((SELECT jsonb_agg(x ORDER BY x.name) FROM (SELECT DISTINCT target_kb AS id,target_kb AS name FROM projected)x),'[]'::jsonb))`, f.Search, f.Source, f.Scope, f.Action, f.Status, f.Stage, f.ID, f.Limit, f.Offset, f.Target, f.Since, f.Until).Scan(&raw)
+ if err != nil {
+ return MonitorTaskPage{}, err
+ }
+ var result MonitorTaskPage
+ err = json.Unmarshal(raw, &result)
+ return result, err
+}
diff --git a/internal/platform/postgres/monitor_tasks_integration_test.go b/internal/platform/postgres/monitor_tasks_integration_test.go
new file mode 100644
index 0000000..d3ba7e7
--- /dev/null
+++ b/internal/platform/postgres/monitor_tasks_integration_test.go
@@ -0,0 +1,105 @@
+package postgres
+
+import (
+ "context"
+ "github.com/jackc/pgx/v5/pgxpool"
+ "os"
+ "testing"
+ "time"
+)
+
+func TestMonitorTasksUnifiedPaginationIntegration(t *testing.T) {
+ url := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
+ if url == "" {
+ t.Skip("TEST_DATABASE_URL is not set")
+ }
+ ctx := context.Background()
+ pool, err := pgxpool.New(ctx, url)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer pool.Close()
+ if err = ApplyMigrations(ctx, pool); err != nil {
+ t.Fatal(err)
+ }
+ exec := func(q string) {
+ t.Helper()
+ if _, e := pool.Exec(ctx, q); e != nil {
+ t.Fatal(e)
+ }
+ }
+ exec(`INSERT INTO source_connections (id,source_type,name) VALUES ('conn-manual','manual','Manual')`)
+ now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
+ insertReviewFixture(t, ctx, pool, 4, "mt-ready", now)
+ insertReviewFixture(t, ctx, pool, 5, "mt-remove", now)
+ insertReviewFixture(t, ctx, pool, 6, "mt-removed", now)
+ exec(`UPDATE source_items SET policy_state='blocked' WHERE id='si-review-5'`)
+ exec(`UPDATE test_publications SET retrieval_state='disabled' WHERE id='mt-removed'`)
+ store := NewStore(pool)
+ page, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Limit: 1})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if page.Total != 3 || len(page.Items) != 1 {
+ t.Fatalf("global pagination: %#v", page)
+ }
+ active, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Status: "active", Limit: 100})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if active.Total != 1 || active.Items[0].ID != "mt-remove" || active.Items[0].Action != "remove" || active.Items[0].Status != "waiting" {
+ t.Fatalf("removal is not completed: %#v", active)
+ }
+ done, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Action: "remove", Status: "completed", Limit: 100})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if done.Total != 1 || done.Items[0].ID != "mt-removed" || done.Items[0].RetryKind != "" {
+ t.Fatalf("removed is not ready/retryable: %#v", done)
+ }
+ empty, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Limit: 1, Offset: 999})
+ if err != nil || empty.Total != 3 || len(empty.Items) != 0 {
+ t.Fatalf("empty last page lost total: %#v %v", empty, err)
+ }
+ exec(`UPDATE test_publications SET retrieval_state='failed' WHERE id='mt-remove'`)
+ exec(`INSERT INTO jobs(id,type,idempotency_key,payload,state) VALUES('mt-removal-job','test.remove','mt-removal-key','{"publication_id":"mt-remove"}','failed')`)
+ exec(`INSERT INTO job_failures(id,job_id,job_type,publication_id,error_code,attempt,max_attempts_at_failure,last_error) VALUES('mt-failure','mt-removal-job','test.remove','mt-remove','removal_failed',8,8,'removal_failed')`)
+ if err = store.RetryMonitorRemoval(ctx, "mt-remove", "tester", now); err != nil {
+ t.Fatalf("retry removal: %v", err)
+ }
+ if err = store.RetryMonitorRemoval(ctx, "mt-remove", "tester", now); err != ErrPublicationStateConflict {
+ t.Fatalf("duplicate retry accepted: %v", err)
+ }
+ detail, e := store.ListMonitorTasks(ctx, MonitorTaskFilter{ID: "mt-remove", Limit: 1})
+ if e != nil || len(detail.Items) != 1 || len(detail.Items[0].History) < 3 {
+ t.Fatalf("missing version/failure/retry history: %#v %v", detail, e)
+ }
+ var n int
+ if err = pool.QueryRow(ctx, `SELECT count(*) FROM jobs WHERE type='test.upload'`).Scan(&n); err != nil || n != 0 {
+ t.Fatalf("removal created upload: %d %v", n, err)
+ }
+ if err = pool.QueryRow(ctx, `SELECT count(*) FROM jobs WHERE id='mt-removal-job' AND state='pending'`).Scan(&n); err != nil || n != 1 {
+ t.Fatalf("not enqueued: %d %v", n, err)
+ }
+ exec(`UPDATE jobs SET state='failed' WHERE id='mt-removal-job'`)
+ exec(`UPDATE source_items SET policy_state='internal' WHERE id='si-review-5'`)
+ // A new version replaces the main row; routing that version to another KB adds one row.
+ exec(`INSERT INTO source_item_versions(id,source_item_id,revision,content_state,raw_record_summary) VALUES('mt-v-new','si-review-4','new-revision','verified','{}')`)
+ exec(`INSERT INTO test_publications(id,source_item_version_id,test_kb_id,target_scope,processing_state,created_at) SELECT 'mt-new','mt-v-new',test_kb_id,'internal','processing',created_at+interval '1 second' FROM test_publications WHERE id='mt-ready'`)
+ exec(`INSERT INTO test_publications(id,source_item_version_id,test_kb_id,target_scope,processing_state) VALUES('mt-external','mt-v-new','external-kb','external','planned')`)
+ versions, e := store.ListMonitorTasks(ctx, MonitorTaskFilter{Search: "mt-ready", Limit: 20})
+ if e != nil || versions.Total != 2 {
+ t.Fatalf("file-target identity: %#v %v", versions, e)
+ }
+ for _, v := range versions.Items {
+ if v.ID == "mt-ready" {
+ t.Fatal("old version escaped dedup")
+ }
+ if v.ID == "mt-new" && (v.Action != "update" || v.Stage != "parsing") {
+ t.Fatalf("latest action/stage: %#v", v)
+ }
+ }
+ if err = store.RetryMonitorRemoval(ctx, "mt-remove", "tester", now); err != ErrPublicationStateConflict {
+ t.Fatalf("changed policy accepted: %v", err)
+ }
+}
diff --git a/web/src/WeKnoraMonitor.test.tsx b/web/src/WeKnoraMonitor.test.tsx
index eba2060..bb5dd46 100644
--- a/web/src/WeKnoraMonitor.test.tsx
+++ b/web/src/WeKnoraMonitor.test.tsx
@@ -1,99 +1,105 @@
import { renderToStaticMarkup } from 'react-dom/server'
-import { describe,expect,it,vi } from 'vitest'
+import { describe, expect, it, vi } from 'vitest'
import { WeKnoraMonitorView } from './WeKnoraMonitor'
-import type { MonitorFilePage,MonitorOverview,RunMetrics } from './monitor'
-import type { TestPublicationPage } from './testPublications'
+import type { MonitorTaskPage } from './monitorTasks'
-const overview:MonitorOverview={mode:'simulation',simulated:true,observed_at:'2026-09-08T12:00:00Z',host:{cpu_percent:42,memory_used_bytes:19971597926,memory_total_bytes:34359738368,disk_used_bytes:128849018880,disk_total_bytes:536870912000,gpu_state:'unknown',gpu_detail:'未接入真实GPU监控',simulated:true},services:[{key:'weknora',name:'WeKnora API',state:'healthy',detail:'模拟连接正常',latency_ms:86,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'mineru',name:'MinerU',state:'healthy',detail:'模拟处理中2,队列5',latency_ms:24,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'postgres',name:'PostgreSQL',state:'unknown',detail:'未接入真实监控',latency_ms:0,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'redis',name:'Redis / MQ',state:'unknown',detail:'未接入真实监控',latency_ms:0,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'oss',name:'OSS',state:'unknown',detail:'未接入真实监控',latency_ms:0,observed_at:'2026-09-08T12:00:00Z',simulated:true}],knowledge_bases:[{id:'kb-sim',name:'公司级技术支持知识库',visibility:'mixed',imported:5750,completed:4530,pending:450,processing:50,finalizing:714,failed:6,cancelled:0,planned:120,needs_removal:3,stale_planned:0,internal_imported:4200,internal_completed:3300,external_imported:1550,external_completed:1230}],pipeline:{fetch_pending:4,fetch_failed:1,fetch_missing:0,concurrency:2},health:[]}
-const files:MonitorFilePage={items:[{name:'[模拟] 产品参数总表.xlsx',directory:'02|产品资料',visibility:'external',status:'finalizing',stage_detail:'基础检索可用,Wiki与图谱仍在生成',elapsed_seconds:2100,error_code:''}],total:1,limit:20,offset:0,sample_only:true}
-const metrics:RunMetrics={observed_at:'2026-09-12T00:00:00Z',active_workers:2,worker_source:'pg_lease',queue:{pending:3,running:4,retry:1,failed:2,oldest_pending_age_seconds:120},short_window:{window_seconds:3600,jobs_started:50,jobs_completed:48,jobs_failed:6,success_rate:88.9,failure_rate:11.1,duration_avg_seconds:55,duration_p50_seconds:30,duration_p95_seconds:1800},long_window:{window_seconds:86400,jobs_started:500,jobs_completed:482,jobs_failed:18,success_rate:96.4,failure_rate:3.6,duration_avg_seconds:70,duration_p50_seconds:45,duration_p95_seconds:2400}}
-const queue:TestPublicationPage={items:[{id:'tp-plan',title:'API.docx',ingest_state:'pending',processing_state:'planned',retrieval_state:'unpublished',target_scope: 'internal', change_set_id: '', change_type:'added',can_start:true,retry_count:0,last_error:'',can_retry:false,updated_at:'2026-09-09T10:01:00Z'},{id:'tp-fetch',title:'焊接手册.pdf',ingest_state:'pending',processing_state:'planned',retrieval_state:'unpublished',target_scope:'external',change_set_id:'',change_type:'added',can_start:false,retry_count:0,last_error:'',can_retry:false,updated_at:'2026-09-09T10:02:00Z'},{id:'tp-failed',title:'CR20A.pdf',ingest_state:'uploaded',processing_state:'failed',retrieval_state:'unpublished',target_scope: 'external', change_set_id: '', change_type:'updated',can_start:false,retry_count:1,last_error:'remote_parse_failed',can_retry:true,updated_at:'2026-09-09T10:00:00Z'}],total:3,limit:50,offset:0}
-const plans=queue.items.filter((item)=>item.can_start)
-const baseProps={overview,files,queue,plans,selectedKB:overview.knowledge_bases[0],status:'' as const,queueStatus:'' as const,queueScope:'' as const,loading:false,error:null as string|null,retryingIDs:[] as string[],starting:false,startMessage:'',onSelectKB:vi.fn(),onStatus:vi.fn(),onQueueStatus:vi.fn(),onQueueScope:vi.fn(),lane:'queue',onLaneChange:vi.fn(),metrics,onRetry:vi.fn(),onBatchRetry:vi.fn(),onStart:vi.fn(),onRefresh:vi.fn()}
-describe('WeKnoraMonitorView',()=>{
- it('filters the processing queue by target knowledge base and shows each task target',()=>{
- const html=renderToStaticMarkup( )
- expect(html).toContain('aria-label="队列目标知识库"')
- expect(html).toContain('对内知识库')
- expect(html).toContain('对外知识库')
- expect(html).toContain('对内知识库')
- expect(html).toContain('目标知识库')
- expect(html).toContain('scope-badge internal">对内')
- expect(html).toContain('scope-badge external">对外')
- })
- it('treats the catalog annotation as the only decision: 待入库 lists plans with one 开始处理 button and no reason form',()=>{
- const waiting=renderToStaticMarkup( )
- expect(waiting).toContain('待入库(2)')
- expect(waiting).toContain('开始处理(1)')
- expect(waiting).toContain('并发上限(当前 2)')
- expect(waiting).not.toContain('确认原因')
- expect(waiting).not.toContain('操作人 {
- const removal=renderToStaticMarkup( )
- expect(removal).toContain('需下架(3)')
- expect(removal).toContain('3 份仍在检索中')
- const withItems = renderToStaticMarkup( as never)
- expect(withItems).toContain('相机线束固定孔位尺寸图.pdf')
- expect(withItems).toContain('下架(从测试库移除)')
- // 确认态在视图内部(不依赖容器传参):初始渲染必须是普通按钮,且不得出现确认/取消,
- // 避免"点一下就直接执行"的降级。
- expect(withItems).toContain('下架(从测试库移除)')
- expect(withItems).not.toContain('确认下架(从测试库移除)')
- })
- it('shows inbox lanes for the knowledge-base diff view',()=>{
- const attention=renderToStaticMarkup( )
- for(const text of ['需要你出手','CR20A.pdf','remote_parse_failed','失败阶段 文档解析','批量重试所选(1)','死信(重试次数用完,系统不再自动处理)','源头取件”=从钉钉/AList'])expect(attention).toContain(text)
- const withFailures=renderToStaticMarkup( )
- expect(withFailures).toContain('「ATOM整机出厂测试.adoc」')
- expect(withFailures).toContain('来源服务当时不可达')
- const inflight=renderToStaticMarkup( )
- for(const text of ['处理中','知识库解析进度','已导入 5750','待入库 120','对内 3300 / 4200','对外 1230 / 1550','源头取件 → Hash'])expect(inflight).toContain(text)
- const queueView=renderToStaticMarkup( )
- expect(queueView).toContain('全部任务(3)')
- expect(queueView).toContain('重试')
- expect(queueView).toContain('1小时·成功')
- expect(queueView).toContain('88.9%')
- const nav=renderToStaticMarkup( )
- for(const text of ['待入库','需人工处理','需下架','处理中','已完成','全部任务'])expect(nav).toContain(text)
- })
- it('does not show simulated success totals after a load error without data',()=>{
- const html=renderToStaticMarkup( )
- expect(html).toContain('加载失败')
- expect(html).not.toContain('completed 0')
- })
- it('keeps attention items visible when they are outside the first queue page',()=>{
- const firstPage:TestPublicationPage={items:[queue.items[0]],total:115,limit:50,offset:0}
- const attentionItem=queue.items[2]
- const html=renderToStaticMarkup( )
- expect(html).toContain('需要你出手(1)')
- expect(html).toContain('CR20A.pdf')
- expect(html).toContain('批量重试所选(1)')
- })
- it('offers to page through the full queue when more rows exist',()=>{
- const firstPage:TestPublicationPage={items:queue.items,total:115,limit:50,offset:0}
- const html=renderToStaticMarkup( )
- expect(html).toContain('已显示 3 / 共 115')
- expect(html).toContain('加载更多')
- })
- it('offers 重新入队 for a 源头取件 dead letter that carries no publication id',()=>{
- // 取件死信没有 publication_id,所以不能走发布重试链路;没有这个按钮,一个终态
- // 取件失败(例如钉钉会话过期)在界面上就永远救不回来。
- const failures=[{id:'jf-fetch',job_id:'job-fetch',job_type:'artifact.fetch',error_code:'artifact_invalid',attempt:1,max_attempts_at_failure:8,state:'open' as const,created_at:'2026-10-08T03:20:35Z',doc_name:'关闭PC2远程桌面弹窗.docx',last_error:'artifact_fetch_rejected: http_401'}]
- const html=renderToStaticMarkup( )
- expect(html).toContain('源头取件')
- expect(html).toContain('关闭PC2远程桌面弹窗.docx')
- expect(html).toContain('重新入队')
- const busy=renderToStaticMarkup( )
- expect(busy).toContain('重排中…')
- expect(busy).not.toContain('>重新入队<')
- })
- it('does not offer 重新入队 for a 上传解析 dead letter without a publication id',()=>{
- const failures=[{id:'jf-upload',job_id:'job-upload',job_type:'test.upload',error_code:'handler_error',attempt:8,max_attempts_at_failure:8,state:'open' as const,created_at:'2026-10-08T03:20:35Z',last_error:'remote_parse_failed'}]
- const html=renderToStaticMarkup( )
- expect(html).toContain('上传解析')
- // 段落提示语里始终有“重新入队=再给它一次机会”,所以断言按钮本身不存在。
- expect(html).not.toContain('>重新入队')
- })
+const page: MonitorTaskPage = {
+ items: [
+ {
+ id: 'tp-1',
+ title: '维护手册.pdf',
+ path: '产品/维护',
+ source_id: 's1',
+ source_name: '技术资料',
+ target_scope: 'internal',
+ target_kb: 'test-kb',
+ action: 'update',
+ stage: 'parsing',
+ status: 'attention',
+ detail: 'remote_parse_failed',
+ updated_at: '2026-10-27T02:00:00Z',
+ retry_kind: 'upload',
+ failure_id: '',
+ retry_count: 2,
+ version_id: 'v1',
+ },
+ {
+ id: 'tp-2',
+ title: '过期资料.pdf',
+ path: '历史',
+ source_id: 's1',
+ source_name: '技术资料',
+ target_scope: 'external',
+ target_kb: 'test-public',
+ action: 'remove',
+ stage: 'finished',
+ status: 'completed',
+ detail: '已从测试库移除;来源文件保留',
+ updated_at: '2026-10-27T02:01:00Z',
+ retry_kind: '',
+ failure_id: '',
+ retry_count: 0,
+ version_id: 'v2',
+ },
+ ],
+ total: 145,
+ limit: 20,
+ offset: 0,
+ sources: [{ id: 's1', name: '技术资料' }],
+ targets: [],
+ retryable: 1,
+}
+const props = {
+ overview: null,
+ metrics: null,
+ page,
+ loading: false,
+ error: '',
+ message: '',
+ filters: { q: '', source: '', scope: '', target: '', action: '', status: 'active', stage: '', since: '', until: '' },
+ selected: [],
+ busy: false,
+ canOperate: true,
+ onFilters: vi.fn(),
+ onPage: vi.fn(),
+ onSelection: vi.fn(),
+ onSelectAll: vi.fn(),
+ onRetry: vi.fn(),
+ onDetail: vi.fn(),
+ onRefresh: vi.fn(),
+}
+describe('unified monitor', () => {
+ it('shows metric cards before a single server-paginated file table without manual start or lane tabs', () => {
+ const html = renderToStaticMarkup( )
+ expect(html).toContain('monitor-metric-grid')
+ expect(html.indexOf('monitor-metric-grid')).toBeLessThan(html.indexOf('文件处理 '))
+ expect(html.match(/ {
+ const html = renderToStaticMarkup( )
+ expect(html).toContain('无法读取运行指标')
+ expect(html).not.toContain('100%')
+ expect(html).toContain('尚未取得服务状态')
+ })
+ it('keeps removal semantics and does not offer retry for completed rows', () => {
+ const html = renderToStaticMarkup( )
+ expect(html).toContain('已从测试库移除;来源文件保留')
+ expect(html).toContain('aria-label="选择维护手册.pdf"')
+ expect(html).not.toContain('aria-label="选择过期资料.pdf"')
+ })
})
diff --git a/web/src/WeKnoraMonitor.tsx b/web/src/WeKnoraMonitor.tsx
index 6cf5aa6..1ad0479 100644
--- a/web/src/WeKnoraMonitor.tsx
+++ b/web/src/WeKnoraMonitor.tsx
@@ -1,478 +1,749 @@
-import { useContext, useEffect, useMemo, useState } from 'react'
-
-import { fetchMonitorFiles, fetchMonitorOverview, fetchRunMetrics } from './monitor'
-import type { RunMetrics } from './monitor'
-import type { KnowledgeProgress, MonitorFilePage, MonitorOverview, MonitorStatus } from './monitor'
-import { fetchJobFailures, fetchTestPublications, retryPublication, retryPublicationsBatch, cancelPublicationsBatch, requeueJobFailure, startTestPublications, fetchRemovalRequiredPublications, startPublicationRemoval } from './testPublications'
-import type { JobFailure, ProcessingState, TestPublication, TestPublicationPage } from './testPublications'
+import { useContext, useEffect, useRef, useState } from 'react'
+import { fetchMonitorOverview, fetchRunMetrics } from './monitor'
+import type { MonitorOverview, RunMetrics, ServiceHealth } from './monitor'
+import {
+ fetchMonitorTasks,
+ defaultMonitorFilters,
+ canRetryTask,
+ actionLabels,
+ stageLabels,
+ taskStatusLabels,
+} from './monitorTasks'
+import type { MonitorFilters, MonitorTask, MonitorTaskPage } from './monitorTasks'
+import { requeueJobFailure, retryPublicationsBatch } from './testPublications'
import { fetchActiveUISettings, settingsRefreshDelays } from './uiSettings'
+import { useOptionalAuth } from './auth'
import { WorkspaceContextTabsSetter } from './workspaceContext'
-import './weknoraMonitor.css'
+import './monitorWorkbench.css'
-const emptyQueue: TestPublicationPage = { items: [], total: 0, limit: 50, offset: 0 }
-const gb = (value: number) => `${(value / 1073741824).toFixed(1)}`
-const scope = (value: string) => value === 'internal' ? '对内' : value === 'external' ? '对外' : '混合'
-const duration = (seconds: number) => seconds <= 0 ? '完成' : seconds < 60 ? `${seconds}秒` : seconds < 3600 ? `${Math.floor(seconds / 60)}分钟` : `${(seconds / 3600).toFixed(1)}小时`
-const statusLabels: Record = { completed: 'completed', pending: 'pending', processing: 'processing', finalizing: 'finalizing', failed: 'failed', cancelled: 'cancelled' }
-const processingLabels: Record = { planned: '待入库', pending: '等待解析', processing: '解析中', finalizing: '索引与增强', completed: '成功', failed: '失败', stalled: '停滞', cancelled: '已取消' }
-
-const secondsSince = (iso: string) => Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 1000))
-const adviceFor = (stage: string, code: string) => {
- if (stage === '文档解析') return '建议先在 WeKnora 手动解析该文件确认格式,再决定是否重试。'
- if (stage === '索引 / 增强') return '多为 MinerU 容量或超时问题,确认服务恢复接单后重试。'
- if (stage === 'WeKnora 上传') return '上传失败,确认 WeKnora 可达后重试;重复提交不会创建第二份知识。'
- if (stage === '源头取件') return '文件尚未从源头取回:钉钉来源需可导出的登录会话,AList/PP 需直连可用;恢复后系统会自动重试。'
- if (stage === 'Hash / 文件校验' || stage === 'OSS') return '文件或暂存校验问题,重新入队会重新拉取来源文件。'
- if (stage === '测试知识移除') return '移除失败,请先在测试知识库确认该文档当前状态。'
- if (stage === '治理策略') return '按治理策略阻止,请先在来源目录修正对内/对外标注。'
- if (code.includes('429')) return '远端容量已满,恢复后可一键重新入队。'
- return '确认错误码后可直接重试;尝试次数耗尽将进入死信队列。'
+const emptyPage: MonitorTaskPage = { items: [], total: 0, limit: 20, offset: 0, sources: [], targets: [], retryable: 0 }
+const time = (value: string) => (value ? new Date(value).toLocaleString('zh-CN', { hour12: false }) : '—')
+const percent = (value: number | null | undefined) => (value == null ? '—' : `${value}%`)
+const seconds = (value: number | null | undefined) => (value == null ? '—' : `${Number(value.toFixed(1))} 秒`)
+const gb = (n: number) => Number((n / 1073741824).toFixed(1))
+export function healthLabel(service: ServiceHealth) {
+ if (!service.applicable) return '不适用'
+ if (!service.configured || service.readiness === 'not_configured') return '未配置'
+ return (
+ (
+ { ready: '可接单', degraded: '降级', blocked: '阻断', down: '不可达', unknown: '未确认' } as Record<
+ string,
+ string
+ >
+ )[service.readiness] || '未确认'
+ )
}
-
-// failureHint turns a dead-letter row's machine code into a plain-Chinese
-// diagnosis of what actually went wrong and what to do.
-const failureHint = (failure: JobFailure) => {
- const detail = `${failure.error_code} ${failure.last_error || ''}`
- if (detail.includes('not ingestable')) return '钉钉侧该类型不是可下载文档(快捷方式、占位或内部对象),重试也不会成功;建议用“文件名筛选”把它排除,或在钉钉确认它是否为空目录残留。'
- if (detail.includes('source_auth_required')) return '来源存储需要网页登录授权(服务凭据未配置),请联系管理员在“系统设置”重新扫码。'
- if (detail.includes('invalid_format')) return '取回的内容不是有效的文档/文本格式,可能源头文件本身损坏;可下载原件确认后重试。'
- if (detail.includes('source_unavailable')) return '来源服务当时不可达(网络/接口抖动)。任务若已自动重试成功,这里只是历史记录;仍在列表说明源头仍不可达。'
- if (failure.error_code === 'test_upload_remote_failed' || detail.includes('test_upload_remote_failed')) return '向测试知识库上传被拒:确认 WeKnora 测试实例可达、目标知识库未停用后,点击重新入队。'
- if (detail.includes('empty_document')) return '钉钉文档正文为空(源头就是空文档),解析器拒收空内容;无需重试,在钉钉里补充内容后重新取件即可。'
- if (failure.error_code === 'remote_parse_failed') return '文档解析失败:建议先在 WeKnora 手动解析该文件确认格式,再重新入队。'
- if (failure.error_code === 'handler_error') return '处理环节报错,展开原始错误定位;确认原因后可重新入队。'
- return `错误码 ${failure.error_code}:确认原因后可重新入队。`
-}
-
-const pipelineStages = ['源头取件', 'Hash', 'OSS', '文档解析', 'WeKnora 上传', '索引', '完成']
-const stageIndex = (label: string) => label === 'Hash / 文件校验' ? 1 : label === 'OSS' ? 2 : label === '文档解析' ? 3 : label === 'WeKnora 上传' ? 4 : label === '索引 / 增强' || label === '测试知识移除' || label === '治理策略' || label === '处理链路' ? 5 : -1
-function chainSteps(item: TestPublication) {
- const broken = item.processing_state === 'failed' || item.processing_state === 'stalled'
- const stopAt = broken ? stageIndex(failureStage(item)) : -1
- const activeAt = item.processing_state === 'completed' ? 7 : item.processing_state === 'finalizing' ? 5 : item.processing_state === 'processing' ? 3 : item.processing_state === 'planned' && !item.can_start ? 0 : item.processing_state === 'pending' ? 1 : 0
- return pipelineStages.map((stage, index) => {
- if (stopAt >= 0 && index === stopAt) return { stage, state: 'bad' as const }
- if (stopAt >= 0) return { stage, state: index < stopAt ? 'done' as const : 'todo' as const }
- if (index < activeAt) return { stage, state: 'done' as const }
- if (index === activeAt && item.processing_state !== 'pending' && item.processing_state !== 'completed' && item.processing_state !== 'planned') return { stage, state: 'active' as const }
- if ((item.processing_state === 'pending' || (item.processing_state === 'planned' && !item.can_start)) && index === 0) return { stage, state: item.can_start || item.processing_state === 'pending' ? 'done' as const : 'active' as const }
- return { stage, state: 'todo' as const }
- })
-}
-
-function failureStage(item: TestPublication) {
- const code = item.last_error
- if (code.includes('artifact') || code.includes('fetch')) return '源头取件'
- if (code.includes('hash')) return 'Hash / 文件校验'
- if (code.includes('oss')) return 'OSS'
- if (code.includes('upload')) return 'WeKnora 上传'
- if (code.includes('parse')) return '文档解析'
- if (code.includes('stalled') || code.includes('index')) return '索引 / 增强'
- if (code.includes('removal')) return '测试知识移除'
- if (code.includes('blocked')) return '治理策略'
- return item.processing_state === 'failed' ? '处理链路' : '—'
-}
-
interface ViewProps {
overview: MonitorOverview | null
- files: MonitorFilePage | null
- queue?: TestPublicationPage
- plans?: TestPublication[]
- attention?: TestPublication[]
- loadingMore?: boolean
- selectedKB: KnowledgeProgress | null
- status: MonitorStatus | ''
- queueStatus?: ProcessingState | ''
- queueScope?: 'internal' | 'external' | ''
+ metrics: RunMetrics | null
+ page: MonitorTaskPage
+ filters: MonitorFilters
+ selected: MonitorTask[]
loading: boolean
- error: string | null
- retryingIDs?: string[]
- requeueingIDs?: string[]
- cancellingIDs?: string[]
- starting?: boolean
- startMessage?: string
- batchMessage?: string
- failures?: JobFailure[]
- metrics?: RunMetrics | null
- onSelectKB: (kb: KnowledgeProgress) => void
- onStatus: (status: MonitorStatus | '') => void
- onQueueStatus: (value: ProcessingState | '') => void
- onQueueScope: (value: 'internal' | 'external' | '') => void
- lane?: string
- onLaneChange?: (lane: string) => void
- onRetry?: (id: string) => void
- onBatchRetry?: (ids: string[]) => void
- onBatchCancel?: (ids: string[]) => void
- onRequeueFailure?: (id: string) => void
- onStart?: (ids: string[]) => void
- onLoadMore?: () => void
+ busy: boolean
+ error: string
+ message: string
+ canOperate: boolean
+ onFilters: (value: MonitorFilters) => void
+ onPage: (offset: number) => void
+ onSelection: (rows: MonitorTask[]) => void
+ onSelectAll: () => void
+ onRetry: (rows: MonitorTask[]) => void
+ onDetail: (row: MonitorTask) => void
onRefresh: () => void
- /** 需下架车道的条目列表(被禁止导入但仍存活在测试库),以及移除动作。 */
- removalItems?: TestPublication[]
- removalBusyIDs?: string[]
- removalMessage?: string
- onStartRemoval?: (id: string) => void
}
-
-export function WeKnoraMonitorView({
- overview,
- files,
- queue = emptyQueue,
- plans = queue.items,
- attention,
- selectedKB,
- status,
- queueStatus = '',
- queueScope = '',
- lane = 'waiting',
- onLaneChange = () => undefined,
- loading,
- error,
- retryingIDs = [],
- requeueingIDs = [],
- cancellingIDs = [],
- starting = false,
- loadingMore = false,
- startMessage = '',
- batchMessage = '',
- failures = [],
- metrics = null,
- onSelectKB,
- onStatus,
- onQueueStatus = () => undefined,
- onQueueScope = () => undefined,
- onRetry = () => undefined,
- onBatchRetry = () => undefined,
- onBatchCancel = () => undefined,
- onRequeueFailure = () => undefined,
- onStart = () => undefined,
- onLoadMore = () => undefined,
- onRefresh,
- removalItems = [],
- removalBusyIDs = [],
- removalMessage = '',
- onStartRemoval = () => undefined,
-}: ViewProps) {
- const [selectedStartIDs, setSelectedStartIDs] = useState([])
- const [selectedRetryIDs, setSelectedRetryIDs] = useState([])
- // 破坏性动作的二次确认状态放在视图内部:容器漏传参也不会把"确认"这一步静默降级掉。
- const [confirmRemovalID, setConfirmRemovalID] = useState('')
- const toggleStartSelection = (id: string) => setSelectedStartIDs((current) => current.includes(id) ? current.filter((value) => value !== id) : [...current, id])
- const toggleRetrySelection = (id: string) => setSelectedRetryIDs((current) => current.includes(id) ? current.filter((value) => value !== id) : [...current, id])
- const stages: Array<[MonitorStatus, number]> = selectedKB ? [
- ['completed', selectedKB.completed], ['pending', selectedKB.pending], ['finalizing', selectedKB.finalizing],
- ['processing', selectedKB.processing], ['failed', selectedKB.failed], ['cancelled', selectedKB.cancelled],
- ] : []
- const orderedQueue = useMemo(() => [...queue.items].sort((left, right) => {
- const priority = (item: TestPublication) => item.processing_state === 'failed' || item.processing_state === 'stalled' ? 0 : 1
- return priority(left) - priority(right)
- }), [queue.items])
- // Batch actions must target every failing item the lane shows, not just the
- // rows that happen to sit inside the current queue page.
- const retryableIDs = (attention ?? queue.items).filter((item) => item.can_retry).map((item) => item.id)
- const batchTargets = selectedRetryIDs.length ? retryableIDs.filter((id) => selectedRetryIDs.includes(id)) : retryableIDs
- const startablePlans = plans.filter((item) => item.can_start)
- const startTargets = selectedStartIDs.length ? startablePlans.filter((item) => selectedStartIDs.includes(item.id)).map((item) => item.id) : startablePlans.map((item) => item.id)
- // Failures needing a human must never be hidden by the queue page window:
- // prefer the dedicated attention fetch, fall back to the visible page.
- const attentionItems = (attention ?? queue.items).filter((item) => item.processing_state === 'failed' || item.processing_state === 'stalled')
- const inflightItems = queue.items.filter((item) => item.processing_state === 'processing' || item.processing_state === 'finalizing' || (item.processing_state === 'pending' && !item.can_start))
- const doneItems = queue.items.filter((item) => item.processing_state === 'completed')
- const plannedItems = queue.items.filter((item) => item.processing_state === 'planned')
- const fetchingPlanned = plannedItems.filter((item) => !item.can_start)
- const laneCounts: Record = {
- waiting: startablePlans.length + fetchingPlanned.length,
- attention: attentionItems.length + failures.length,
- removal: (overview?.knowledge_bases || []).reduce((total, kb) => total + (kb.needs_removal || 0), 0),
- inflight: inflightItems.length,
- done: doneItems.length,
- queue: queue.total,
- }
- const laneTitles: Record = { waiting: '待入库', attention: '需人工处理', removal: '需下架', inflight: '处理中', done: '已完成', queue: '全部任务' }
-
- const actionTask = (item: TestPublication) =>
- {item.can_start ? toggleStartSelection(item.id)} /> : item.can_retry ? toggleRetrySelection(item.id)} /> : null}
-
-
{item.title} {scope(item.target_scope)} {item.change_type === 'added' ? 新增 : item.change_type === 'updated' ? 更新 : null}
-
{item.processing_state === 'failed' || item.processing_state === 'stalled' ? <>{item.processing_state === 'stalled' ? '任务停滞:' : '为什么失败:'}错误码 {item.last_error || '—'} · 第{item.retry_count + 1}次尝试 · 已停留{duration(secondsSince(item.updated_at))} · 失败阶段 {failureStage(item)}> : <>状态 {processingLabels[item.processing_state]}{item.last_error ? <> · 最近错误 {item.last_error}> : null} · 已停留{duration(secondsSince(item.updated_at))}>}
-
{chainSteps(item).map((step) => {step.stage}{step.state === 'done' ? ' ✓' : step.state === 'bad' ? ' ✗' : step.state === 'active' ? ` 中 ${duration(secondsSince(item.updated_at))}` : ''} )}
-
{item.processing_state === 'failed' || item.processing_state === 'stalled' ? adviceFor(failureStage(item), item.last_error) : item.processing_state === 'planned' && !item.can_start ? '正在从源头取回文件,完成后自动进入可处理状态' : item.can_start ? '文件已就绪,等待开始处理' : '无需操作,等待自动处理'} · {item.id}
-
- {item.can_retry ? onRetry(item.id)}>{retryingIDs.includes(item.id) ? '重试中…' : '重试'} : null}{item.can_retry ? 0} title="不再自动重试;原始记录保留,重新标记入库可再次处理" onClick={() => onBatchCancel([item.id])}>{cancellingIDs.includes(item.id) ? '取消中…' : '不再处理'} : null}
-
- const emptyLane = (text: string) => {text}
- return
-
- {error ? {error}
: null}
- {overview?.simulated ? 本地模拟数据 资源和知识库汇总用于确认页面流程,不代表测试服务器真实数据;任务卡片来自真实治理 API。
: null}
-
-
- 知识库差异
- {[['waiting', '待入库', 'blocked'], ['attention', '需人工处理', ''], ['removal', '需下架', ''], ['inflight', '处理中', ''], ['done', '已完成', 'okall']].map(([value, label, tone]) => onLaneChange(value)}>{label} {laneCounts[value]} )}
- onLaneChange('queue')}>全部任务 {queue.total}
-
-
- {lane === 'waiting' ?
-
待入库({startablePlans.length + fetchingPlanned.length}) onStart(startTargets)}>{starting ? '正在生成任务…' : `开始处理(${startTargets.length})`}
- 来源目录中标记对内/对外的文件会自动出现在这里;“开始处理”按系统设置的并发上限(当前 {overview?.pipeline?.concurrency ?? 2})创建解析任务,无需再确认。
- {startMessage ? {startMessage}
: null}
- {plannedItems.length ? plannedItems.map(actionTask) : emptyLane('还没有标记入库的文件。去“来源与目录”把文件或目录标为对内/对外即可。')}
- {fetchingPlanned.length ? 其中 {fetchingPlanned.length} 项仍在从源头取件,取回后自动可处理。
: null}
- : null}
- {lane === 'attention' ?
-
需要你出手({attentionItems.length}) 0 || retryingIDs.length > 0} title="不再自动重试(如源头就是空文档);原始记录保留,重新标记入库可再次处理" onClick={() => onBatchCancel(batchTargets)}>{cancellingIDs.length ? '取消中…' : `批量取消所选(${batchTargets.length})`} 0} onClick={() => onBatchRetry(batchTargets)}>{retryingIDs.length ? '重试中…' : `批量重试所选(${batchTargets.length})`}
- {batchMessage ? {batchMessage}
: null}
- {attentionItems.length ? attentionItems.map(actionTask) : emptyLane('没有失败或停滞的任务。')}
-
死信(重试次数用完,系统不再自动处理) “源头取件”=从钉钉/AList 把原始文件拉回本地;“上传解析”=把文件送进知识库做解析。死信只是失败任务的登记表,不会改动原始记录;重新入队=再给它一次机会
- {failures.length ? {failures.map((failure) => {failure.error_code} {failure.job_type === 'test.upload' ? '上传解析' : failure.job_type === 'artifact.fetch' ? '源头取件' : failure.job_type} {failure.doc_name ? 「{failure.doc_name}」 : failure.publication_id ? {failure.publication_id} : null}{failure.attempt}/{failure.max_attempts_at_failure} 次 · {new Date(failure.created_at).toLocaleString('zh-CN', { hour12: false })} {failureHint(failure)} {failure.job_type === 'artifact.fetch' ? onRequeueFailure(failure.id)}>{requeueingIDs.includes(failure.id) ? '重排中…' : '重新入队'} : failure.publication_id ? onBatchRetry([failure.publication_id as string])}>重新入队 : null} )} : emptyLane('当前没有等待处理的死信记录。')}
- : null}
- {lane === 'removal' ?
-
需下架({laneCounts.removal}) 来源目录里被改为“禁止导入”或撤销标注、但仍在知识库中可检索的文件
- {removalMessage ? {removalMessage}
: null}
- {removalItems.length ? {removalItems.map((item) => 需下架 「{item.title}」 {scope(item.target_scope)} · 知识仍在测试库中可检索 {confirmRemovalID === item.id ? <> setConfirmRemovalID('')}>取消 { setConfirmRemovalID(''); onStartRemoval(item.id) }}>{removalBusyIDs.includes(item.id) ? '下架中…' : '确认下架(从测试库移除)'} > : setConfirmRemovalID(item.id)}>下架(从测试库移除) } )} : null}
- {(overview?.knowledge_bases || []).some((kb) => kb.needs_removal > 0) ? {(overview?.knowledge_bases || []).filter((kb) => kb.needs_removal > 0).map((kb) => 需下架 {kb.name} {kb.needs_removal} 份仍在检索中 )} : removalItems.length ? null : emptyLane('没有标注为禁止但仍留在知识库里的文件。')}
- 这里只负责把差异暴露出来:来源项被判“禁止导入”后,仍存活在测试库里的知识必须移除。逐条点“下架(从测试库移除)”并二次确认后,走 test.remove 工作流删除测试库里的知识条目(会读回校验;重新标注后可再次入库,不影响生产知识库)。
- : null}
- {lane === 'inflight' ?
-
处理中({inflightItems.length}) 无需操作,仅供查看
- {inflightItems.length ? inflightItems.map(actionTask) : emptyLane('当前没有正在处理或索引增强的任务。')}
- {overview ? <>
- 源头取件 → Hash → OSS → 文档解析 → WeKnora 上传 → 索引 → 完成
-
-
知识库解析进度 {overview.knowledge_bases.map((kb) => onSelectKB(kb)}>{kb.name} {scope(kb.visibility)} · 已导入 {kb.imported}{kb.planned ? ` · 待入库 ${kb.planned}` : ''} {kb.completed}完成 )}
-
{selectedKB ? <>
-
{selectedKB.name} {scope(selectedKB.visibility)} · 已导入 {selectedKB.imported}{selectedKB.planned ? ` · 待入库 ${selectedKB.planned}` : ''} 对内 {selectedKB.internal_completed} / {selectedKB.internal_imported} 对外 {selectedKB.external_completed} / {selectedKB.external_imported}
-
onStatus('')}>全部 {selectedKB.imported} {stages.filter(([, count]) => count > 0).map(([value, count]) => onStatus(value)}>{statusLabels[value]} {count} )}
-
finalizing: 基础解析、切片和向量通常已完成,Wiki、摘要或图谱仍在生成;不能算全部完成。
- {files?.sample_only ?
当前只展示本地模拟文件样例;真实模式会列出所选状态下的实际文件。
: null}
- {files?.items.length ?
文件名 来源目录 对内/对外 当前阶段 已停留 说明 {files.items.map((file) => {file.name} {file.directory} {scope(file.visibility)} {file.status} {duration(file.elapsed_seconds)} {file.stage_detail}{file.error_code ? {file.error_code} : null} )}
: !loading ?
当前筛选没有文件。
: null}
- > : null}
+export function WeKnoraMonitorView(p: ViewProps) {
+ const { overview, metrics, page, filters, selected, busy, loading } = p
+ const patchFilter = (key: keyof MonitorFilters, value: string) => p.onFilters({ ...filters, [key]: value })
+ const toggle = (item: MonitorTask) =>
+ p.onSelection(
+ selected.some((r) => r.id === item.id) ? selected.filter((r) => r.id !== item.id) : [...selected, item]
+ )
+ const eligible = page.items.filter(canRetryTask)
+ const selectedIDs = new Set(selected.map((r) => r.id))
+ const allPage = eligible.length > 0 && eligible.every((r) => selectedIDs.has(r.id))
+ const kbName = (id: string) => overview?.knowledge_bases.find((k) => k.id === id)?.name || id
+ return (
+
+
+
+ {overview ? `快照 ${time(overview.observed_at)}` : '等待运行快照'}
+
+ {loading ? '刷新中…' : '刷新'}
+
+
+ {p.error ? (
+
+ {p.error}
+
+ ) : null}
+ {overview?.simulated || metrics?.worker_source === 'simulation' ? (
+ 运行指标含模拟数据;下方文件状态来自治理数据库。
+ ) : null}
+
+
+
+ 任务成功率 全部任务口径
+
+
+
+ {percent(metrics?.short_window.success_rate)}
+ 最近 1 小时
- > : null}
- : null}
- {lane === 'done' ?
-
已完成({doneItems.length}) 最近一页数据中的成功任务
- {doneItems.length ? doneItems.map(actionTask) : emptyLane('当前页没有已完成任务;用全部任务视图翻页查看。')}
- : null}
- {lane === 'queue' ?
-
全部任务({queue.total}) 目标知识库 onQueueScope(event.target.value as 'internal' | 'external' | '')}>内/外全部 对内知识库 对外知识库 onQueueStatus(event.target.value as ProcessingState | '')}>全部状态 待入库 等待解析 解析中 索引与增强 成功 失败 停滞 已取消
- {orderedQueue.length ? orderedQueue.map(actionTask) : !loading ? emptyLane('当前筛选没有解析任务。') : null}
- {queue.items.length < queue.total ? 已显示 {queue.items.length} / 共 {queue.total} onLoadMore?.()}>{loadingMore ? '加载中…' : '加载更多'}
: null}
- : null}
- {metrics ?
1小时·成功 5 ? 'bad' : 'good'}>{metrics.short_window.success_rate === null ? '数据不足' : `${metrics.short_window.success_rate}%`}
24小时·成功 {metrics.long_window.success_rate === null ? '数据不足' : `${metrics.long_window.success_rate}%`}
p50 {metrics.short_window.duration_p50_seconds === null ? '—' : duration(Math.round(metrics.short_window.duration_p50_seconds))} · p95 {metrics.short_window.duration_p95_seconds === null ? '—' : duration(Math.round(metrics.short_window.duration_p95_seconds))}
Worker {metrics.active_workers} · 等待 {metrics.queue.pending} · 运行 {metrics.queue.running}{metrics.queue.failed ? · 失败 {metrics.queue.failed} : null}
{overview?.health.map((service) => {service.name} {service.readiness === 'ready' ? '可接单' : service.readiness === 'degraded' ? '降级' : service.readiness === 'not_configured' ? '未配置' : service.readiness === 'down' ? '不可达' : '阻断'}
)}{overview ? CPU {overview.host.cpu_percent.toFixed(0)}% · 内存 {gb(overview.host.memory_used_bytes)}/{gb(overview.host.memory_total_bytes)}GB
: null} : null}
-
-
-
+
+ {percent(metrics?.long_window.success_rate)}
+ 最近 24 小时
+
+
+
+
+
+ 处理耗时 最近 1 小时
+
+
+
+ {seconds(metrics?.short_window.duration_p50_seconds)}
+ p50 · 中位耗时
+
+
+ {seconds(metrics?.short_window.duration_p95_seconds)}
+ p95 · 处理耗时
+
+
+
+
+
+ 任务队列 全部任务口径
+
+
+
+ {metrics?.active_workers ?? '—'}
+ 活跃 Worker
+
+
+ {metrics?.queue.pending ?? '—'}
+ 等待
+
+
+ {metrics?.queue.running ?? '—'}
+ 运行
+
+
+ {metrics?.queue.failed ?? '—'}
+ 失败
+
+
+
+
+
+ 主机资源 当前
+
+
+
+ {overview ? `${overview.host.cpu_percent.toFixed(0)}%` : '—'}
+ CPU 使用率
+
+
+
+ {overview ? `${gb(overview.host.memory_used_bytes)} / ${gb(overview.host.memory_total_bytes)}` : '—'}
+
+ 内存 · GB
+
+
+
+
+
+
+
服务与依赖
+ 展开查看状态原因
+
+
+ {overview?.health.length ? (
+ overview.health.map((s) => (
+
+
+ {s.name}
+
+ ● {healthLabel(s)}
+
+
+ {s.reason || '未提供状态说明'}
+
+ 检测:{time(s.observed_at)} · {s.latency_ms} ms
+
+
+ ))
+ ) : (
+
尚未取得服务状态
+ )}
+
+
+
+
+
文件处理
+ 生产发布授权在「发布管理」
+
+
+ patchFilter('q', e.target.value)}
+ />
+ patchFilter('source', e.target.value)}
+ >
+ 全部来源
+ {page.sources.map((s) => (
+
+ {s.name}
+
+ ))}
+
+ patchFilter('scope', e.target.value)}
+ >
+ 对内 / 对外
+ 对内知识库
+ 对外知识库
+
+ patchFilter('target', e.target.value)}
+ >
+ 全部目标知识库
+ {page.targets.map((t) => (
+
+ {kbName(t.id)}
+
+ ))}
+
+ patchFilter('action', e.target.value)}
+ >
+ 全部处理动作
+ {Object.entries(actionLabels).map(([key, label]) => (
+
+ {label}
+
+ ))}
+
+ patchFilter('status', e.target.value)}
+ >
+ 未结束
+ 全部处理状态
+ {Object.entries(taskStatusLabels).map(([key, label]) => (
+
+ {label}
+
+ ))}
+
+ patchFilter('stage', e.target.value)}
+ >
+ 全部阶段
+ {Object.entries(stageLabels).map(([key, label]) => (
+
+ {label}
+
+ ))}
+
+
+ 时间范围{filters.since || filters.until ? ' · 已筛选' : ''}
+
+ 更新时间起
+ patchFilter('since', e.target.value ? new Date(e.target.value).toISOString() : '')}
+ />
+
+
+ 更新时间止
+ patchFilter('until', e.target.value ? new Date(e.target.value).toISOString() : '')}
+ />
+
+
+ p.onFilters({ ...defaultMonitorFilters, status: '' })}
+ >
+ 清除筛选
+
+
+ {p.canOperate ? (
+
+ 已选 {selected.length} 项
+ 只重试可恢复项,沿用原处理动作
+
+ 选择筛选内全部可重试项({page.retryable})
+
+ p.onRetry(selected)}
+ >
+ {busy ? '处理中…' : `重试所选(${selected.length})`}
+
+
+ ) : null}
+ {p.message ? (
+
+ {p.message}
+
+ ) : null}
+
+
+
+ 共 {page.total} 项 · 每页 {page.limit} 项
+
+
+ p.onPage(Math.max(0, page.offset - page.limit))}
+ >
+ 上一页
+
+
+ {Math.floor(page.offset / page.limit) + 1} / {Math.max(1, Math.ceil(page.total / page.limit))}
+
+ = page.total}
+ onClick={() => p.onPage(page.offset + page.limit)}
+ >
+ 下一页
+
+
+
+
+
+ )
}
-
-export function WeKnoraMonitor() {
- const [overview, setOverview] = useState
(null)
- const [selected, setSelected] = useState(null)
- const [files, setFiles] = useState(null)
- const [queue, setQueue] = useState(emptyQueue)
- const [plans, setPlans] = useState([])
- const [status, setStatus] = useState('')
- const [queueStatus, setQueueStatus] = useState('')
- const [queueScope, setQueueScope] = useState<'internal' | 'external' | ''>('')
- const [lane, setLane] = useState<'waiting' | 'attention' | 'removal' | 'inflight' | 'done' | 'queue'>('waiting')
- const [loading, setLoading] = useState(true)
- const [error, setError] = useState(null)
- const [documentRevision, setDocumentRevision] = useState(0)
- const [resourceRevision, setResourceRevision] = useState(0)
- const [documentRefreshMs, setDocumentRefreshMs] = useState(15000)
- const [resourceRefreshMs, setResourceRefreshMs] = useState(30000)
- const [retryingIDs, setRetryingIDs] = useState([])
- const [requeueingIDs, setRequeueingIDs] = useState([])
- const [cancellingIDs, setCancellingIDs] = useState([])
- const [starting, setStarting] = useState(false)
- const [startMessage, setStartMessage] = useState('')
- const [batchMessage, setBatchMessage] = useState('')
- const [failures, setFailures] = useState([])
- const [attention, setAttention] = useState(null)
- const [removalItems, setRemovalItems] = useState([])
- const [removalBusyIDs, setRemovalBusyIDs] = useState([])
- const [removalMessage, setRemovalMessage] = useState('')
- const [loadingMore, setLoadingMore] = useState(false)
- const [metrics, setMetrics] = useState(null)
- const setContextTabs = useContext(WorkspaceContextTabsSetter)
-
+function TaskDetail({ item, onClose }: { item: MonitorTask; onClose: () => void }) {
+ const ref = useRef(null)
useEffect(() => {
- const controller = new AbortController()
- void fetchActiveUISettings(controller.signal).then((active) => {
- const delays = settingsRefreshDelays(active)
- if (!controller.signal.aborted) { setDocumentRefreshMs(delays.documentMs); setResourceRefreshMs(delays.resourceMs) }
- }).catch((cause) => { if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载刷新设置失败') })
- return () => controller.abort()
+ ref.current?.showModal()
}, [])
-
- useEffect(() => {
- const controller = new AbortController(); setLoading(true)
- void fetchMonitorOverview(controller.signal).then((result) => {
- if (controller.signal.aborted) return
- setOverview(result)
- setSelected((current) => current && result.knowledge_bases.find((kb) => kb.id === current.id) || result.knowledge_bases[0] || null)
- setError(null)
- }).catch((cause) => { if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载资源监控失败') }).finally(() => { if (!controller.signal.aborted) setLoading(false) })
- return () => controller.abort()
- }, [resourceRevision])
-
- useEffect(() => {
- const controller = new AbortController(); setLoading(true)
- void Promise.all([
- fetchTestPublications({ processing_state: queueStatus, target_scope: queueScope, limit: 50, offset: 0 }, controller.signal),
- fetchTestPublications({ processing_state: 'planned', limit: 100, offset: 0 }, controller.signal),
- fetchTestPublications({ processing_state: ['failed', 'stalled'], limit: 100, offset: 0 }, controller.signal),
- fetchRemovalRequiredPublications(controller.signal),
- ]).then(([queuePage, planPage, attentionPage, removalPage]) => {
- if (!controller.signal.aborted) { setQueue(queuePage); setPlans(planPage.items); setAttention(attentionPage.items); setRemovalItems(removalPage.items); setError(null) }
- }).catch((cause) => { if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载解析队列失败') }).finally(() => { if (!controller.signal.aborted) setLoading(false) })
- return () => controller.abort()
- }, [queueStatus, queueScope, documentRevision])
-
- useEffect(() => {
- if (!selected) return
- const controller = new AbortController()
- setLoading(true)
- void fetchMonitorFiles(selected.id, status, 20, 0, controller.signal).then((result) => {
- if (!controller.signal.aborted) setFiles(result)
- }).catch((cause) => {
- if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : '加载文档阶段失败')
- }).finally(() => {
- if (!controller.signal.aborted) setLoading(false)
- })
- return () => controller.abort()
- }, [selected, status, documentRevision])
-
- useEffect(() => {
- const timer = window.setTimeout(() => setDocumentRevision((value) => value + 1), documentRefreshMs)
- return () => window.clearTimeout(timer)
- }, [documentRevision, documentRefreshMs])
- useEffect(() => {
- const timer = window.setTimeout(() => setResourceRevision((value) => value + 1), resourceRefreshMs)
- return () => window.clearTimeout(timer)
- }, [resourceRevision, resourceRefreshMs])
- useEffect(() => {
- setContextTabs(selected ? [{ key: selected.id, label: selected.name, onActivate: () => setStatus('') }] : [])
- return () => setContextTabs([])
- }, [selected, setContextTabs])
- useEffect(() => {
- const controller = new AbortController()
- void fetchJobFailures(controller.signal).then((items) => { if (!controller.signal.aborted) setFailures(items) }).catch(() => undefined)
- return () => controller.abort()
- }, [documentRevision, resourceRevision])
- useEffect(() => {
- const controller = new AbortController()
- void fetchRunMetrics(controller.signal).then((result) => { if (!controller.signal.aborted) setMetrics(result) }).catch(() => undefined)
- return () => controller.abort()
- }, [documentRevision, resourceRevision])
-
- const requeueFailure = async (failureID: string) => {
- setRequeueingIDs((current) => [...current, failureID])
- setBatchMessage('')
- try {
- const result = await requeueJobFailure(failureID, '源头取件死信重新入队')
- setError(null)
- setBatchMessage(result.already_queued
- ? `该取件任务已在队列中(第 ${result.round} 轮)`
- : `已重新入队:${result.doc_name || result.version_id.slice(0, 12)}(第 ${result.round} 轮取件),取回后自动进入待入库`)
- setDocumentRevision((value) => value + 1)
- } catch (cause) {
- setError(cause instanceof Error ? cause.message : '重新取件失败')
- } finally {
- setRequeueingIDs((current) => current.filter((value) => value !== failureID))
- }
- }
-
- const retryOne = async (id: string) => {
- setRetryingIDs((current) => [...current, id])
- try {
- await retryPublication(id)
- setError(null)
- setDocumentRevision((value) => value + 1)
- } catch (cause) {
- setError(cause instanceof Error ? cause.message : '重试失败')
- } finally {
- setRetryingIDs((current) => current.filter((value) => value !== id))
- }
- }
-
- // 需下架 → 移除:把被禁止导入、但仍存活在测试库的知识交给既有的 test.remove 工作流。
- const startRemoval = async (id: string) => {
- if (removalBusyIDs.includes(id)) return
- setRemovalBusyIDs((current: string[]) => [...current, id])
- setRemovalMessage('')
- try {
- await startPublicationRemoval(id)
- setRemovalMessage('已受理下架:正在移除测试库中的知识条目,完成后本条会从「需下架」消失。')
- setDocumentRevision((value) => value + 1)
- } catch (cause) {
- setRemovalMessage(cause instanceof Error ? cause.message : '触发下架失败')
- } finally {
- setRemovalBusyIDs((current: string[]) => current.filter((value) => value !== id))
- }
- }
-
- const loadMoreQueue = async () => {
- if (loadingMore) return
- setLoadingMore(true)
- try {
- const next = await fetchTestPublications({ processing_state: queueStatus, target_scope: queueScope, limit: 50, offset: queue.items.length })
- setQueue((current) => {
- const seen = new Set(current.items.map((item) => item.id))
- return { ...next, offset: 0, items: [...current.items, ...next.items.filter((item) => !seen.has(item.id))] }
- })
- setError(null)
- } catch (cause) {
- setError(cause instanceof Error ? cause.message : '加载更多失败')
- } finally {
- setLoadingMore(false)
- }
- }
-
- const retryBatch = async (ids: string[]) => {
- setRetryingIDs(ids)
- setBatchMessage('')
- try {
- const result = await retryPublicationsBatch(ids, '解析队列批量重试')
- setError(null)
- const skipped = result.already_running + result.not_retryable
- setBatchMessage(`已重新入队 ${result.started} 项${skipped ? `,跳过 ${skipped} 项(处理中或不可重试)` : ''}`)
- setDocumentRevision((value) => value + 1)
- } catch (cause) {
- setError(cause instanceof Error ? cause.message : '批量重试失败')
- } finally {
- setRetryingIDs([])
- }
- }
- const cancelBatch = async (ids: string[]) => {
- setCancellingIDs(ids)
- setBatchMessage('')
- try {
- const result = await cancelPublicationsBatch(ids, '解析队列批量取消(不再重试)')
- setError(null)
- const skipped = result.already_running + result.not_retryable
- setBatchMessage(`已取消 ${result.started} 项,不再自动重试${skipped ? `,跳过 ${skipped} 项(处理中或不可取消)` : ''};原始记录未删除,重新标记入库可再次处理`)
- setDocumentRevision((value) => value + 1)
- } catch (cause) {
- setError(cause instanceof Error ? cause.message : '批量取消失败')
- } finally {
- setCancellingIDs([])
- }
- }
-
- const startPlans = async (ids: string[]) => {
- setStarting(true)
- setStartMessage('')
- try {
- const result = await startTestPublications(ids, '开始处理')
- setStartMessage(`已提交 ${result.selected} 项;新建解析任务 ${result.started} 项;已在队列 ${result.already_started} 项;不可处理 ${result.unavailable} 项(多为源头取件未完成,稍后自动重试)。`)
- setError(null)
- setDocumentRevision((value) => value + 1)
- } catch (cause) {
- setStartMessage(cause instanceof Error ? cause.message : '开始处理失败')
- } finally {
- setStarting(false)
- }
- }
- return { setSelected(kb); setStatus(''); setFiles(null) }} onStatus={(value) => { setStatus(value); setFiles(null) }} onQueueStatus={setQueueStatus} onQueueScope={setQueueScope} lane={lane} onLaneChange={(value) => setLane(value as typeof lane)} onRetry={(id) => void retryOne(id)} onBatchRetry={(ids) => void retryBatch(ids)} onBatchCancel={(ids) => void cancelBatch(ids)} onRequeueFailure={(id) => void requeueFailure(id)} onStart={(ids) => void startPlans(ids)} attention={attention ?? undefined} loadingMore={loadingMore} onLoadMore={() => void loadMoreQueue()} onRefresh={() => { setDocumentRevision((value) => value + 1); setResourceRevision((value) => value + 1) }} removalItems={removalItems} removalBusyIDs={removalBusyIDs} removalMessage={removalMessage} onStartRemoval={(id) => void startRemoval(id)} />
+ return (
+
+
+ {item.title}
+
+ {[
+ ['来源', item.source_name],
+ ['目录', item.path],
+ ['目标知识库', item.target_kb],
+ ['处理动作', actionLabels[item.action]],
+ ['当前阶段', stageLabels[item.stage]],
+ ['处理状态', taskStatusLabels[item.status]],
+ ['状态说明', item.detail],
+ ['更新时间', time(item.updated_at)],
+ ['累计重试', String(item.retry_count)],
+ ['版本标识', item.version_id],
+ ].map(([label, value]) => (
+
+
{label}
+ {value || '—'}
+
+ ))}
+
+ {item.history?.length ? (
+
+ 版本与重试记录
+ 最近 50 条已登记记录,不推断未记录的处理步骤。
+ {item.history.map((event, i) => (
+
+ {event.kind}
+ {time(event.at)}
+ {event.detail}
+
+ ))}
+
+ ) : null}
+ 新增、更新完成表示测试库处理完成;移除完成表示测试库内容移除,不代表生产发布。
+ {item.status === 'attention' ? (
+
+ 请先确认配置、权限或依赖已恢复。
+ {canRetryTask(item) ? '重试将沿用原处理动作。' : '当前记录不满足安全重试条件,需先修复前置条件。'}
+
+ ) : null}
+
+ )
+}
+export function WeKnoraMonitor() {
+ const auth = useOptionalAuth()
+ const canOperate = auth?.identity?.role === 'admin' || auth?.identity?.role === 'engineer'
+ const [overview, setOverview] = useState(null),
+ [metrics, setMetrics] = useState(null)
+ const [page, setPage] = useState(emptyPage),
+ [filters, setFilters] = useState(defaultMonitorFilters),
+ [offset, setOffset] = useState(0)
+ const [selected, setSelected] = useState([]),
+ [detail, setDetail] = useState(null)
+ const [loading, setLoading] = useState(true),
+ [busy, setBusy] = useState(false),
+ [message, setMessage] = useState('')
+ const [errors, setErrors] = useState({ files: '', overview: '', metrics: '', settings: '' })
+ const [docRev, setDocRev] = useState(0),
+ [resRev, setResRev] = useState(0),
+ [delays, setDelays] = useState({ documentMs: 15000, resourceMs: 30000 })
+ const setTabs = useContext(WorkspaceContextTabsSetter)
+ useEffect(() => {
+ setTabs([])
+ return () => setTabs([])
+ }, [setTabs])
+ useEffect(() => {
+ const c = new AbortController()
+ void fetchActiveUISettings(c.signal)
+ .then((s) => {
+ if (!c.signal.aborted) setDelays(settingsRefreshDelays(s))
+ })
+ .catch((e) => {
+ if (!c.signal.aborted) setErrors((x) => ({ ...x, settings: String(e) }))
+ })
+ return () => c.abort()
+ }, [])
+ useEffect(() => {
+ const t = window.setTimeout(() => setDocRev((x) => x + 1), delays.documentMs)
+ return () => clearTimeout(t)
+ }, [docRev, delays.documentMs])
+ useEffect(() => {
+ const t = window.setTimeout(() => setResRev((x) => x + 1), delays.resourceMs)
+ return () => clearTimeout(t)
+ }, [resRev, delays.resourceMs])
+ useEffect(() => {
+ const c = new AbortController()
+ setLoading(true)
+ const timer = window.setTimeout(() => {
+ void fetchMonitorTasks(filters, offset, 20, c.signal)
+ .then((p) => {
+ if (c.signal.aborted) return
+ setPage(p)
+ setErrors((e) => ({ ...e, files: '' }))
+ setSelected((old) =>
+ old.flatMap((r) => {
+ const fresh = p.items.find((i) => i.id === r.id)
+ return fresh ? (canRetryTask(fresh) ? [fresh] : []) : [r]
+ })
+ )
+ })
+ .catch((e) => {
+ if (!c.signal.aborted) {
+ setPage({ ...emptyPage, offset })
+ setErrors((x) => ({ ...x, files: e instanceof Error ? e.message : '读取文件状态失败' }))
+ }
+ })
+ .finally(() => {
+ if (!c.signal.aborted) setLoading(false)
+ })
+ }, 200)
+ return () => {
+ clearTimeout(timer)
+ c.abort()
+ }
+ }, [filters, offset, docRev])
+ useEffect(() => {
+ const c = new AbortController()
+ void fetchMonitorOverview(c.signal)
+ .then((r) => {
+ if (!c.signal.aborted) {
+ setOverview(r)
+ setErrors((e) => ({ ...e, overview: '' }))
+ }
+ })
+ .catch((e) => {
+ if (!c.signal.aborted) {
+ setOverview(null)
+ setErrors((x) => ({ ...x, overview: e instanceof Error ? e.message : '读取服务状态失败' }))
+ }
+ })
+ void fetchRunMetrics(c.signal)
+ .then((r) => {
+ if (!c.signal.aborted) {
+ setMetrics(r)
+ setErrors((e) => ({ ...e, metrics: '' }))
+ }
+ })
+ .catch((e) => {
+ if (!c.signal.aborted) {
+ setMetrics(null)
+ setErrors((x) => ({ ...x, metrics: e instanceof Error ? e.message : '读取运行指标失败' }))
+ }
+ })
+ return () => c.abort()
+ }, [resRev])
+ const refresh = () => {
+ setDocRev((x) => x + 1)
+ setResRev((x) => x + 1)
+ }
+ const selectAll = async () => {
+ if (busy) return
+ setBusy(true)
+ setMessage('')
+ try {
+ const found = new Map()
+ for (let at = 0; ; at += 100) {
+ const p = await fetchMonitorTasks(filters, at, 100)
+ if (p.total > 10000) throw new Error('筛选结果超过 10000 项,请缩小范围后再选择')
+ p.items.filter(canRetryTask).forEach((r) => found.set(r.id, r))
+ if (at + p.items.length >= p.total || !p.items.length) break
+ }
+ setSelected([...found.values()])
+ setMessage(`已选择 ${found.size} 项;提交前会重新核验状态。`)
+ } catch (e) {
+ setMessage(e instanceof Error ? e.message : '跨页选择失败')
+ } finally {
+ setBusy(false)
+ }
+ }
+ const retry = async (rows: MonitorTask[]) => {
+ if (busy || !rows.length) return
+ const removals = rows.filter((r) => r.action === 'remove').length
+ if (
+ !window.confirm(
+ `重试 ${rows.length} 项:新增/更新 ${rows.length - removals} 项,测试库移除 ${removals} 项。\n不会删除来源文件或操作生产知识库。是否继续?`
+ )
+ )
+ return
+ setBusy(true)
+ setMessage('')
+ let accepted = 0,
+ skipped = 0,
+ failed = 0
+ const notes: string[] = []
+ for (const row of rows) {
+ try {
+ const fresh = (await fetchMonitorTasks({ id: row.id }, 0, 1)).items[0]
+ if (!fresh || !canRetryTask(fresh) || fresh.action !== row.action) {
+ skipped++
+ notes.push(`${row.title}:状态已变化,跳过`)
+ continue
+ }
+ if (fresh.retry_kind === 'fetch') {
+ const r = await requeueJobFailure(fresh.failure_id, '解析监控重试')
+ if (r.already_queued) {
+ skipped++
+ continue
+ }
+ } else if (fresh.retry_kind === 'upload') {
+ const r = await retryPublicationsBatch([fresh.id], '解析监控重试')
+ if (!r.started) {
+ skipped++
+ notes.push(`${row.title}:${r.outcomes[0]?.detail || '处理中或不可重试'}`)
+ continue
+ }
+ } else {
+ const r = await fetch(`/api/v1/test-publications/${encodeURIComponent(fresh.id)}/removal-retry`, {
+ method: 'POST',
+ headers: { Accept: 'application/json', 'X-Governance-Action': 'monitor-removal-retry' },
+ })
+ if (!r.ok) throw new Error(`测试库移除重试失败(HTTP ${r.status})`)
+ }
+ accepted++
+ try {
+ await fetchMonitorTasks({ id: fresh.id }, 0, 1)
+ } catch {
+ notes.push(`${row.title}:已受理,状态读回失败,请刷新确认`)
+ }
+ } catch (e) {
+ failed++
+ notes.push(`${row.title}:${e instanceof Error ? e.message : '请求失败'}`)
+ }
+ }
+ setMessage(
+ `已受理 ${accepted} 项,已跳过 ${skipped} 项,失败 ${failed} 项。受理不代表处理完成。${notes.length ? ' ' + notes.join(';') : ''}`
+ )
+ setSelected([])
+ setBusy(false)
+ refresh()
+ }
+ return (
+ <>
+ {
+ setFilters(v)
+ setOffset(0)
+ setSelected([])
+ setMessage('')
+ setLoading(true)
+ }}
+ onPage={setOffset}
+ onSelection={setSelected}
+ onSelectAll={() => void selectAll()}
+ onRetry={(r) => void retry(r)}
+ onDetail={(row) => {
+ void fetchMonitorTasks({ id: row.id }, 0, 1)
+ .then((p) => {
+ if (p.items[0]) setDetail(p.items[0])
+ else setMessage('该条记录已被新版本替换,请刷新列表')
+ })
+ .catch((e) => setMessage(e instanceof Error ? e.message : '读取详情失败'))
+ }}
+ onRefresh={refresh}
+ />
+ {detail ? setDetail(null)} /> : null}
+ >
+ )
}
diff --git a/web/src/monitorTasks.test.ts b/web/src/monitorTasks.test.ts
new file mode 100644
index 0000000..599dfad
--- /dev/null
+++ b/web/src/monitorTasks.test.ts
@@ -0,0 +1,46 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { fetchMonitorTasks } from './monitorTasks'
+import { healthLabel } from './WeKnoraMonitor'
+import type { ServiceHealth } from './monitor'
+afterEach(() => vi.unstubAllGlobals())
+describe('monitor table contract', () => {
+ it('rejects malformed facets instead of rendering a broken selector', async () => {
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn().mockResolvedValue({
+ ok: true,
+ json: async () => ({
+ data: {
+ items: [],
+ total: 0,
+ limit: 20,
+ offset: 0,
+ retryable: 0,
+ sources: [{ id: 7, name: 'bad' }],
+ targets: [],
+ },
+ }),
+ })
+ )
+ await expect(fetchMonitorTasks({ status: 'active' })).rejects.toThrow('筛选项')
+ })
+ it('passes the filter and page to the server without client-side totals', async () => {
+ const data = { items: [], total: 145, limit: 20, offset: 40, retryable: 12, sources: [], targets: [] }
+ const request = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data }) })
+ vi.stubGlobal('fetch', request)
+ const result = await fetchMonitorTasks({ status: 'attention', action: 'remove', q: '手册' }, 40)
+ expect(result.total).toBe(145)
+ expect(result.retryable).toBe(12)
+ const url = new URL(request.mock.calls[0][0], 'http://localhost')
+ expect(url.searchParams.get('offset')).toBe('40')
+ expect(url.searchParams.get('action')).toBe('remove')
+ expect(url.searchParams.get('q')).toBe('手册')
+ })
+ it('distinguishes unknown, unconfigured, blocked and inapplicable services', () => {
+ const s = { applicable: true, configured: true, readiness: 'unknown' } as ServiceHealth
+ expect(healthLabel(s)).toBe('未确认')
+ expect(healthLabel({ ...s, configured: false })).toBe('未配置')
+ expect(healthLabel({ ...s, applicable: false })).toBe('不适用')
+ expect(healthLabel({ ...s, readiness: 'blocked' })).toBe('阻断')
+ })
+})
diff --git a/web/src/monitorTasks.ts b/web/src/monitorTasks.ts
new file mode 100644
index 0000000..3fe2641
--- /dev/null
+++ b/web/src/monitorTasks.ts
@@ -0,0 +1,129 @@
+export interface MonitorTask {
+ history?: { at: string; kind: string; detail: string }[]
+ id: string
+ title: string
+ path: string
+ source_id: string
+ source_name: string
+ target_scope: string
+ target_kb: string
+ action: string
+ stage: string
+ status: string
+ detail: string
+ updated_at: string
+ retry_kind: string
+ failure_id: string
+ retry_count: number
+ version_id: string
+}
+export interface MonitorTaskPage {
+ items: MonitorTask[]
+ total: number
+ limit: number
+ offset: number
+ sources: { id: string; name: string }[]
+ targets: { id: string; name: string }[]
+ retryable: number
+}
+export interface MonitorFilters {
+ q: string
+ source: string
+ scope: string
+ target: string
+ action: string
+ status: string
+ stage: string
+ since: string
+ until: string
+}
+export const defaultMonitorFilters: MonitorFilters = {
+ q: '',
+ source: '',
+ scope: '',
+ target: '',
+ action: '',
+ status: 'active',
+ stage: '',
+ since: '',
+ until: '',
+}
+export const actionLabels: Record = { add: '新增', update: '更新', remove: '移除' }
+export const stageLabels: Record = {
+ waiting: '等待调度',
+ fetch: '取件',
+ upload: '上传',
+ parsing: '解析',
+ indexing: '索引增强',
+ removal: '测试库移除',
+ finished: '结束',
+}
+export const taskStatusLabels: Record = {
+ waiting: '等待中',
+ running: '执行中',
+ retrying: '重试中',
+ attention: '需人工处理',
+ completed: '已完成',
+ cancelled: '已取消',
+}
+export const canRetryTask = (item: MonitorTask) =>
+ item.status === 'attention' && ['upload', 'fetch', 'remove'].includes(item.retry_kind)
+const record = (v: unknown): v is Record => typeof v === 'object' && v !== null
+export async function fetchMonitorTasks(
+ filters: Partial & { id?: string },
+ offset = 0,
+ limit = 20,
+ signal?: AbortSignal
+): Promise {
+ const query = new URLSearchParams({ limit: String(limit), offset: String(offset) })
+ Object.entries(filters).forEach(([k, v]) => {
+ if (v) query.set(k, v)
+ })
+ const response = await fetch(`/api/v1/monitor/tasks?${query}`, { headers: { Accept: 'application/json' }, signal })
+ if (!response.ok) throw new Error(`加载文件处理状态失败(HTTP ${response.status})`)
+ const value: unknown = await response.json()
+ if (!record(value) || !record(value.data)) throw new Error('文件处理响应无效')
+ const d = value.data
+ const count = (v: unknown) => Number.isInteger(v) && Number(v) >= 0
+ if (
+ !Array.isArray(d.items) ||
+ !count(d.total) ||
+ !count(d.limit) ||
+ !count(d.offset) ||
+ !count(d.retryable) ||
+ !Array.isArray(d.sources) ||
+ !Array.isArray(d.targets)
+ )
+ throw new Error('文件处理分页响应无效')
+ for (const item of d.items) {
+ if (
+ !record(item) ||
+ ![
+ 'id',
+ 'title',
+ 'path',
+ 'source_id',
+ 'source_name',
+ 'target_scope',
+ 'target_kb',
+ 'action',
+ 'stage',
+ 'status',
+ 'detail',
+ 'updated_at',
+ 'retry_kind',
+ 'failure_id',
+ 'version_id',
+ ].every((k) => typeof item[k] === 'string') ||
+ !count(item.retry_count) ||
+ !Object.hasOwn(actionLabels, String(item.action)) ||
+ !Object.hasOwn(stageLabels, String(item.stage)) ||
+ !Object.hasOwn(taskStatusLabels, String(item.status))
+ )
+ throw new Error('文件处理条目无效')
+ }
+ for (const option of [...d.sources, ...d.targets])
+ if (!record(option) || typeof option.id !== 'string' || typeof option.name !== 'string')
+ throw new Error('文件处理筛选项无效')
+ return d as unknown as MonitorTaskPage
+}
diff --git a/web/src/monitorWorkbench.css b/web/src/monitorWorkbench.css
new file mode 100644
index 0000000..43f8d4b
--- /dev/null
+++ b/web/src/monitorWorkbench.css
@@ -0,0 +1,465 @@
+.monitor-workbench {
+ max-width: 1800px;
+ color: var(--ink-base);
+}
+.monitor-workbench button {
+ box-shadow: none;
+}
+.monitor-heading {
+ display: flex;
+ align-items: center;
+ gap: 14px;
+ margin-bottom: 20px;
+}
+.monitor-heading h1 {
+ font-size: 24px;
+ margin: 0 0 5px;
+}
+.monitor-heading p {
+ margin: 0;
+ color: var(--ink-mute);
+ font-size: 12px;
+}
+.monitor-heading > span {
+ margin-left: auto;
+ color: var(--ink-mute);
+ font-size: 11px;
+}
+.monitor-metric-grid {
+ display: grid;
+ grid-template-columns: 1fr 1fr 1.3fr 1.1fr;
+ gap: 14px;
+ margin-bottom: 15px;
+}
+.monitor-metric-grid article {
+ min-width: 0;
+ padding: 18px;
+ border: 1px solid var(--border-base);
+ border-radius: 9px;
+ background: var(--bg-panel);
+}
+.monitor-metric-grid h2 {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ gap: 8px;
+ font-size: 12px;
+ font-weight: 500;
+ color: var(--ink-mute);
+ margin: 0;
+}
+.monitor-metric-grid h2 small {
+ font-size: 10px;
+ color: var(--ink-faint);
+}
+.monitor-numbers {
+ display: flex;
+ justify-content: space-between;
+ gap: 12px;
+ margin-top: 15px;
+}
+.monitor-numbers > div {
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+ min-width: 0;
+}
+.monitor-numbers strong {
+ font-size: 27px;
+ line-height: 1.3;
+ font-weight: 700;
+ letter-spacing: -0.6px;
+ white-space: nowrap;
+ font-variant-numeric: tabular-nums;
+}
+.monitor-numbers span {
+ font-size: 11px;
+ color: var(--ink-mute);
+}
+.monitor-numbers .monitor-memory {
+ font-size: 23px;
+}
+.monitor-good {
+ color: var(--c-done) !important;
+}
+.monitor-bad {
+ color: var(--c-fail) !important;
+}
+.monitor-service-panel,
+.monitor-files {
+ border: 1px solid var(--border-base);
+ border-radius: 9px;
+ background: var(--bg-panel);
+ overflow: hidden;
+}
+.monitor-service-panel {
+ padding: 15px 18px;
+ margin-bottom: 22px;
+}
+.monitor-section-heading {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+}
+.monitor-section-heading h2 {
+ font-size: 16px;
+ margin: 0;
+ font-weight: 650;
+}
+.monitor-section-heading > span {
+ font-size: 11px;
+ color: var(--ink-mute);
+}
+.monitor-service-grid {
+ display: grid;
+ grid-template-columns: repeat(6, minmax(0, 1fr));
+ gap: 9px;
+ margin-top: 13px;
+ align-items: start;
+}
+.monitor-service {
+ border: 1px solid var(--border-base);
+ border-radius: 6px;
+ background: var(--bg-soft);
+ padding: 10px 11px;
+ font-size: 12px;
+ min-width: 0;
+}
+.monitor-service summary {
+ list-style: none;
+ cursor: pointer;
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+}
+.monitor-service summary::-webkit-details-marker {
+ display: none;
+}
+.monitor-service summary > span {
+ overflow-wrap: anywhere;
+}
+.monitor-service b {
+ font-size: 11px;
+ font-weight: 500;
+ color: var(--ink-mute);
+}
+.monitor-service p,
+.monitor-service small {
+ font-size: 11px;
+ color: var(--ink-mute);
+ overflow-wrap: anywhere;
+ line-height: 1.6;
+}
+.monitor-files > .monitor-section-heading {
+ padding: 18px 19px 10px;
+}
+.monitor-filters {
+ display: flex;
+ gap: 8px;
+ align-items: center;
+ flex-wrap: wrap;
+ padding: 10px 19px 16px;
+}
+.monitor-filters input,
+.monitor-filters select {
+ border: 1px solid var(--border-strong);
+ border-radius: 6px;
+ padding: 8px 10px;
+ background: var(--bg-panel);
+ color: var(--ink-base);
+ font: inherit;
+ font-size: 12px;
+ max-width: 230px;
+ min-height: 35px;
+}
+.monitor-filters > input {
+ width: 210px;
+}
+.monitor-time-filter {
+ font-size: 12px;
+ color: var(--ink-mute);
+}
+.monitor-time-filter summary {
+ cursor: pointer;
+}
+.monitor-time-filter label {
+ display: flex;
+ gap: 8px;
+ align-items: center;
+ margin-top: 8px;
+}
+.monitor-batch {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ flex-wrap: wrap;
+ padding: 11px 19px;
+ background: var(--c-run-bg);
+ border-top: 1px solid var(--c-run-border);
+ border-bottom: 1px solid var(--c-run-border);
+ font-size: 12px;
+}
+.monitor-batch b {
+ color: var(--c-run);
+}
+.monitor-batch > span {
+ color: var(--ink-mute);
+}
+.monitor-batch > .primary {
+ margin-left: auto;
+}
+.monitor-workbench .primary {
+ background: var(--c-run);
+ border-color: var(--c-run);
+ color: #fff;
+}
+.monitor-workbench button:disabled {
+ cursor: not-allowed;
+ opacity: 0.5;
+}
+.monitor-workbench .monitor-link {
+ padding: 4px 6px;
+ background: transparent;
+ color: var(--c-run);
+ border: 0;
+ border-radius: 4px;
+ font-size: 12px;
+ white-space: nowrap;
+}
+.monitor-table-scroll {
+ overflow: auto;
+}
+.monitor-unified-table {
+ width: 100%;
+ min-width: 1180px;
+ border-collapse: collapse;
+ table-layout: fixed;
+ text-align: left;
+}
+.monitor-unified-table th {
+ padding: 12px 9px;
+ background: var(--bg-soft);
+ font-size: 11px;
+ font-weight: 500;
+ color: var(--ink-mute);
+ border-bottom: 1px solid var(--border-base);
+ white-space: normal;
+}
+.monitor-unified-table td {
+ padding: 14px 9px;
+ font-size: 12px;
+ line-height: 1.6;
+ border-bottom: 1px solid var(--border-base);
+ vertical-align: middle;
+ white-space: normal;
+ overflow-wrap: anywhere;
+}
+.monitor-unified-table tr:last-child td {
+ border-bottom: 0;
+}
+.monitor-unified-table td small {
+ display: block;
+ font-size: 10px;
+ color: var(--ink-mute);
+ white-space: nowrap;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ margin-top: 3px;
+}
+.monitor-file-name {
+ display: block;
+ font-size: 13px;
+ line-height: 1.6;
+ font-weight: 550;
+}
+.monitor-unified-table input {
+ accent-color: var(--c-run);
+ width: 14px;
+ height: 14px;
+}
+.monitor-unified-table tr:hover,
+.monitor-selected {
+ background: var(--c-run-bg);
+}
+.monitor-state,
+.monitor-scope {
+ display: inline-block;
+ font-size: 11px;
+ padding: 2px 7px;
+ border-radius: 4px;
+ white-space: nowrap;
+ background: var(--c-run-bg);
+ color: var(--c-run);
+}
+.monitor-state.attention {
+ background: var(--c-fail-bg);
+ color: var(--c-fail);
+}
+.monitor-state.completed {
+ background: var(--c-done-bg);
+ color: var(--c-done);
+}
+.monitor-state.retrying {
+ background: var(--c-warn-bg);
+ color: var(--c-warn);
+}
+.monitor-state.waiting,
+.monitor-state.cancelled {
+ background: var(--c-dead-bg);
+ color: var(--c-dead);
+}
+.monitor-pagination {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+ padding: 13px 19px;
+ border-top: 1px solid var(--border-base);
+ font-size: 11px;
+ color: var(--ink-mute);
+}
+.monitor-pagination > div {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+}
+.monitor-pagination button {
+ font-size: 11px;
+ padding: 6px 10px;
+}
+.monitor-unified-table .monitor-empty {
+ text-align: center;
+ padding: 45px 20px;
+ color: var(--ink-mute);
+}
+.monitor-alert {
+ padding: 11px 15px;
+ border: 1px solid var(--c-warn-border);
+ background: var(--c-warn-bg);
+ color: var(--c-warn);
+ border-radius: 6px;
+ font-size: 12px;
+ margin-bottom: 14px;
+ overflow-wrap: anywhere;
+}
+.monitor-result {
+ padding: 12px 19px;
+ font-size: 12px;
+ line-height: 1.8;
+ border-bottom: 1px solid var(--border-base);
+ overflow-wrap: anywhere;
+}
+.monitor-muted {
+ font-size: 12px;
+ color: var(--ink-mute);
+ line-height: 1.8;
+}
+.monitor-drawer {
+ position: fixed;
+ inset: 0 0 0 auto;
+ width: min(480px, 100vw);
+ height: 100dvh;
+ max-height: 100dvh;
+ max-width: 100vw;
+ margin: 0;
+ border: 0;
+ border-left: 1px solid var(--border-base);
+ padding: 25px;
+ background: var(--bg-panel);
+ color: var(--ink-base);
+ overflow: auto;
+ box-shadow: -10px 0 35px #10204020;
+}
+.monitor-drawer::backdrop {
+ background: #15253f35;
+}
+.monitor-drawer header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 10px;
+}
+.monitor-drawer h2 {
+ font-size: 19px;
+}
+.monitor-drawer h3 {
+ font-size: 17px;
+ overflow-wrap: anywhere;
+}
+.monitor-drawer dl > div {
+ display: grid;
+ grid-template-columns: 90px 1fr;
+ gap: 12px;
+ padding: 12px 0;
+ border-bottom: 1px solid var(--border-base);
+ font-size: 12px;
+ line-height: 1.7;
+}
+.monitor-drawer dt {
+ color: var(--ink-mute);
+}
+.monitor-drawer dd {
+ margin: 0;
+ overflow-wrap: anywhere;
+}
+.monitor-workbench :focus-visible,
+.monitor-drawer :focus-visible {
+ outline: 2px solid var(--c-run);
+ outline-offset: 3px;
+}
+@media (max-width: 1200px) {
+ .monitor-metric-grid {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ }
+ .monitor-service-grid {
+ grid-template-columns: repeat(4, minmax(0, 1fr));
+ }
+}
+@media (max-width: 760px) {
+ .monitor-heading {
+ flex-wrap: wrap;
+ }
+ .monitor-heading > span {
+ margin-left: 0;
+ }
+ .monitor-service-grid {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ }
+ .monitor-metric-grid {
+ gap: 8px;
+ }
+ .monitor-metric-grid article {
+ padding: 12px;
+ }
+ .monitor-metric-grid h2 {
+ flex-wrap: wrap;
+ }
+ .monitor-numbers {
+ gap: 6px;
+ }
+ .monitor-numbers strong,
+ .monitor-numbers .monitor-memory {
+ font-size: 20px;
+ }
+ .monitor-batch > span {
+ display: none;
+ }
+ .monitor-section-heading {
+ align-items: flex-start;
+ flex-direction: column;
+ }
+ .monitor-pagination {
+ padding: 12px;
+ }
+ .monitor-filters {
+ padding: 12px;
+ }
+ .monitor-filters > input {
+ width: 100%;
+ max-width: none;
+ }
+ .monitor-drawer {
+ padding: 18px;
+ }
+}