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

Allow setting default and individual SU budgets for project members #437

Merged
merged 3 commits into from
Mar 18, 2024

Conversation

AnishReddyRavula
Copy link
Contributor

@AnishReddyRavula AnishReddyRavula commented Mar 8, 2024

This commit introduces the functionality to set default and individual SU budgets for project members. It includes the following changes:

  • Added models ChargeBudget to track individual SU budgets for users in projects.
  • Added default_su_budget to Project model to enforce the budget for all other members
  • Implemented set_budget_for_user_in_project function to set SU budgets for individual users in projects.
  • Added form inputs to allow setting default SU budgets for all non-manager users in a project.
  • Modified the view_project template to display current SU usage and budget inputs for project members.
  • Implemented backend logic to handle setting default and individual SU budgets in the view_project view.

These changes enable project managers to manage SU budgets more effectively within projects.

Docs PR

This commit introduces the functionality to set default and individual SU budgets for project members. It includes the following changes:

- Added models ChargeBudget to track individual SU budgets for users in projects.
- Implemented set_budget_for_user_in_project function to set SU budgets for individual users in projects.
- Added form inputs to allow setting default SU budgets for all non-manager users in a project.
- Modified the view_project template to display current SU usage and budget inputs for project members.
- Implemented backend logic to handle setting default and individual SU budgets in the view_project view.

These changes enable project managers to manage SU budgets more effectively within projects.
Copy link
Contributor

@Mark-Powers Mark-Powers left a comment

Choose a reason for hiding this comment

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

I have a few concerns about this implementation:

  1. I think enforced_by should be removed, as I don't think it's ever exposed to users or would be particularly useful.
  2. Projects should probably have a "default SU budget" field, and inside _check_usage_against_user_budget , if there is no ChargeBudget, it should instead use the project's default SU budget.

allocations/models.py Outdated Show resolved Hide resolved
def _check_usage_against_user_budget(self, user, allocation, new_charge):
user_budget = ChargeBudget.objects.get(user=user, project=allocation.project)
left = user_budget.su_left()
if left - new_charge < 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if left < new_charge would be more clear.

<p class="su-budget-display" style="display: inline-block">{{ u.su_budget }}</p>
</div>
<div class="col-xs-5 col-sm-5 col-lg-5">
<input
Copy link
Contributor

Choose a reason for hiding this comment

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

How usable is this? It seems like it would be tricky to precisely set the slider. Also steps of 1000 is a lot, since our allocations only have 20000 SUs, and a class might have more than 20 people.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe 500 would be sufficient step.

projects/views.py Show resolved Hide resolved
projects/views.py Show resolved Hide resolved
@AnishReddyRavula
Copy link
Contributor Author

I have a few concerns about this implementation:

  1. I think enforced_by should be removed, as I don't think it's ever exposed to users or would be particularly useful.
  2. Projects should probably have a "default SU budget" field, and inside _check_usage_against_user_budget , if there is no ChargeBudget, it should instead use the project's default SU budget.

Nice. These are very useful comments. Thanks. I will work on it

from django.conf import settings
from django.db import models
from django.db.models import ExpressionWrapper, F, Sum, functions
from django.core.validators import MaxValueValidator, MinValueValidator
Copy link
Contributor

Choose a reason for hiding this comment

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

[flake8] <401> reported by reviewdog 🐶
'django.core.validators.MaxValueValidator' imported but unused

@@ -12,6 +12,7 @@
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.contrib.sites.models import Site
from django.core.validators import MaxValueValidator, MinValueValidator
Copy link
Contributor

Choose a reason for hiding this comment

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

[flake8] <401> reported by reviewdog 🐶
'django.core.validators.MaxValueValidator' imported but unused

Copy link
Contributor

@Mark-Powers Mark-Powers left a comment

Choose a reason for hiding this comment

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

A few minor comments to make things more consistent.

This needs a docs PR too.

role, scopes = keycloak_client.get_user_project_role_scopes(
lease_eval.user.username, lease_eval.project.charge_code
)
if role == 'member':
Copy link
Contributor

Choose a reason for hiding this comment

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

good idea

elif "default_su_budget" in request.POST:
portal_project.default_su_budget = request.POST["default_su_budget"]
portal_project.save()
for u in users:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think for consistency, this should be removed to the balance service check. That way, all of the chargebudget objects are created in the same place, when the user first tries to use SUs.

else:
su_budget_value = su_budget.su_budget
user["su_budget"] = int(su_budget_value)
user["su_used"] = ChargeBudget(
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a little confusing that this creates a ChargeBudget object, since it's not actually getting it from the database? Insteade maybe current_usage should be a static/util method somewhere?

Refactors the charge calculation logic in the allocations/models.py
file for clarity. It introduces a utility function in balance_service/utils/su_calculators.py
to calculate total SU usage for users in projects.

Additionally, project budget enforcement is implemented.
When adding users to projects in projects/membership.py, their budgets are updated to match the project's default budget.
The projects/views.py file is updated to delete user budgets if they are assigned the manager role to ensure budget enforcement.
Copy link
Contributor

@Mark-Powers Mark-Powers left a comment

Choose a reason for hiding this comment

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

Looks good!

@AnishReddyRavula AnishReddyRavula merged commit f3ad018 into master Mar 18, 2024
10 of 12 checks passed
@AnishReddyRavula AnishReddyRavula deleted the user_su_budgeting branch March 18, 2024 19:11
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.

2 participants