Add dispensary_crawl_status view to consolidated schema
This commit is contained in:
@@ -852,6 +852,8 @@ WHERE NOT EXISTS (SELECT 1 FROM public.users WHERE email = 'admin@example.com');
|
|||||||
|
|
||||||
DROP VIEW IF EXISTS public.v_dashboard_stats CASCADE;
|
DROP VIEW IF EXISTS public.v_dashboard_stats CASCADE;
|
||||||
DROP VIEW IF EXISTS public.v_latest_snapshots CASCADE;
|
DROP VIEW IF EXISTS public.v_latest_snapshots CASCADE;
|
||||||
|
DROP VIEW IF EXISTS public.v_brands CASCADE;
|
||||||
|
DROP VIEW IF EXISTS public.v_categories CASCADE;
|
||||||
DROP VIEW IF EXISTS public.v_product_brands CASCADE;
|
DROP VIEW IF EXISTS public.v_product_brands CASCADE;
|
||||||
DROP VIEW IF EXISTS public.v_product_categories CASCADE;
|
DROP VIEW IF EXISTS public.v_product_categories CASCADE;
|
||||||
|
|
||||||
@@ -874,10 +876,11 @@ SELECT
|
|||||||
(SELECT COUNT(DISTINCT (type, subcategory)) FROM public.dutchie_products WHERE type IS NOT NULL) as category_count;
|
(SELECT COUNT(DISTINCT (type, subcategory)) FROM public.dutchie_products WHERE type IS NOT NULL) as category_count;
|
||||||
|
|
||||||
-- Brands derived from products
|
-- Brands derived from products
|
||||||
CREATE OR REPLACE VIEW public.v_product_brands AS
|
CREATE OR REPLACE VIEW public.v_brands AS
|
||||||
SELECT
|
SELECT
|
||||||
brand_name,
|
brand_name,
|
||||||
brand_id,
|
brand_id,
|
||||||
|
MAX(brand_logo_url) as brand_logo_url,
|
||||||
COUNT(*) as product_count,
|
COUNT(*) as product_count,
|
||||||
COUNT(DISTINCT dispensary_id) as dispensary_count,
|
COUNT(DISTINCT dispensary_id) as dispensary_count,
|
||||||
ARRAY_AGG(DISTINCT type) FILTER (WHERE type IS NOT NULL) as product_types
|
ARRAY_AGG(DISTINCT type) FILTER (WHERE type IS NOT NULL) as product_types
|
||||||
@@ -887,16 +890,62 @@ GROUP BY brand_name, brand_id
|
|||||||
ORDER BY product_count DESC;
|
ORDER BY product_count DESC;
|
||||||
|
|
||||||
-- Categories derived from products
|
-- Categories derived from products
|
||||||
CREATE OR REPLACE VIEW public.v_product_categories AS
|
CREATE OR REPLACE VIEW public.v_categories AS
|
||||||
SELECT
|
SELECT
|
||||||
type,
|
type,
|
||||||
subcategory,
|
subcategory,
|
||||||
COUNT(*) as product_count,
|
COUNT(*) as product_count,
|
||||||
COUNT(DISTINCT dispensary_id) as dispensary_count,
|
COUNT(DISTINCT dispensary_id) as dispensary_count,
|
||||||
COUNT(DISTINCT brand_name) as brand_count
|
AVG(thc) as avg_thc,
|
||||||
|
MIN(thc) as min_thc,
|
||||||
|
MAX(thc) as max_thc
|
||||||
FROM public.dutchie_products
|
FROM public.dutchie_products
|
||||||
|
WHERE type IS NOT NULL
|
||||||
GROUP BY type, subcategory
|
GROUP BY type, subcategory
|
||||||
ORDER BY product_count DESC;
|
ORDER BY type, subcategory;
|
||||||
|
|
||||||
|
-- Dispensary crawl status view (consolidated schema)
|
||||||
|
DROP VIEW IF EXISTS public.dispensary_crawl_status CASCADE;
|
||||||
|
CREATE OR REPLACE VIEW public.dispensary_crawl_status AS
|
||||||
|
SELECT
|
||||||
|
d.id AS dispensary_id,
|
||||||
|
COALESCE(d.dba_name, d.name) AS dispensary_name,
|
||||||
|
d.slug AS dispensary_slug,
|
||||||
|
d.city,
|
||||||
|
d.state,
|
||||||
|
d.menu_url,
|
||||||
|
d.menu_type,
|
||||||
|
d.scrape_enabled,
|
||||||
|
d.last_crawl_at,
|
||||||
|
d.crawl_status,
|
||||||
|
d.crawl_error,
|
||||||
|
cs.cron_expression,
|
||||||
|
cs.is_active,
|
||||||
|
cs.priority,
|
||||||
|
cs.last_run_at,
|
||||||
|
cs.next_run_at,
|
||||||
|
cs.last_status AS schedule_last_status,
|
||||||
|
cs.last_error AS schedule_last_error,
|
||||||
|
cs.consecutive_failures,
|
||||||
|
j.id AS latest_job_id,
|
||||||
|
j.status AS latest_job_status,
|
||||||
|
j.job_type AS latest_job_type,
|
||||||
|
j.started_at AS latest_job_started,
|
||||||
|
j.completed_at AS latest_job_completed,
|
||||||
|
j.products_found AS latest_products_found,
|
||||||
|
j.products_created AS latest_products_created,
|
||||||
|
j.products_updated AS latest_products_updated,
|
||||||
|
j.error_message AS latest_job_error
|
||||||
|
FROM public.dispensaries d
|
||||||
|
LEFT JOIN public.dispensary_crawl_schedule cs ON cs.dispensary_id = d.id
|
||||||
|
LEFT JOIN LATERAL (
|
||||||
|
SELECT *
|
||||||
|
FROM public.dispensary_crawl_jobs dj
|
||||||
|
WHERE dj.dispensary_id = d.id
|
||||||
|
ORDER BY dj.created_at DESC
|
||||||
|
LIMIT 1
|
||||||
|
) j ON true
|
||||||
|
WHERE d.state = 'AZ';
|
||||||
|
|
||||||
-- Done!
|
-- Done!
|
||||||
SELECT 'Migration 031 completed successfully' as status;
|
SELECT 'Migration 031 completed successfully' as status;
|
||||||
|
|||||||
Reference in New Issue
Block a user