diff --git a/open_energy_view/errors.py b/open_energy_view/errors.py new file mode 100644 index 0000000..4ad824c --- /dev/null +++ b/open_energy_view/errors.py @@ -0,0 +1,5 @@ +"""Define errors.""" + + +class OEVErrorIntervalBlockURLNotFound(Exception): + pass diff --git a/open_energy_view/resources.py b/open_energy_view/resources.py index 094fa22..15634e7 100644 --- a/open_energy_view/resources.py +++ b/open_energy_view/resources.py @@ -26,6 +26,7 @@ from celery import chain from celery.result import AsyncResult +from . import errors from . import models from . import bcrypt from . import db @@ -367,6 +368,7 @@ def get(self): user_info = json.loads(user_info) task_ids = [] + failed_usage_points = [] for usage_point, entry in names.items(): if entry["kind"] != "electricity": @@ -384,10 +386,17 @@ def get(self): usage_point=usage_point, published_period_start=user_info.get("published_period_start"), ) + + try: + task_ids.append( + pge_api.get_historical_data_incrementally(new_source).id + ) + except errors.OEVErrorIntervalBlockURLNotFound: + failed_usage_points.append(usage_point) + continue + new_source.save_to_db() - task_ids.append( - pge_api.get_historical_data_incrementally(new_source).id - ) + except exc.IntegrityError: db.session.rollback() source = ( @@ -408,6 +417,8 @@ def get(self): task_ids.append(pge_api.get_historical_data_incrementally(source).id) if len(task_ids) == 0: + if len(failed_usage_points > 0): + return {"error": f"Could not retrieve usage points: {failed_usage_points}"}, 500 return {"message": "No electrical service submitted."}, 200 return ( task_ids[0], diff --git a/open_energy_view/utility_apis.py b/open_energy_view/utility_apis.py index c740a0e..3305cb1 100644 --- a/open_energy_view/utility_apis.py +++ b/open_energy_view/utility_apis.py @@ -9,6 +9,7 @@ from gevent import sleep +from . import errors from . import models from . import db from .espi_helpers import save_espi_xml @@ -459,7 +460,7 @@ def get_historical_data_incrementally(self, source): else: print("Could not find interval block url") save_espi_xml(response_text, filename=f"SubRespForSource{source.id}") - return {"error": "could not find interval block url"}, 500 + raise errors.OEVErrorIntervalBlockURLNotFound published_period_start = source.published_period_start