From 8e4993d3c58d7bda975bd14b243d1f3a9d6bb8bf Mon Sep 17 00:00:00 2001 From: primal Date: Thu, 29 Jan 2026 13:00:13 -0500 Subject: [PATCH] Optimize stats and TLD queries for performance - Reduce stats loop interval from 1 minute to 10 seconds - Simplify TLD query by removing expensive feeds JOIN (30s -> 1.8s) - Sort TLDs alphabetically Co-Authored-By: Claude Opus 4.5 --- crawler.go | 4 ++-- dashboard.go | 21 +++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/crawler.go b/crawler.go index 65cb636..ca1259c 100644 --- a/crawler.go +++ b/crawler.go @@ -68,11 +68,11 @@ func (c *Crawler) Close() error { return nil } -// StartStatsLoop updates cached stats once per minute +// StartStatsLoop updates cached stats every 10 seconds func (c *Crawler) StartStatsLoop() { for { c.UpdateStats() - time.Sleep(1 * time.Minute) + time.Sleep(10 * time.Second) } } diff --git a/dashboard.go b/dashboard.go index 5cc8732..7d15bb5 100644 --- a/dashboard.go +++ b/dashboard.go @@ -1421,17 +1421,14 @@ func (c *Crawler) filterFeeds(w http.ResponseWriter, tld, domain, status string, } func (c *Crawler) handleAPITLDs(w http.ResponseWriter, r *http.Request) { + // Simple count by TLD from domains table (fast with index) + // Feed counts fetched separately per-TLD if needed rows, err := c.db.Query(` - SELECT d.tld, COUNT(DISTINCT d.host)::int as domain_count, COALESCE(SUM(f.feed_count), 0)::int as feed_count - FROM domains d - LEFT JOIN ( - SELECT source_host, COUNT(*)::int as feed_count - FROM feeds - GROUP BY source_host - ) f ON d.host = f.source_host - WHERE d.tld IS NOT NULL AND d.tld != '' - GROUP BY d.tld - ORDER BY d.tld ASC + SELECT tld, COUNT(*)::int as domain_count + FROM domains + WHERE tld IS NOT NULL AND tld != '' + GROUP BY tld + ORDER BY tld ASC `) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -1445,10 +1442,10 @@ func (c *Crawler) handleAPITLDs(w http.ResponseWriter, r *http.Request) { FeedCount int `json:"feed_count"` } - tlds := []TLDInfo{} // Initialize as empty slice, not nil + tlds := []TLDInfo{} for rows.Next() { var t TLDInfo - if err := rows.Scan(&t.TLD, &t.DomainCount, &t.FeedCount); err != nil { + if err := rows.Scan(&t.TLD, &t.DomainCount); err != nil { continue } tlds = append(tlds, t)