Skip to content

Commit a84633d

Browse files
committed
report-listener: speed up by-brand search path and skip n=1 heavy counts
1 parent 4b58e53 commit a84633d

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

report-listener/.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
BUILD_VERSION=1.0.114
1+
BUILD_VERSION=1.0.115

report-listener/database/database.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,14 +1472,15 @@ func (d *Database) GetReportsByLatLngLite(ctx context.Context, latitude, longitu
14721472
// PERFORMANCE: Skip report_status and reports_owners checks for speed
14731473
// These tables are sparsely populated and add 40+ seconds to large brand queries
14741474
func (d *Database) GetReportsByBrandName(ctx context.Context, brandName string, limit int) ([]models.ReportWithAnalysis, error) {
1475-
// FAST PATH: Query directly on report_analysis index, skip rarely-used status/owner tables
1475+
// FAST PATH: Drive from report_analysis with the brand+valid+seq index and
1476+
// order by ra.seq to avoid slow join/order plans on reports for high-volume brands.
14761477
reportsQuery := `
1477-
SELECT r.seq, r.ts, r.id, r.latitude, r.longitude
1478-
FROM reports r
1479-
INNER JOIN report_analysis ra ON r.seq = ra.seq
1478+
SELECT ra.seq, r.ts, r.id, r.latitude, r.longitude
1479+
FROM report_analysis ra FORCE INDEX (idx_ra_brand_valid_seq)
1480+
INNER JOIN reports r ON r.seq = ra.seq
14801481
WHERE ra.brand_name = ?
14811482
AND ra.is_valid = TRUE
1482-
ORDER BY r.seq DESC
1483+
ORDER BY ra.seq DESC
14831484
LIMIT ?
14841485
`
14851486

report-listener/handlers/handlers.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -981,18 +981,21 @@ func (h *Handlers) GetReportsByBrand(c *gin.Context) {
981981
}
982982

983983
// Get counts with a bounded timeout to avoid slow/full-table scans blocking UI.
984+
// For n=1 (digital search flow), skip aggregate counts entirely.
984985
totalCount := len(reports)
985986
highPriorityCount := 0
986987
mediumPriorityCount := 0
987-
countCtx, cancelCounts := context.WithTimeout(c.Request.Context(), 4*time.Second)
988-
defer cancelCounts()
989-
t, hpc, mpc, err := h.db.GetBrandPriorityCountsByBrandName(countCtx, brandName)
990-
if err != nil {
991-
log.Printf("Failed to get aggregated counts for brand '%s' (fallbacking to partial): %v", brandName, err)
992-
} else {
993-
totalCount = t
994-
highPriorityCount = hpc
995-
mediumPriorityCount = mpc
988+
if limit > 1 {
989+
countCtx, cancelCounts := context.WithTimeout(c.Request.Context(), 1200*time.Millisecond)
990+
defer cancelCounts()
991+
t, hpc, mpc, err := h.db.GetBrandPriorityCountsByBrandName(countCtx, brandName)
992+
if err != nil {
993+
log.Printf("Failed to get aggregated counts for brand '%s' (fallbacking to partial): %v", brandName, err)
994+
} else {
995+
totalCount = t
996+
highPriorityCount = hpc
997+
mediumPriorityCount = mpc
998+
}
996999
}
9971000

9981001
// Create the response in the same format as other endpoints

0 commit comments

Comments
 (0)