From 71ca5b809f5b50f289bd7b7f5d6ac25a7a6e506a Mon Sep 17 00:00:00 2001 From: robinj0726 <88188467+robinj0726@users.noreply.github.com> Date: Mon, 31 Aug 2026 15:10:18 +0800 Subject: [PATCH] feat(product): add diagnostics for empty category tree responses and logging --- internal/repository/product_repository.go | 53 +++++++++++++++++++++++ internal/server/product_server.go | 45 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index e15f849..cc1ce06 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -235,6 +235,22 @@ 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 @@ -1938,6 +1954,43 @@ 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, ` diff --git a/internal/server/product_server.go b/internal/server/product_server.go index 7701fe2..aaa887f 100644 --- a/internal/server/product_server.go +++ b/internal/server/product_server.go @@ -466,6 +466,9 @@ 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{ @@ -480,6 +483,48 @@ 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 {