Skip to content

Commit

Permalink
Update etl_processor.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmio committed Dec 9, 2024
1 parent dbc90a4 commit e4aa86c
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions functions/etl_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,89 @@ def create_analytics_views(self) -> None:
logger.error(f"Error creating grower_achievements view: {str(e)}")
raise

# Create regional records views
try:
regional_records_sql = """
-- Create regional records view
DROP MATERIALIZED VIEW IF EXISTS analytics.regional_records;
CREATE MATERIALIZED VIEW analytics.regional_records AS
WITH ranked_entries AS (
SELECT
e.category,
e.weight_lbs,
e.grower_name,
e.gpc_site,
e.year,
e.state_prov,
e.country,
r.region_name,
RANK() OVER (
PARTITION BY e.category, rm.region_id
ORDER BY e.weight_lbs DESC
) as rank_in_region
FROM core.entries e
JOIN analytics.region_mappings rm ON
e.country = rm.country AND
(e.state_prov = rm.state_prov OR rm.state_prov IS NULL)
JOIN analytics.regions r ON rm.region_id = r.region_id
WHERE e.entry_type NOT IN ('DMG', 'EXH')
)
SELECT
category,
region_name,
weight_lbs,
grower_name,
gpc_site,
year,
state_prov,
country
FROM ranked_entries
WHERE rank_in_region = 1
ORDER BY category, region_name;
-- Create regional records by year view
DROP MATERIALIZED VIEW IF EXISTS analytics.regional_records_by_year;
CREATE MATERIALIZED VIEW analytics.regional_records_by_year AS
WITH ranked_entries AS (
SELECT
e.category,
e.weight_lbs,
e.grower_name,
e.gpc_site,
e.year,
e.state_prov,
e.country,
r.region_name,
RANK() OVER (
PARTITION BY e.category, rm.region_id, e.year
ORDER BY e.weight_lbs DESC
) as rank_in_region
FROM core.entries e
JOIN analytics.region_mappings rm ON
e.country = rm.country AND
(e.state_prov = rm.state_prov OR rm.state_prov IS NULL)
JOIN analytics.regions r ON rm.region_id = r.region_id
WHERE e.entry_type NOT IN ('DMG', 'EXH')
)
SELECT
category,
region_name,
year,
weight_lbs,
grower_name,
gpc_site,
state_prov,
country
FROM ranked_entries
WHERE rank_in_region = 1
ORDER BY category, region_name, year DESC;
"""
self.supabase.rpc('execute_sql', {'query': regional_records_sql}).execute()
logger.info("Created regional records views")
except Exception as e:
logger.error(f"Error creating regional records views: {str(e)}")
raise

except Exception as e:
logger.error(f"Error creating analytics views: {str(e)}")
if hasattr(e, 'message'):
Expand Down

0 comments on commit e4aa86c

Please sign in to comment.