Point main dashboard stats to AZ pipeline DB

This commit is contained in:
Kelly
2025-12-02 10:36:58 -07:00
parent 53a0918233
commit 8c83bd451a

View File

@@ -1,15 +1,16 @@
import { Router } from 'express';
import { authMiddleware } from '../auth/middleware';
import { pool } from '../db/migrate';
import { query as azQuery } from '../dutchie-az/db/connection'; // AZ pipeline DB
const router = Router();
router.use(authMiddleware);
// Get dashboard stats
// Get dashboard stats - now sourced from AZ pipeline (dutchie_az DB)
router.get('/stats', async (req, res) => {
try {
// Dispensary stats (using unified dispensaries table)
const dispensariesResult = await pool.query(`
// Dispensary stats (AZ pipeline)
const dispensariesResult = await azQuery(`
SELECT
COUNT(*) as total,
COUNT(*) FILTER (WHERE scrape_enabled = true) as active,
@@ -20,16 +21,15 @@ router.get('/stats', async (req, res) => {
FROM dispensaries
`);
// Current product stats (active inventory only)
const productsResult = await pool.query(`
// Product stats from AZ pipeline (dutchie_products)
const productsResult = await azQuery(`
SELECT
COUNT(*) as total,
COUNT(*) FILTER (WHERE in_stock = true) as in_stock,
COUNT(*) FILTER (WHERE local_image_path IS NOT NULL) as with_images,
COUNT(DISTINCT brand) as unique_brands,
COUNT(*) FILTER (WHERE stock_status = 'in_stock') as in_stock,
COUNT(*) FILTER (WHERE primary_image_url IS NOT NULL) as with_images,
COUNT(DISTINCT brand_name) as unique_brands,
COUNT(DISTINCT dispensary_id) as stores_with_products
FROM products
WHERE last_seen_at >= NOW() - INTERVAL '7 days'
FROM dutchie_products
`);
// Campaign stats
@@ -47,11 +47,11 @@ router.get('/stats', async (req, res) => {
WHERE clicked_at >= NOW() - INTERVAL '24 hours'
`);
// Recent products added (last 24 hours)
const recentProductsResult = await pool.query(`
// Recent products added (last 24 hours) from AZ pipeline
const recentProductsResult = await azQuery(`
SELECT COUNT(*) as new_products_24h
FROM products
WHERE first_seen_at >= NOW() - INTERVAL '24 hours'
FROM dutchie_products
WHERE created_at >= NOW() - INTERVAL '24 hours'
`);
// Proxy stats
@@ -77,7 +77,7 @@ router.get('/stats', async (req, res) => {
}
});
// Get recent activity
// Get recent activity - uses AZ data
router.get('/activity', async (req, res) => {
try {
const { limit = 20 } = req.query;
@@ -87,19 +87,29 @@ router.get('/activity', async (req, res) => {
SELECT
COALESCE(d.dba_name, d.name) as name,
d.last_crawl_at as last_scraped_at,
(SELECT COUNT(*) FROM products p WHERE p.dispensary_id = d.id AND p.last_seen_at >= NOW() - INTERVAL '7 days') as product_count
(SELECT COUNT(*) FROM dutchie_products p WHERE p.dispensary_id = d.id) as product_count
FROM dispensaries d
WHERE d.last_crawl_at IS NOT NULL
ORDER BY d.last_crawl_at DESC
LIMIT $1
`, [limit]);
// Recent products
// Recent products from AZ pipeline
const productsResult = await pool.query(`
SELECT p.name, p.price, COALESCE(d.dba_name, d.name) as store_name, p.first_seen_at
FROM products p
SELECT
p.name,
COALESCE(
(SELECT (options->0->>'price')::numeric
FROM dutchie_product_snapshots s
WHERE s.dutchie_product_id = p.id
ORDER BY crawled_at DESC LIMIT 1),
0
) as price,
COALESCE(d.dba_name, d.name) as store_name,
p.created_at as first_seen_at
FROM dutchie_products p
JOIN dispensaries d ON p.dispensary_id = d.id
ORDER BY p.first_seen_at DESC
ORDER BY p.created_at DESC
LIMIT $1
`, [limit]);