Add AllocationDailyBillableUsage model and improve tests#317
Add AllocationDailyBillableUsage model and improve tests#317jimmysway wants to merge 25 commits into
Conversation
…mmand Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
…mpty usage, remove duplicate print Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
…odel Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
…Model from the start Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Co-authored-by: knikolla <4123322+knikolla@users.noreply.github.com>
Added tests to validate ORM layer and fetching layer that interacts with ORM
…ueries and standardizing string formatting in models and tests.
…nfo-database-table
reconfigured imports
01b3089 to
8781083
Compare
|
|
||
| def ready(self): | ||
| import coldfront_plugin_cloud.signals # noqa: F401 | ||
| import coldfront_plugin_cloud.models.daily_billable_usage # noqa: F401 |
There was a problem hiding this comment.
why is this being imported here?
There was a problem hiding this comment.
I don't really understand everything here but I stumbled across this article when trying to figure out why .signals was being imported which led me to this article in the django documentation
https://docs.djangoproject.com/en/6.0/ref/applications/#how-applications-are-loaded
Where it says
Then Django attempts to import the models submodule of each application, if there is one.
You must define or import all models in your application’s models.py or models/init.py. Otherwise, the application registry may not be fully populated at this point, which could cause the ORM to malfunction.
Once this stage completes, APIs that operate on models such as get_model() become usable.
We have historically left the __init__.py blank and I don't know if this daily_billable_usage model is fundamentally different than the other ones because it is ORM related? I might need some help understanding this
There was a problem hiding this comment.
Would adding the import in models/__init__.py instead of here work?
knikolla
left a comment
There was a problem hiding this comment.
Did a first pass at reviewing timeboxed to 25mins. Overall looks in the right direction. Please instead of creating a new command enhance the testing code to create the testing setup you need.
| return f"{today.year}-{today.month:02d}" | ||
|
|
||
|
|
||
| class Command(BaseCommand): |
There was a problem hiding this comment.
Please don't create a new command for this. Instead look into either mocking the tests so that the values that you need are returned from the fictional CSV or sample values inserted into the DB.
There was a problem hiding this comment.
Just to clarify, this seeding logic is primarily for testing of the downstream graphical UI elements in coldfront-nerc. I will find another way to insert sample values directly into the DB
|
|
||
| def ready(self): | ||
| import coldfront_plugin_cloud.signals # noqa: F401 | ||
| import coldfront_plugin_cloud.models.daily_billable_usage # noqa: F401 |
There was a problem hiding this comment.
Would adding the import in models/__init__.py instead of here work?
There was a problem hiding this comment.
Pull request overview
Adds persistent storage and retrieval for per-allocation daily billable usage by introducing an AllocationDailyBillableUsage model/table, wiring the existing fetch_daily_billable_usage command to upsert rows (and support deleting a day’s rows via --remove), and expanding test coverage plus a local-dev seeding command.
Changes:
- Introduce
AllocationDailyBillableUsagemodel + initial migration to store daily usage per (allocation, date, SU type). - Extend
fetch_daily_billable_usageto write daily usage rows to DB and add--removeto delete rows for a given date. - Add DB-backed read helpers (
billable_usage.py), aseed_daily_billable_usagecommand, and new/expanded unit tests.
Reviewed changes
Copilot reviewed 9 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/coldfront_plugin_cloud/tests/unit/test_fetch_daily_billable_usage.py | Adds tests covering DB insert/update behavior and the new --remove path. |
| src/coldfront_plugin_cloud/tests/unit/test_daily_billable_usage.py | New unit tests for DB read helpers and the new model’s constraints/behavior. |
| src/coldfront_plugin_cloud/models/daily_billable_usage.py | Adds the AllocationDailyBillableUsage Django model (FK to Allocation, date, su_type, value). |
| src/coldfront_plugin_cloud/migrations/0001_initial.py | Creates the new table, indexes, and unique constraint. |
| src/coldfront_plugin_cloud/migrations/init.py | Migration package marker. |
| src/coldfront_plugin_cloud/management/commands/seed_daily_billable_usage.py | New command to seed daily usage rows for local development/testing. |
| src/coldfront_plugin_cloud/management/commands/fetch_daily_billable_usage.py | Persists fetched usage to DB and adds --remove option to delete a date’s rows. |
| src/coldfront_plugin_cloud/billable_usage.py | New helper APIs to read daily usage (single day and date range) from the DB. |
| src/coldfront_plugin_cloud/apps.py | Imports the new model module during app initialization. |
| setup.cfg | Adds django-model-utils dependency for TimeStampedModel. |
| .gitignore | Ignores *.db files (likely local sqlite DB artifacts). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| time.sleep(0.02) | ||
| row.value = Decimal("150.00") | ||
| row.save() | ||
| row.refresh_from_db() |
| for su_type, amount in base.items(): | ||
| step = _RAMP_STEP.get(su_type, 0) | ||
| ramped[su_type] = str(float(amount) + day_index * step) | ||
| return usage_models.UsageInfo(ramped) |
| Raises: | ||
| TypeError: If rows is None. | ||
| ValueError: If a row has an empty su_type. |
|
I will go over all the comments but I also have one question right now there is a I find it confusing sometimes for which is which do you think I should rename these to distinguish between them better or is it fine. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Closes #292