feat(findings): show consolidation across report task and asset views

This commit is contained in:
ji ruo committed 2026-10-09 12:41:46 +08:00
1 parent d68594e81a
commit d36812b7e1
10 files changed
+627 -462

No files matched your search

+27 -1
View File
@@ -62,9 +62,30 @@ func (s *Server) listFindingCases(w http.ResponseWriter, r *http.Request) {
caseHTTPError(w, err)
return
}
caseIDs := []int64{}
for _, row := range rows {
if row.Case != nil {
caseIDs = append(caseIDs, row.Case.ID)
}
}
contexts, err := s.m.pg.FindingCaseListContexts(caseIDs)
if err != nil {
caseHTTPError(w, err)
return
}
assetIDs := []int64{}
for _, c := range contexts {
assetIDs = append(assetIDs, c.AssetIDs...)
}
assets := s.resolveAssetIDs(assetIDs)
items := []map[string]any{}
for _, row := range rows {
v := map[string]any{"case": row.Case, "matched_ids": row.MatchedIDs}
if row.Case != nil {
c := contexts[row.Case.ID]
v["task_name"], v["task_description"], v["last_found_at"], v["asset_count"] = c.TaskName, c.TaskDescription, c.LastFoundAt, c.AssetCount
v["assets"] = append([]FindingAssetDTO{}, findingFromDB(&db.DBFinding{AssetIDs: c.AssetIDs}, assets).Assets...)
}
if row.FindingID > 0 {
f, err := s.m.pg.FindingCaseSummary(row.FindingID)
if err != nil {
@@ -84,7 +105,12 @@ func (s *Server) listFindingCases(w http.ResponseWriter, r *http.Request) {
caseHTTPError(w, err)
return
}
writeJSON(w, 200, map[string]any{"items": items, "total": total, "page": page, "page_size": size, "stats": stats})
matchingReports, err := s.m.pg.MatchingFindingReportCount(findingFilterFromQuery(q))
if err != nil {
caseHTTPError(w, err)
return
}
writeJSON(w, 200, map[string]any{"items": items, "total": total, "matching_reports": matchingReports, "page": page, "page_size": size, "stats": stats})
}
func (s *Server) findingCaseAccess(w http.ResponseWriter, r *http.Request, mutation bool) (*db.FindingCase, bool) {
id := int64(atoiDefault(r.PathValue("id"), 0))
+31
View File
@@ -321,3 +321,34 @@ func TestFindingCaseGroupedTrafficExportAndInheritedAccess(t *testing.T) {
t.Fatal(w.Code, w.Body)
}
}
func TestFindingCaseListIncludesContextAndFilteredCounts(t *testing.T) {
s, task, ids := testCaseServer(t)
cid, err := s.m.pg.MergeFindingCase(t.Context(), task, ids[:3], "same defect", "verified root cause", "human")
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", fmt.Sprintf("/api/finding-cases?task_id=%d&severity=medium", task), nil)
w := httptest.NewRecorder()
s.listFindingCases(w, r)
if w.Code != 200 {
t.Fatalf("status %d %s", w.Code, w.Body.String())
}
var page struct {
Total int `json:"total"`
MatchingReports int `json:"matching_reports"`
Items []struct {
Case *db.FindingCase `json:"case"`
TaskDescription string `json:"task_description"`
Assets []FindingAssetDTO `json:"assets"`
AssetCount int `json:"asset_count"`
LastFoundAt string `json:"last_found_at"`
} `json:"items"`
}
if err := json.Unmarshal(w.Body.Bytes(), &page); err != nil {
t.Fatal(err)
}
if page.Total != 1 || page.MatchingReports != 1 || len(page.Items) != 1 || page.Items[0].Case.ID != cid || page.Items[0].Case.Count != 3 || page.Items[0].Case.Report != "" || page.Items[0].TaskDescription == "" || page.Items[0].LastFoundAt == "" || page.Items[0].Assets == nil {
t.Fatalf("list payload %s", w.Body.String())
}
}