This repository stores rates, outages and invoicing configuration for the New England Research Cloud.
To make use of the rates and outages data, install the package and import the modules:
from nerc_rates import rates
rate_data = rates.load_from_url()
rate_data.get_value_at("CPU SU Rate", "2024-06", Decimal)from nerc_rates import outages
outage_data = outages.load_from_url()
outage_data.get_outages_during("2024-05-01", "2024-06-01", "NERC OpenStack")from nerc_rates import rates
rate_data = rates.load_from_file("rates.yaml")
rate_data.get_value_at("CPU SU Rate", "2024-06", Decimal)from nerc_rates import outages
outage_data = outages.load_from_file("outages.yaml")
outage_data.get_outages_during("2024-05-01", "2024-06-01", "NERC OpenStack")This guide explains how the system automatically verifies the integrity and correctness of rates and outages data when it is loaded. The data models are designed with built-in validation rules to catch common errors, such as overlapping date ranges, incorrect data types, or duplicate entries.
When you load data using rates.load_from_file() or outages.load_from_file(), the system attempts to parse your YAML data into its internal data models. During this process, several validation checks are performed automatically:
-
Rates Verification Checks:
- Date Range Order: Ensures that
date_untilis always afterdate_fromwithin aRateValue. - No Overlapping Date Ranges: Verifies that there are no overlapping timeframes for
RateValueentries within aRateItem'shistory. - Rate Type Consistency: Checks that
valuestrings can be correctly converted to theRateTypespecified for theRateItem(e.g., aDecimalrate is a valid number,boolrates are "true" or "false"). - Unique Rate Names: Ensures that each
RateItemhas a uniquenameacross the entire rates list.
- Date Range Order: Ensures that
-
Outages Verification Checks:
- Time Range Order: Ensures that
time_untilis always aftertime_fromwithin anOutageTimeFrames. - Unique Affected Services: Verifies that
affected_serviceswithin anOutageTimeFramesdo not contain duplicate service names. - Unique Outage URLs: Checks for duplicate
urlentries across allOutageItems in the outages list. - UTC Timezone Enforcement: Ensures all outage
datetimevalues (time_from,time_until) are provided with UTC timezone information and are not naive datetimes.
- Time Range Order: Ensures that
If any of these validation rules are violated, the loading process will raise a ValueError or TypeError, providing a clear message about what went wrong and where.
Let's say you have a rates.yaml file with an overlapping date range, which is an invalid configuration:
- name: CPU SU Rate
history:
- value: 0.013
from: 2023-06
until: 2024-06
- value: 0.15
from: 2024-05 # This date range overlaps with the previous oneWhen you try to load this data, you would see an error like this:
from nerc_rates import rates
try:
rate_data = rates.load_from_file("rates.yaml")
except ValueError as e:
print(f"Rates data validation failed: {e}")Expected Output:
Rates data validation failed: date ranges overlap
Similarly, if your outages.yaml contains an outage with a naive datetime (missing timezone information), it will trigger a validation error:
- url: http://example.com/outage-1
timeframes:
- from: 2024-01-01T12:00:00 # Missing timezone info (e.g., Z or +00:00)
until: 2024-01-01T14:00:00Z
affected_services: ["NERC OpenStack"]Loading this file would result in:
from nerc_rates import outages
try:
outage_data = outages.load_from_file("outages.yaml")
except ValueError as e:
print(f"Outages data validation failed: {e}")Expected Output:
Outages data validation failed: Naive datetime without timezone information is not allowed. Please provide timezone information (e.g., '2024-01-01T12:00:00Z' or '2024-01-01T12:00:00+00:00')
This section guides you through setting up and running the test suite for this project using pytest. Running tests is crucial for verifying that all components of the system are working as expected and that any changes introduced do not break existing functionality.
- Python 3.8+ installed
pipinstalled for dependency management
-
Install Dependencies: Navigate to the root directory of the project in your terminal and install the project dependencies.
pip install -r requirements.txt pip install -r test-requirements.txt
-
Run Tests: After installing the dependencies, you can run the test suite from the project's root directory:
pytest
This command will discover and execute all tests located in the
tests/directory.
- Specific Tests: To run a specific test file or test case, you can provide its path to
pytest:pytest tests/test_rates.py pytest tests/test_rates.py::test_get_cpu_rate
- Verbose Output: Use the
-vflag for more detailed output during test execution:pytest -v