feat(product): remove CategoryTreeDiagnostics and related logging for empty category tree responses

This commit is contained in:
robinj0726 2026-08-31 18:02:57 +08:00
parent 71ca5b809f
commit f48a3bce2e
2 changed files with 0 additions and 98 deletions

View File

@ -235,22 +235,6 @@ type CategoryRecord struct {
Version int32
}
// CategoryTreeDiagnostics summarizes category rows when the storefront tree query is empty.
type CategoryTreeDiagnostics struct {
TotalRows int64
MatchingRows int64
ExpectedCategoryRows int64
ExpectedVisibleRows int64
ExpectedHiddenRows int64
LegacyCategoryRows int64
LegacyVisibleRows int64
LegacyHiddenRows int64
GroupRows int64
GroupVisibleRows int64
GroupHiddenRows int64
OtherNodeTypeRows int64
}
// CategoryFieldUpdate selects mutable fields of one category node.
type CategoryFieldUpdate struct {
ID string
@ -1954,43 +1938,6 @@ ORDER BY level ASC, sort_num ASC, id ASC
return categories, nil
}
// CategoryTreeDiagnostics loads aggregate counts used to explain an empty category tree response.
func (r *ProductRepository) CategoryTreeDiagnostics(ctx context.Context, onlyVisible bool) (*CategoryTreeDiagnostics, error) {
var stats CategoryTreeDiagnostics
if err := r.db.QueryRowContext(ctx, `
SELECT
COUNT(*) AS total_rows,
COUNT(*) FILTER (WHERE node_type = 1 AND ($1 = false OR status = 1)) AS matching_rows,
COUNT(*) FILTER (WHERE node_type = 1) AS expected_category_rows,
COUNT(*) FILTER (WHERE node_type = 1 AND status = 1) AS expected_visible_rows,
COUNT(*) FILTER (WHERE node_type = 1 AND status = 0) AS expected_hidden_rows,
COUNT(*) FILTER (WHERE node_type = 0) AS legacy_category_rows,
COUNT(*) FILTER (WHERE node_type = 0 AND status = 1) AS legacy_visible_rows,
COUNT(*) FILTER (WHERE node_type = 0 AND status = 0) AS legacy_hidden_rows,
COUNT(*) FILTER (WHERE node_type = 2) AS group_rows,
COUNT(*) FILTER (WHERE node_type = 2 AND status = 1) AS group_visible_rows,
COUNT(*) FILTER (WHERE node_type = 2 AND status = 0) AS group_hidden_rows,
COUNT(*) FILTER (WHERE node_type NOT IN (0, 1, 2)) AS other_node_type_rows
FROM product.product_category
`, onlyVisible).Scan(
&stats.TotalRows,
&stats.MatchingRows,
&stats.ExpectedCategoryRows,
&stats.ExpectedVisibleRows,
&stats.ExpectedHiddenRows,
&stats.LegacyCategoryRows,
&stats.LegacyVisibleRows,
&stats.LegacyHiddenRows,
&stats.GroupRows,
&stats.GroupVisibleRows,
&stats.GroupHiddenRows,
&stats.OtherNodeTypeRows,
); err != nil {
return nil, fmt.Errorf("query category tree diagnostics: %w", err)
}
return &stats, nil
}
// ListGroupNodes loads product group nodes.
func (r *ProductRepository) ListGroupNodes(ctx context.Context, onlyVisible bool) ([]*CategoryRecord, error) {
rows, err := r.db.QueryContext(ctx, `

View File

@ -466,9 +466,6 @@ func (s *ProductServer) ListCategoryTree(ctx context.Context, req *productv1.Lis
return nil, status.Errorf(codes.Internal, "list category tree: %v", err)
}
dbSpan.SetAttributes(attribute.Int("category.result_count", len(categories)))
if len(categories) == 0 {
s.logEmptyCategoryTreeReason(ctx, dbCtx, onlyVisible)
}
observability.IncCategoryTreeLists()
resp := &productv1.ListCategoryTreeResponse{
@ -483,48 +480,6 @@ func (s *ProductServer) ListCategoryTree(ctx context.Context, req *productv1.Lis
return resp, nil
}
func (s *ProductServer) logEmptyCategoryTreeReason(ctx context.Context, dbCtx context.Context, onlyVisible bool) {
stats, err := s.repo.CategoryTreeDiagnostics(dbCtx, onlyVisible)
if err != nil {
s.observer.Logger(ctx).Warn("category tree returned empty and diagnostics failed",
zap.Bool("only_visible", onlyVisible),
zap.Error(err),
)
return
}
reason := "no rows matched category tree query"
switch {
case stats.TotalRows == 0:
reason = "product.product_category has no rows"
case stats.ExpectedCategoryRows == 0 && stats.LegacyCategoryRows > 0:
reason = "category rows use legacy node_type=0 but query expects node_type=1"
case onlyVisible && stats.ExpectedCategoryRows > 0 && stats.ExpectedVisibleRows == 0:
reason = "only_visible=true filtered out all node_type=1 category rows because status is not 1"
case stats.ExpectedCategoryRows == 0 && stats.GroupRows > 0:
reason = "table has group rows but no node_type=1 category rows"
case stats.OtherNodeTypeRows > 0:
reason = "table rows use unsupported node_type values"
}
s.observer.Logger(ctx).Info("category tree returned empty",
zap.String("reason", reason),
zap.Bool("only_visible", onlyVisible),
zap.Int64("total_rows", stats.TotalRows),
zap.Int64("matching_rows", stats.MatchingRows),
zap.Int64("expected_category_rows_node_type_1", stats.ExpectedCategoryRows),
zap.Int64("expected_visible_rows_node_type_1_status_1", stats.ExpectedVisibleRows),
zap.Int64("expected_hidden_rows_node_type_1_status_0", stats.ExpectedHiddenRows),
zap.Int64("legacy_category_rows_node_type_0", stats.LegacyCategoryRows),
zap.Int64("legacy_visible_rows_node_type_0_status_1", stats.LegacyVisibleRows),
zap.Int64("legacy_hidden_rows_node_type_0_status_0", stats.LegacyHiddenRows),
zap.Int64("group_rows_node_type_2", stats.GroupRows),
zap.Int64("group_visible_rows_node_type_2_status_1", stats.GroupVisibleRows),
zap.Int64("group_hidden_rows_node_type_2_status_0", stats.GroupHiddenRows),
zap.Int64("other_node_type_rows", stats.OtherNodeTypeRows),
)
}
// CreateCategoryTree creates a top-level category and optional second-level categories.
func (s *ProductServer) CreateCategoryTree(ctx context.Context, req *productv1.CreateCategoryTreeRequest) (*productv1.CreateCategoryTreeResponse, error) {
if req == nil {