11import logging
2+ from datetime import datetime
23from flask import Flask , render_template
34from graphene import Schema
45from graphql .utils import schema_printer
1112from flask_graphql import GraphQLView
1213
1314# Set up logging at module level
14- logging .basicConfig (format = "%(asctime)s %(levelname)-8s %(message)s" ,
15- level = logging .INFO ,
16- datefmt = "%Y-%m-%d %H:%M:%S" )
15+ logging .basicConfig (format = "%(asctime)s %(levelname)-8s %(message)s" , level = logging .INFO , datefmt = "%Y-%m-%d %H:%M:%S" )
1716logger = logging .getLogger (__name__ )
1817
18+
1919def create_app (run_migrations = False ):
2020 """
2121 Application factory for Flask app.
@@ -37,14 +37,13 @@ def create_app(run_migrations=False):
3737 if not all ([db_user , db_password , db_name , db_host , db_port ]):
3838 logger .error ("Missing required database configuration variables" )
3939 raise ValueError (
40- "Missing required database configuration. "
41- "Please ensure all database environment variables are set."
40+ "Missing required database configuration. " "Please ensure all database environment variables are set."
4241 )
4342
4443 # Configure database
4544 logger .info ("Configuring database connection to %s:%s/%s" , db_host , db_port , db_name )
46- app .config [' SQLALCHEMY_DATABASE_URI' ] = db_url
47- app .config [' SQLALCHEMY_TRACK_MODIFICATIONS' ] = False
45+ app .config [" SQLALCHEMY_DATABASE_URI" ] = db_url
46+ app .config [" SQLALCHEMY_TRACK_MODIFICATIONS" ] = False
4847
4948 # Set up extensions
5049 logger .info ("Setting up Flask extensions" )
@@ -85,11 +84,12 @@ def shutdown_session(exception=None):
8584 logger .info ("Application initialization complete" )
8685 return app
8786
87+
8888def setup_scrapers (app ):
8989 """Set up scrapers and scheduled tasks"""
9090 # Import scraper-related modules only when needed
9191 from flask_apscheduler import APScheduler
92- from src .scrapers .capacities_scraper import fetch_capacities
92+ from src .scrapers .capacities_scraper import fetch_capacities , update_hourly_capacity
9393 from src .scrapers .reg_hours_scraper import fetch_reg_building , fetch_reg_facility
9494 from src .scrapers .scraper_helpers import clean_past_hours
9595 from src .scrapers .sp_hours_scraper import fetch_sp_facility
@@ -110,9 +110,9 @@ def setup_scrapers(app):
110110 # Scrape hours every 15 minutes
111111 @scheduler .task ("interval" , id = "scrape_hours" , seconds = 900 )
112112 def scrape_hours ():
113- job = scheduler .get_job (' scrape_hours' )
114- next_run = job .next_run_time .strftime (' %Y-%m-%d %H:%M:%S' ) if job and job .next_run_time else "Unknown"
115- logging .info (" Running job \ " scrape_hours (trigger: interval[0:15:00], next run at: %s EST)\" " , next_run )
113+ job = scheduler .get_job (" scrape_hours" )
114+ next_run = job .next_run_time .strftime (" %Y-%m-%d %H:%M:%S" ) if job and job .next_run_time else "Unknown"
115+ logging .info (' Running job "scrape_hours (trigger: interval[0:15:00], next run at: %s EST)"' , next_run )
116116 try :
117117 logging .info ("Scraping hours from sheets..." )
118118 # Clear hours
@@ -122,38 +122,54 @@ def scrape_hours():
122122 fetch_sp_facility ()
123123 clean_past_hours ()
124124 logging .info (
125- "Job \" scrape_hours (trigger: interval[0:15:00], next run at: %s EST)\" executed successfully" , next_run )
125+ 'Job "scrape_hours (trigger: interval[0:15:00], next run at: %s EST)" executed successfully' , next_run
126+ )
126127 except Exception as e :
127128 logging .error (f"Error in scrape_hours: { e } " )
128129
129130 # Scrape capacities every 10 minutes
130131 @scheduler .task ("interval" , id = "scrape_capacities" , seconds = 600 )
131132 def scrape_capacities ():
132- job = scheduler .get_job (' scrape_capacities' )
133- next_run = job .next_run_time .strftime (' %Y-%m-%d %H:%M:%S' ) if job and job .next_run_time else "Unknown"
134- logging .info (" Running job \ " scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)\" " , next_run )
133+ job = scheduler .get_job (" scrape_capacities" )
134+ next_run = job .next_run_time .strftime (" %Y-%m-%d %H:%M:%S" ) if job and job .next_run_time else "Unknown"
135+ logging .info (' Running job "scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)"' , next_run )
135136 try :
136137 logging .info ("Scraping capacities from C2C..." )
137138 fetch_capacities ()
138139 logging .info (
139- "Job \" scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)\" executed successfully" , next_run )
140+ 'Job "scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)" executed successfully' ,
141+ next_run ,
142+ )
140143 except Exception as e :
141144 logging .error (f"Error in scrape_capacities: { e } " )
142145
143146 # Scrape classes every hour
144147 @scheduler .task ("interval" , id = "scrape_classes" , seconds = 3600 )
145148 def scrape_classes ():
146- job = scheduler .get_job (' scrape_classes' )
147- next_run = job .next_run_time .strftime (' %Y-%m-%d %H:%M:%S' ) if job and job .next_run_time else "Unknown"
148- logging .info (" Running job \ " scrape_classes (trigger: interval[1:00:00], next run at: %s EST)\" " , next_run )
149+ job = scheduler .get_job (" scrape_classes" )
150+ next_run = job .next_run_time .strftime (" %Y-%m-%d %H:%M:%S" ) if job and job .next_run_time else "Unknown"
151+ logging .info (' Running job "scrape_classes (trigger: interval[1:00:00], next run at: %s EST)"' , next_run )
149152 try :
150153 logging .info ("Scraping classes from group-fitness-classes..." )
151154 fetch_classes (10 )
152155 logging .info (
153- "Job \" scrape_classes (trigger: interval[1:00:00], next run at: %s EST)\" executed successfully" , next_run )
156+ 'Job "scrape_classes (trigger: interval[1:00:00], next run at: %s EST)" executed successfully' , next_run
157+ )
154158 except Exception as e :
155159 logging .error (f"Error in scrape_classes: { e } " )
156160
161+ # Update hourly average capacity every hour
162+ @scheduler .task ("cron" , id = "update_capacity" , hour = "*" )
163+ def scheduled_job ():
164+ current_time = datetime .now ()
165+ current_day = current_time .strftime ("%A" ).upper ()
166+ current_hour = current_time .hour
167+ try :
168+ logging .info (f"Updating hourly average capacity for { current_day } , hour { current_hour } ..." )
169+ update_hourly_capacity (current_day , current_hour )
170+ except Exception as e :
171+ logging .error (f"Error updating hourly average capacity for { current_day } , hour { current_hour } : { e } " )
172+
157173 # We're now handling job execution logging within each task function
158174
159175 # Initialize scheduler
0 commit comments