fix(national): Count active states by product data, not crawl status

Active states should count states with actual product data, not just
states where crawling is enabled. A state can have historical data
even if crawling is currently disabled.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Kelly
2025-12-10 23:23:20 -07:00
parent dffc124920
commit de93669652

View File

@@ -702,23 +702,25 @@ export class StateQueryService {
async getNationalSummary(): Promise<NationalSummary> {
const stateMetrics = await this.getAllStateMetrics();
// Active states = states with at least one crawl-enabled dispensary
// Active states = states with at least one dispensary that has products
const result = await this.pool.query(`
SELECT
COUNT(DISTINCT s.code) AS total_states,
COUNT(DISTINCT CASE WHEN EXISTS (
SELECT 1 FROM dispensaries d WHERE d.state = s.code AND d.crawl_enabled = true
SELECT 1 FROM dispensaries d
JOIN store_products sp ON sp.dispensary_id = d.id
WHERE d.state = s.code
) THEN s.code END) AS active_states,
(SELECT COUNT(*) FROM dispensaries WHERE state IS NOT NULL AND crawl_enabled = true) AS total_stores,
(SELECT COUNT(*) FROM dispensaries WHERE state IS NOT NULL) AS total_stores,
(SELECT COUNT(*) FROM store_products sp
JOIN dispensaries d ON sp.dispensary_id = d.id
WHERE d.state IS NOT NULL AND d.crawl_enabled = true) AS total_products,
WHERE d.state IS NOT NULL) AS total_products,
(SELECT COUNT(DISTINCT brand_id) FROM store_products sp
JOIN dispensaries d ON sp.dispensary_id = d.id
WHERE d.state IS NOT NULL AND d.crawl_enabled = true AND sp.brand_id IS NOT NULL) AS total_brands,
WHERE d.state IS NOT NULL AND sp.brand_id IS NOT NULL) AS total_brands,
(SELECT AVG(price_rec) FROM store_products sp
JOIN dispensaries d ON sp.dispensary_id = d.id
WHERE d.state IS NOT NULL AND d.crawl_enabled = true AND sp.price_rec > 0) AS avg_price_national
WHERE d.state IS NOT NULL AND sp.price_rec > 0) AS avg_price_national
FROM states s
`);