fix(monitor): keep failing publications visible outside the queue page

- The attention lane was derived from page 1 of the queue (limit 50, newest
  by updated_at), so failures ranked past 50 silently disappeared from
  "needs attention" as the completed backlog grew.
- Fetch attention items with a dedicated request: the API now accepts a
  comma-separated processing_state list (failed,stalled) and the view prefers
  that result set over the paged queue.
- Batch retry/cancel targets are computed from the same failure set instead of
  the visible page, so actions reach every failing item.
- 全部任务 shows 已显示 N / 共 M plus 加载更多 so rows past 50 are reachable.
- Fix pre-existing ui_settings integration test: strict JSON decoding rejects
  the legacy updated_by field (actor comes from the session).
- Tests: view regression (attention item outside the page, load-more
  affordance) + handler multi-state filter + isolated-DB set filter.
This commit is contained in:
姚章浩
2026-09-16 08:53:18 +08:00
parent 553eff5f7f
commit 1d6ba6dc2e
10 changed files with 149 additions and 21 deletions
+14 -2
View File
@@ -1725,8 +1725,8 @@ func getReleaseBatch(w http.ResponseWriter, r *http.Request, store Store) {
func listTestPublications(w http.ResponseWriter, r *http.Request, store Store) {
filter := governance.TestPublicationFilter{ProcessingState: r.URL.Query().Get("processing_state"), TargetScope: r.URL.Query().Get("target_scope"), Limit: 50}
if filter.ProcessingState != "" && !validProcessingState(filter.ProcessingState) {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "processing_state must be planned, pending, processing, finalizing, completed, failed, or stalled"})
if filter.ProcessingState != "" && !validProcessingStateFilter(filter.ProcessingState) {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "processing_state must be a comma-separated list of planned, pending, processing, finalizing, completed, failed, stalled, or cancelled"})
return
}
if filter.TargetScope != "" && filter.TargetScope != "internal" && filter.TargetScope != "external" {
@@ -2032,6 +2032,18 @@ func validProcessingState(state string) bool {
}
}
// validProcessingStateFilter accepts a single state or a comma-separated list so
// callers can ask for several lanes at once (e.g. the attention lane needs
// failed+stalled in one query instead of paging the whole queue).
func validProcessingStateFilter(filter string) bool {
for _, state := range strings.Split(filter, ",") {
if !validProcessingState(state) {
return false
}
}
return true
}
func newID() (string, error) {
var raw [16]byte
if _, err := rand.Read(raw[:]); err != nil {
+27
View File
@@ -7,6 +7,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
"slices"
"strings"
"testing"
@@ -1335,6 +1336,32 @@ func TestHandlerListsTestPublicationsSafeProjection(t *testing.T) {
}
}
func TestHandlerAcceptsMultiStateAttentionFilter(t *testing.T) {
store := &fakeJobStore{}
handler, err := NewHandler(Config{Store: store})
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/test-publications?processing_state=failed,stalled&limit=100", nil))
if response.Code != http.StatusOK {
t.Fatalf("status = %d; body=%s", response.Code, response.Body.String())
}
if store.publicationFilter.ProcessingState != "failed,stalled" {
t.Fatalf("filter = %#v", store.publicationFilter)
}
// One bad entry anywhere in the list is still rejected.
for _, value := range []string{"failed,bogus", "failed,", ",stalled"} {
response = httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/test-publications?processing_state="+url.QueryEscape(value), nil))
if response.Code != http.StatusBadRequest {
t.Fatalf("processing_state=%q status = %d; want 400", value, response.Code)
}
}
}
func TestHandlerConfirmsChangeSetItems(t *testing.T) {
now := time.Date(2026, 9, 12, 5, 0, 0, 0, time.UTC)
store := &fakeJobStore{confirmChangeSetResult: governance.ConfirmCatalogChangeSetResult{ChangeSetID: "cs-1", ConfirmedItems: 2, UploadsCreated: 2}}
@@ -37,7 +37,7 @@ func TestUISettingsWriteReadBackIntegration(t *testing.T) {
}
create := httptest.NewRecorder()
createRequest := httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400,"updated_by":"engineer-e2e"}`))
createRequest := httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400}`))
createRequest.Header.Set("X-Governance-Action", "ui-settings")
createRequest.Header.Set("Idempotency-Key", "op-e2e-uis-create")
handler.ServeHTTP(create, createRequest)
@@ -133,3 +133,52 @@ VALUES('job-cancel-dlq','test.upload','tp-cancel-1','remote_parse_failed','remot
t.Fatal("cancelled publication lost its version link")
}
}
// The attention lane lists every failing publication in one request, so the
// processing_state filter has to behave as a set — a single literal would make
// failures silently vanish again as soon as the queue grows past one page.
func TestListPublicationsAcceptsStateSet(t *testing.T) {
databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if databaseURL == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err := ApplyMigrations(ctx, pool); err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, `
INSERT INTO source_connections(id,source_type,name,enabled) VALUES('conn-set','dingtalk','集合来源',true);
INSERT INTO content_objects(id,sha256,size_bytes,local_spool_ref,content_state) VALUES('co-set',repeat('d',64),4891,'verified/set.docx','verified');
INSERT INTO source_items(id,source_connection_id,stable_external_id,canonical_uri_hash,title,item_type,revision,visibility,visibility_origin)
VALUES('item-set','conn-set','ext-set',repeat('e',64),'FAQ.adoc','document','r1','internal','manual');
INSERT INTO source_item_versions(id,source_item_id,revision,content_object_id,content_state) VALUES('siv-set','item-set','r1','co-set','verified');
INSERT INTO test_publications(id,source_item_version_id,test_kb_id,ingest_state,processing_state,retrieval_state,target_scope,last_error,updated_at) VALUES
('tp-set-failed','siv-set','kb-set-1','uploaded','failed','unpublished','internal','remote_parse_failed',NOW()),
('tp-set-stalled','siv-set','kb-set-2','uploaded','stalled','unpublished','internal','stalled',NOW()),
('tp-set-done','siv-set','kb-set-3','uploaded','completed','enabled','internal','',NOW());`); err != nil {
t.Fatal(err)
}
store := NewStore(pool)
for _, testCase := range []struct {
filter string
want int
}{
{"failed,stalled", 2},
{"failed", 1},
{"cancelled", 0},
{"", 3},
} {
page, err := store.ListTestPublications(ctx, governance.TestPublicationFilter{ProcessingState: testCase.filter, Limit: 50})
if err != nil {
t.Fatal(err)
}
if page.Total != testCase.want || len(page.Items) != testCase.want {
t.Fatalf("filter %q = %d items / total %d, want %d", testCase.filter, len(page.Items), page.Total, testCase.want)
}
}
}
@@ -116,7 +116,7 @@ ORDER BY p.id
const countTestPublicationsSQL = `
SELECT COUNT(*)
FROM test_publications
WHERE ($1 = '' OR processing_state = $1) AND ($2 = '' OR target_scope = $2)
WHERE ($1 = '' OR processing_state = ANY(string_to_array($1, ','))) AND ($2 = '' OR target_scope = $2)
`
const listTestPublicationsSQL = `
@@ -143,7 +143,7 @@ FROM test_publications p
JOIN source_item_versions siv ON siv.id = p.source_item_version_id
JOIN source_items si ON si.id = siv.source_item_id
LEFT JOIN content_objects co ON co.id = siv.content_object_id
WHERE ($1 = '' OR p.processing_state = $1) AND ($2 = '' OR p.target_scope = $2) AND p.superseded_at IS NULL
WHERE ($1 = '' OR p.processing_state = ANY(string_to_array($1, ','))) AND ($2 = '' OR p.target_scope = $2) AND p.superseded_at IS NULL
ORDER BY p.updated_at DESC, p.id
LIMIT $3 OFFSET $4
`