Skip to content

Commit

Permalink
Merge pull request #591 from ucb-rit/issue_581
Browse files Browse the repository at this point in the history
Issue 581: Add service units field to project view
  • Loading branch information
matthew-li authored Oct 10, 2024
2 parents 584d0fb + a5c82a2 commit 3a3f35e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
4 changes: 4 additions & 0 deletions coldfront/core/portal/templates/portal/authorized_home.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ <h3>My {{ PROGRAM_NAME_SHORT }} Cluster Projects &raquo;</h3>
<th scope="col" class="text-nowrap">
Access to Project on Cluster
</th>
<th scope="col" class="text-nowrap">
Service Units
</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -172,6 +175,7 @@ <h3>My {{ PROGRAM_NAME_SHORT }} Cluster Projects &raquo;</h3>
<span class="badge badge-danger">No Access</span>
{% endif %}
</td>
<td>{{ project.rendered_compute_usage }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
9 changes: 8 additions & 1 deletion coldfront/core/portal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from coldfront.core.allocation.models import (Allocation,
AllocationUser,
AllocationUserAttribute)
from coldfront.core.allocation.utils import get_project_compute_resource_name
from coldfront.core.allocation.utils import (get_project_compute_allocation,
get_project_compute_resource_name)
# from coldfront.core.grant.models import Grant
from coldfront.core.portal.utils import (generate_allocations_chart_data,
generate_publication_by_year_chart_data,
Expand All @@ -20,6 +21,7 @@
from coldfront.core.project.models import Project, ProjectUserJoinRequest
from coldfront.core.project.models import ProjectUserJoinRequest
from coldfront.core.project.models import ProjectUserRemovalRequest
from coldfront.core.project.utils import render_project_compute_usage


# from coldfront.core.publication.models import Publication
Expand Down Expand Up @@ -53,6 +55,11 @@ def home(request):

resource_name = get_project_compute_resource_name(project)
project.cluster_name = resource_name.replace(' Compute', '')
try:
rendered_compute_usage = render_project_compute_usage(project)
except Exception:
rendered_compute_usage = 'Unexpected error'
project.rendered_compute_usage = rendered_compute_usage

allocation_list = Allocation.objects.filter(
Q(status__name__in=['Active', 'New', 'Renewal Requested', ]) &
Expand Down
4 changes: 4 additions & 0 deletions coldfront/core/project/templates/project/project_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ <h1>{{ PROGRAM_NAME_SHORT }} Cluster Projects</h1><hr>
<i class="fas fa-sort-down"></i>
</a>
</th>
<th scope="col" class="text-nowrap">
Service Units
</th>
</tr>
</thead>
<tbody>
Expand All @@ -139,6 +142,7 @@ <h1>{{ PROGRAM_NAME_SHORT }} Cluster Projects</h1><hr>
<td>{{ project.cluster_name|upper }}</td>
<!-- <td>{{ project.field_of_science.description }}</td>-->
<td>{{ project.status.name }}</td>
<td>{{ project.rendered_compute_usage }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
44 changes: 44 additions & 0 deletions coldfront/core/project/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal

from django.db import transaction
from flags.state import flag_enabled

Expand Down Expand Up @@ -42,6 +44,48 @@ def project_join_list_url():
return urljoin(domain, view)


def render_project_compute_usage(project):
"""Return a str containing the given Project's usage of its compute
allowance, the total allowance, and the percentage used, rounded to
two decimal places.
Return 'N/A':
- If the Project's status is not 'Active'.
- If relevant database objects cannot be retrieved. This is done
for simplicity, even though some cases would be unexpected.
Return 'Failed to compute' if calculations fail.
Returns:
- str
Raises:
- AssertionError
"""
assert isinstance(project, Project)

n_a = 'N/A'
if project.status.name != 'Active':
return n_a
try:
accounting_allocation_objects = get_accounting_allocation_objects(
project)
except Exception:
return n_a

try:
allowance = Decimal(
accounting_allocation_objects.allocation_attribute.value)
usage = Decimal(
accounting_allocation_objects.allocation_attribute_usage.value)
# Retain two decimal places.
percentage = (
Decimal(100.00) * usage / allowance).quantize(Decimal('0.00'))
except Exception:
return 'Failed to compute'
return f'{usage}/{allowance} ({percentage} %)'


def review_project_join_requests_url(project):
domain = import_from_settings('CENTER_BASE_URL')
view = reverse('project-review-join-requests', kwargs={'pk': project.pk})
Expand Down
13 changes: 11 additions & 2 deletions coldfront/core/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
ProjectUserStatusChoice,
ProjectUserRemovalRequest,
SavioProjectAllocationRequest)
from coldfront.core.project.utils import (annotate_queryset_with_cluster_name,
is_primary_cluster_project)
from coldfront.core.project.utils import annotate_queryset_with_cluster_name
from coldfront.core.project.utils import is_primary_cluster_project
from coldfront.core.project.utils import render_project_compute_usage
from coldfront.core.project.utils_.addition_utils import can_project_purchase_service_units
from coldfront.core.project.utils_.new_project_user_utils import NewProjectUserRunnerFactory
from coldfront.core.project.utils_.new_project_user_utils import NewProjectUserSource
Expand Down Expand Up @@ -443,6 +444,14 @@ def get_context_data(self, **kwargs):
context['user_agreement_signed'] = \
access_agreement_signed(self.request.user)

for project in project_list:
try:
rendered_compute_usage = render_project_compute_usage(project)
except Exception:
rendered_compute_usage = 'Unexpected error'
project.rendered_compute_usage = rendered_compute_usage
context['project_list'] = project_list

return context


Expand Down

0 comments on commit 3a3f35e

Please sign in to comment.