diff --git a/india_forecast_app/app.py b/india_forecast_app/app.py index d1a4e41..13047d4 100644 --- a/india_forecast_app/app.py +++ b/india_forecast_app/app.py @@ -275,7 +275,7 @@ def app(timestamp: dt.datetime | None, write_to_db: bool, log_level: str): app_run(timestamp=timestamp, write_to_db=write_to_db, log_level=log_level) -def app_run(timestamp: dt.datetime | None, write_to_db: bool=False, log_level: str= "info"): +def app_run(timestamp: dt.datetime | None, write_to_db: bool = False, log_level: str = "info"): """ Main function for running forecasts for sites in India """ @@ -308,7 +308,7 @@ def app_run(timestamp: dt.datetime | None, write_to_db: bool=False, log_level: s # 2. Load data/models all_model_configs = get_all_models(client_abbreviation=os.getenv("CLIENT_NAME", "ruvnl")) - models = [] + successful_runs = 0 for model_config in all_model_configs.models: asset_sites = pv_sites if model_config.asset_type == "pv" else wind_sites @@ -337,39 +337,35 @@ def app_run(timestamp: dt.datetime | None, write_to_db: bool=False, log_level: s ) ml_model.site_uuid = site.site_uuid - # TODO should we make this an object? - models.append(ml_model) log.info(f"{asset_type} model loaded") - successful_runs = 0 - for model in models: - # 3. Run model for each site - site_id = model.site_uuid - asset_type = model.asset_type - log.info(f"Running {asset_type} model for site={site_id}...") - forecast_values = run_model(model=model, site_id=site_id, timestamp=timestamp) - - if forecast_values is None: - log.info(f"No forecast values for site_id={site_id}") - else: - # 4. Write forecast to DB or stdout - log.info(f"Writing forecast for site_id={site_id}") - forecast = { - "meta": { - "site_id": site_id, - "version": version, - "timestamp": timestamp, - }, - "values": forecast_values, - } - save_forecast( - session, - forecast=forecast, - write_to_db=write_to_db, - ml_model_name=model.name, - ml_model_version=version, - ) - successful_runs += 1 + # 3. Run model for each site + site_id = ml_model.site_uuid + asset_type = ml_model.asset_type + log.info(f"Running {asset_type} model for site={site_id}...") + forecast_values = run_model(model=ml_model, site_id=site_id, timestamp=timestamp) + + if forecast_values is None: + log.info(f"No forecast values for site_id={site_id}") + else: + # 4. Write forecast to DB or stdout + log.info(f"Writing forecast for site_id={site_id}") + forecast = { + "meta": { + "site_id": site_id, + "version": version, + "timestamp": timestamp, + }, + "values": forecast_values, + } + save_forecast( + session, + forecast=forecast, + write_to_db=write_to_db, + ml_model_name=ml_model.name, + ml_model_version=version, + ) + successful_runs += 1 log.info(f"Completed forecasts for {successful_runs} runs for {len(sites)} sites") if successful_runs == len(sites):