Skip to content

Commit

Permalink
Add cybermodels support in the OasisAPI - V1 Only (#1133)
Browse files Browse the repository at this point in the history
* tmp

* retun checks for V2 runs

* Only block input generation with a missing loc file on run_mode = V2

* Revert "retun checks for V2 runs"

This reverts commit 925474d.

* fix

* Update error in v2 test

* Fix testing and validation

* update port val messages
  • Loading branch information
sambles authored Oct 31, 2024
1 parent 559fb35 commit a585706
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/model_execution_worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,12 @@ def generate_input(self,
task_params['model_settings_file'] = model_settings_fp if model_settings_fp and os.path.isfile(model_settings_fp) else None

# Fetch input files
loc_extention = "".join(pathlib.Path(loc_file).suffixes)
task_params['oed_location_csv'] = filestore.get(
loc_file,
os.path.join(oasis_files_dir, f'location{loc_extention}'),
required=True
)
if loc_file:
loc_extention = "".join(pathlib.Path(loc_file).suffixes)
task_params['oed_location_csv'] = filestore.get(
loc_file,
os.path.join(oasis_files_dir, f'location{loc_extention}')
)
if acc_file:
acc_extention = "".join(pathlib.Path(acc_file).suffixes)
task_params['oed_accounts_csv'] = filestore.get(
Expand Down
47 changes: 28 additions & 19 deletions src/server/oasisapi/analyses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,19 @@ def generate_inputs(self, initiator, run_mode_override=None):
self.status_choices.RUN_CANCELLED,
self.status_choices.RUN_ERROR,
]
valid_run_modes = [
self.run_mode_choices.V1,
self.run_mode_choices.V2,
]

# check run model
run_mode = run_mode_override if run_mode_override else self.model.run_mode
if run_mode not in valid_run_modes:
raise ValidationError(
{'run_mode': ['run_mode must be [{}]'.format(', '.join(valid_run_modes))]}
)

# check everything else
errors = {}
if self.status not in valid_choices:
errors['status'] = ['Analysis status must be one of [{}]'.format(', '.join(valid_choices))]
Expand All @@ -604,29 +616,26 @@ def generate_inputs(self, initiator, run_mode_override=None):
if (self.model.run_mode is None) and (run_mode_override is None):
errors['model'] = ['Model pk "{}" - "run_mode" must not be null'.format(self.model.id)]

if not self.portfolio.location_file:
errors['portfolio'] = ['"location_file" must not be null']
else:
try:
loc_lines = self.portfolio.location_file_len()
except Exception as e:
errors['portfolio'] = [f"Failed to read location file size for chunking: {e}"]
if loc_lines < 1:
errors['portfolio'] = ['"location_file" must at least one row']
# check for eitehr location or account file if V1
if run_mode == self.run_mode_choices.V1:
if (not self.portfolio.location_file) and (not self.portfolio.accounts_file):
errors['portfolio'] = ['Either "location_file" or "accounts_file" must not be null for run_mode = V1']

# check for location file if V2
if run_mode == self.run_mode_choices.V2:
if not self.portfolio.location_file:
errors['portfolio'] = ['"location_file" must not be null for run_mode = V2']
else:
try:
loc_lines = self.portfolio.location_file_len()
except Exception as e:
errors['portfolio'] = [f"Failed to read location file size for chunking: {e}"]
if loc_lines < 1:
errors['portfolio'] = ['"location_file" must at least one row']

if errors:
raise ValidationError(errors)

valid_run_modes = [
self.run_mode_choices.V1,
self.run_mode_choices.V2,
]
run_mode = run_mode_override if run_mode_override else self.model.run_mode
if run_mode not in valid_run_modes:
raise ValidationError(
{'run_mode': ['run_mode must be [{}]'.format(', '.join(valid_run_modes))]}
)

self.status = self.status_choices.INPUTS_GENERATION_QUEUED
self.lookup_errors_file = None
self.lookup_success_file = None
Expand Down
4 changes: 2 additions & 2 deletions src/server/oasisapi/analyses/v1_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def validate(self, attrs):

# Check that portfilio has a location file
if attrs.get('portfolio'):
if not attrs['portfolio'].location_file:
raise ValidationError({'portfolio': '"location_file" must not be null'})
if (not attrs['portfolio'].location_file) and (not attrs['portfolio'].accounts_file):
raise ValidationError({'portfolio': 'either "location_file" or "accounts_file" must not be null'})

# check that model isn't soft-deleted
if attrs.get('model'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ def test_portfolio_has_no_location_file___validation_error_is_raised_revoke_is_n
with self.assertRaises(ValidationError) as ex:
analysis.generate_inputs(initiator, run_mode_override='V1')

self.assertEqual({'portfolio': ['"location_file" must not be null']}, ex.exception.detail)
self.assertEqual(
{'portfolio': ['Either "location_file" or "accounts_file" must not be null for run_mode = V1']}, ex.exception.detail)

self.assertEqual(Analysis.status_choices.NEW, analysis.status)
self.assertFalse(res_factory.revoke_called)
Expand Down
4 changes: 2 additions & 2 deletions src/server/oasisapi/analyses/v2_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ def validate(self, attrs):
except ValidationError:
raise ValidationError({'portfolio': 'You are not allowed to use this portfolio'})

if not attrs['portfolio'].location_file:
raise ValidationError({'portfolio': '"location_file" must not be null'})
if (not attrs['portfolio'].location_file) and (not attrs['portfolio'].accounts_file):
raise ValidationError({'portfolio': 'either "location_file" or "accounts_file" must not be null'})

# check that model isn't soft-deleted
if attrs.get('model'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def test_portfolio_has_no_location_file___validation_error_is_raised_revoke_is_n
with self.assertRaises(ValidationError) as ex:
analysis.generate_inputs(initiator, run_mode_override='V2')

self.assertEqual({'portfolio': ['"location_file" must not be null']}, ex.exception.detail)
self.assertEqual({'portfolio': ['"location_file" must not be null for run_mode = V2']}, ex.exception.detail)

self.assertEqual(Analysis.status_choices.NEW, analysis.status)
self.assertFalse(res_factory.revoke_called)
Expand Down
4 changes: 2 additions & 2 deletions src/server/oasisapi/portfolios/v1_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ def __init__(self, portfolio=None, *args, **kwargs):

def validate(self, attrs):
attrs['portfolio'] = self.portfolio
if not self.portfolio.location_file:
raise ValidationError({'portfolio': '"location_file" must not be null'})
if (not self.portfolio.location_file) and (not self.portfolio.accounts_file):
raise ValidationError({'portfolio': 'either "location_file" or "accounts_file" must not be null'})

return attrs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_portfolio_does_not_have_location_file_set___response_is_400(self):
)

self.assertEqual(400, response.status_code)
self.assertIn('"location_file" must not be null', response.json['portfolio'])
self.assertIn('either "location_file" or "accounts_file" must not be null', response.json['portfolio'])

def test_model_is_not_provided___response_is_400(self):
user = fake_user()
Expand Down
4 changes: 2 additions & 2 deletions src/server/oasisapi/portfolios/v2_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ def __init__(self, portfolio=None, *args, **kwargs):

def validate(self, attrs):
attrs['portfolio'] = self.portfolio
if not self.portfolio.location_file:
raise ValidationError({'portfolio': '"location_file" must not be null'})
if (not self.portfolio.location_file) and (not self.portfolio.accounts_file):
raise ValidationError({'portfolio': 'either "location_file" or "accounts_file" must not be null'})

# Validate and update groups parameter
validate_and_update_groups(self.partial, self.context.get('request').user, attrs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def test_portfolio_does_not_have_location_file_set___response_is_400(self):
)

self.assertEqual(400, response.status_code)
self.assertIn('"location_file" must not be null', response.json['portfolio'])
self.assertIn('either "location_file" or "accounts_file" must not be null', response.json['portfolio'])

def test_model_is_not_provided___response_is_400(self):
user = fake_user()
Expand Down

0 comments on commit a585706

Please sign in to comment.