Skip to content

Commit

Permalink
only log anomalous state leading to undefined min/max for an existing…
Browse files Browse the repository at this point in the history
… cost surface

Avoid automatically deleting the scenario: we log the anomalous
situation and arbitrarily set the cost surface's range to [0,0]. In
practice, the scenario that uses such cost surface may have contained
invalid or incomplete data to start with, so setting an arbitrary range
may not really matter here (as in, it should not make things worse).
  • Loading branch information
hotzevzl committed Oct 6, 2023
1 parent 6e427bf commit ecc6781
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions data/scripts/cost-surface/cost-surface-data-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,28 @@
min_max = cur_geo_db.fetchone()
min_val, max_val = min_max if min_max else (None, None)

# If min and max are null, remove scenario and cost_surface from api_db
if min_val is None or max_val is None:
cur_api_db.execute("DELETE FROM scenarios WHERE id = %s;", (scenario_id,))
cur_api_db.execute("DELETE FROM cost_surfaces WHERE id = %s;", (cost_surface_id,))
orphan_scenario_ids.append(scenario_id)
else:
# Update min and max in the cost_surfaces table in the api_db
cur_api_db.execute("""
UPDATE cost_surfaces
SET min = %s, max = %s
WHERE id = %s;
""", (min_val, max_val, cost_surface_id))
print(f'''Could not find Min/Max for scenario {scenario_id}, cost surface {cost_surface_id}.
This may mean that no scenarios_pu_cost_data rows were created for this scenario.
This should never happen, so data for this scenario should be checked.
The scenario may be invalid, and it may need to be deleted.
''')

# If min and max are null, remove scenario and cost_surface from api_db
if min_val is None:
min_val = 0
print(f'Setting min to 0 for scenario {scenario_id}, cost surface {cost_surface_id}')
if max_val is None:
max_val = 0
print(f'Setting max to 0 for scenario {scenario_id}, cost surface {cost_surface_id}')

# Update min and max in the cost_surfaces table in the api_db
cur_api_db.execute("""
UPDATE cost_surfaces
SET min = %s, max = %s
WHERE id = %s;
""", (min_val, max_val, cost_surface_id))

#Default Cost Surface for projects
cur_api_db.execute("SELECT id FROM projects;")
Expand Down

0 comments on commit ecc6781

Please sign in to comment.