Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit #1

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/unit-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Unit tests
on:
push:
pull_request:

jobs:
run-unit-tests:
name: Run unit tests
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
pip install -e .
pip install -r test-requirements.txt

- name: Run unit tests
run: |
pytest src/nerc_rates/tests
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
44 changes: 25 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# moc-templates

This repository serves two purposes:

- It may be used as a template repository when creating new repositories in the [CCI-MOC][] organization.
- It is the canonical location of the `LICENSE` and `CONTRIBUTING.md` files.

[cci-moc]: https://github.com/CCI-MOC/

## Contributing

We'd love to have you contribute! Please refer to our [contribution
guidelines](CONTRIBUTING.md) for details.

## License

[Apache 2.0 License](LICENSE).

The code is provided as-is with no warranties.
# NERC Rates
This repository stores rates and invoicing configuration for the New England
Research Cloud.

The values are stored in `rates.yaml` as a list with each item under the
following format. Each item in the list contains a `name` and `history`.
`history` is itself a list containing `value` (required), `from` (required),
and `until` (optional).

```yaml
- name: CPU SU Rate
history:
- value: 0.013
from: 2023-06
until: 2024-06
- value: 0.15
from: 2024-07
```

To make use of the rates, install the package and import the module
```python
from nerc_rates import load_from_url
rates = load_from_url()
rates.get_value_at("CPU SU Rate", "2024-06")
```
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build-system]
requires = [
"setuptools>=42",
"wheel"
]
build-backend = "setuptools.build_meta"
56 changes: 56 additions & 0 deletions rates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#################################################
# Service Unit Rates
#################################################
- name: CPU SU Rate
history:
- value: 0.013
from: 2023-06

- name: GPUA100 SU Rate
history:
- value: 1.803
from: 2023-06

- name: GPUA100SXM4 SU Rate
history:
- value: 2.078
from: 2023-06

- name: GPUV100 SU Rate
history:
- value: 1.214
from: 2023-06

- name: GPUK80 SU Rate
history:
- value: 0.463
from: 2023-06

- name: Storage GB Rate
history:
- value: 0.000009
from: 2023-06

#################################################
# Feature Flags
#################################################
- name: Charge for Stopped Instances
history:
- value: False
from: 2023-06
until: 2024-02
- value: True
from: 2024-03

#################################################
# SU Definitions
#################################################
- name: vCPUs in CPU SU
larsks marked this conversation as resolved.
Show resolved Hide resolved
history:
- value: 1
from: 2023-06

- name: RAM in CPU SU
history:
- value: 4096
from: 2023-06
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyyaml
requests
19 changes: 19 additions & 0 deletions setup.cfg
larsks marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[metadata]
name = nerc_rates
version = 0.1
author = MOC Alliance
author_email = contact@massopen.cloud
description = Rates and invoicing configuration for the NERC
long_description = file: README.md
classifiers =
Programming Language :: Python :: 3


[options]
package_dir =
= src
packages = find:
python_requires = >=3.8
install_requires =
pyyaml
requests
5 changes: 5 additions & 0 deletions setup.py
larsks marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python
import setuptools

if __name__ == "__main__":
setuptools.setup()
1 change: 1 addition & 0 deletions src/nerc_rates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from nerc_rates.rates import load_from_file, load_from_url
43 changes: 43 additions & 0 deletions src/nerc_rates/rates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from datetime import date, datetime

import requests
import yaml

DEFAULT_URL = "https://raw.githubusercontent.com/knikolla/nerc-rates/main/rates.yaml"
larsks marked this conversation as resolved.
Show resolved Hide resolved


class Rates:
def __init__(self, config):
self.values = {x["name"]: x for x in config}

@staticmethod
def _parse_date(d: str | date) -> date:
if isinstance(d, str):
d = datetime.strptime(d, "%Y-%m").date()
return d

def get_value_at(self, name: str, queried_date: date | str):
d = self._parse_date(queried_date)
for v_dict in self.values[name]["history"]:
v_from = self._parse_date(v_dict["from"])
v_until = self._parse_date(v_dict.get("until", d))
if v_from <= d <= v_until:
return v_dict["value"]

raise ValueError(f"No value for {name} for {queried_date}.")


def load_from_url(url=DEFAULT_URL) -> Rates:
r = requests.get(url, allow_redirects=True)
# Using the BaseLoader prevents conversion of numeric
# values to floats and loads them as strings.
config = yaml.load(r.content.decode("utf-8"), Loader=yaml.BaseLoader)
return Rates(config)


def load_from_file() -> Rates:
with open("rates.yaml", "r") as f:
# Using the BaseLoader prevents conversion of numeric
# values to floats and loads them as strings.
config = yaml.load(f, Loader=yaml.BaseLoader)
return Rates(config)
Empty file.
Loading
Loading