Skip to content

Add AllocationDailyBillableUsage model and improve tests#317

Open
jimmysway wants to merge 25 commits into
nerc-project:mainfrom
jimmysway:copilot/add-usage-info-database-table
Open

Add AllocationDailyBillableUsage model and improve tests#317
jimmysway wants to merge 25 commits into
nerc-project:mainfrom
jimmysway:copilot/add-usage-info-database-table

Conversation

@jimmysway

Copy link
Copy Markdown
Contributor

Closes #292

Copilot AI and others added 21 commits February 10, 2026 20:05
…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.
reconfigured imports
@jimmysway jimmysway force-pushed the copilot/add-usage-info-database-table branch from 01b3089 to 8781083 Compare May 28, 2026 20:07

def ready(self):
import coldfront_plugin_cloud.signals # noqa: F401
import coldfront_plugin_cloud.models.daily_billable_usage # noqa: F401

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this being imported here?

@jimmysway jimmysway Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would adding the import in models/__init__.py instead of here work?

@knikolla knikolla left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jimmysway jimmysway Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would adding the import in models/__init__.py instead of here work?

@knikolla knikolla requested a review from Copilot June 4, 2026 20:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AllocationDailyBillableUsage model + initial migration to store daily usage per (allocation, date, SU type).
  • Extend fetch_daily_billable_usage to write daily usage rows to DB and add --remove to delete rows for a given date.
  • Add DB-backed read helpers (billable_usage.py), a seed_daily_billable_usage command, 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.

Comment thread src/coldfront_plugin_cloud/tests/unit/test_daily_billable_usage.py Outdated
Comment on lines +310 to +313
time.sleep(0.02)
row.value = Decimal("150.00")
row.save()
row.refresh_from_db()
Comment on lines +40 to +43
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)
Comment on lines +21 to +23
Raises:
TypeError: If rows is None.
ValueError: If a row has an empty su_type.
@jimmysway

jimmysway commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

I will go over all the comments but I also have one question right now there is a management/commands/fetch_daily_billable_usage.py which is the fetching code for S3 and also my newly written models/daily_billable_usage.py which is fetching from the database.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create new table AllocationDailyBillableUsage

4 participants