Skip to content

Commit

Permalink
change allocation model to choose correct quota/usage values; add mod…
Browse files Browse the repository at this point in the history
…el property to display bytes values
  • Loading branch information
claire-peters committed Nov 7, 2023
1 parent 77389e8 commit c2261b3
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 163 deletions.
3 changes: 2 additions & 1 deletion coldfront/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
path('allocation-summary', portal_views.allocation_summary, name='allocation-summary'),
path('allocation-by-fos', portal_views.allocation_by_fos, name='allocation-by-fos'),
path('user/', include('coldfront.core.user.urls')),
# path('opt_out/', portal_views.opt_out, name='opt-out'),
# path('__debug__/', include('debug_toolbar.urls')),
path('project/', include('coldfront.core.project.urls')),
path('allocation/', include('coldfront.core.allocation.urls')),
Expand Down Expand Up @@ -52,4 +53,4 @@
urlpatterns.append(path('', include('coldfront.plugins.fasrc_monitoring.urls')))

if 'django_prometheus' in settings.INSTALLED_APPS:
urlpatterns.append(path('metrics', include('coldfront.plugins.fasrc_monitoring.urls')))
urlpatterns.append(path('', include('django_prometheus.urls')))
92 changes: 71 additions & 21 deletions coldfront/core/allocation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,56 +133,102 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

def pull_allocationattribute(self, attr_name):
try:
return self.allocationattribute_set.get(
allocation_attribute_type__name=attr_name
).value
except ObjectDoesNotExist:
return None

@property
def offer_letter_code(self):
return self.pull_allocationattribute('Offer Letter Code')
return self.get_attribute('Offer Letter Code')

@property
def expense_code(self):
return self.pull_allocationattribute('Expense Code')
return self.get_attribute('Expense Code')

@property
def heavy_io(self):
return self.pull_allocationattribute('Heavy IO')
return self.get_attribute('Heavy IO')

@property
def mounted(self):
return self.pull_allocationattribute('Mounted')
return self.get_attribute('Mounted')

@property
def external_sharing(self):
return self.pull_allocationattribute('External Sharing')
return self.get_attribute('External Sharing')

@property
def high_security(self):
return self.pull_allocationattribute('High Security')
return self.get_attribute('High Security')

@property
def dua(self):
return self.pull_allocationattribute('DUA')
return self.get_attribute('DUA')

def _return_size_attr_name(self, s_type='display'):
parent_resource = self.get_parent_resource
if not parent_resource:
return None
if parent_resource.resource_type.name == 'Cluster':
size_attr_name = 'Core Usage (Hours)'
elif 'Storage' in parent_resource.resource_type.name:
if s_type == 'exact':
size_attr_name = 'Quota_In_Bytes'
elif s_type=='display':
size_attr_name = 'Storage Quota (TB)'
else:
return None
return size_attr_name

@property
def size(self):
size_attr_name = self._return_size_attr_name()
if not size_attr_name:
return None
try:
return self.allocationattribute_set.get(allocation_attribute_type_id=1).value
return float(self.get_attribute(size_attr_name))
except ObjectDoesNotExist:
if self.size_exact:
if 'TB' in self.get_parent_resource.quantity_label:
divisor = 1099511627776
return self.size_exact/divisor
return None
except TypeError:
return None


@property
def usage(self):
size_attr_name = self._return_size_attr_name()
if not size_attr_name:
return None
try:
return float(self.allocationattribute_set.get(
allocation_attribute_type__name=size_attr_name
).allocationattributeusage.value)
except ObjectDoesNotExist:
if self.usage_exact:
if 'TB' in self.get_parent_resource.quantity_label:
divisor = 1099511627776
return self.usage_exact/divisor
return None
except TypeError:
return None

@property
def size_exact(self):
size_attr_name = self._return_size_attr_name(s_type='exact')
if not size_attr_name:
return None
try:
return self.allocationattribute_set.get(
allocation_attribute_type_id=1
).allocationattributeusage.value
return self.get_attribute(size_attr_name, typed=True)
except ObjectDoesNotExist:
return None

@property
def usage_exact(self):
size_attr_name = self._return_size_attr_name(s_type='exact')
if not size_attr_name:
return None
try:
return float(self.allocationattribute_set.get(
allocation_attribute_type__name=size_attr_name
).allocationattributeusage.value)
except ObjectDoesNotExist:
return None

Expand All @@ -204,7 +250,11 @@ def cost(self):
return None
except TypeError:
return None
size = self.allocationattribute_set.get(allocation_attribute_type_id=1).value
size_attr_name = self._return_size_attr_name()
if not size_attr_name:
return None
size = self.allocationattribute_set.get(
allocation_attribute_type__name=size_attr_name).value
return 0 if not size else price * float(size)

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,18 @@ <h3><i class="fas fa-list" aria-hidden="true"></i> Allocation Information</h3>
<td>{{ allocation_users.count }}</td>
</tr>
<tr>
<th scope="row" class="text-nowrap">Total Usage (TB):</th>
<td>{{ allocation_usage_tb|floatformat:2 }}
<th scope="row" class="text-nowrap">Total Usage ({{allocation.get_parent_resource.quantity_label}}):</th>
<td>{{ allocation.usage|floatformat:2 }}
<span class="float-right">Last Synced {{user_sync_dt}}</span>
</td>
</tr>
<tr>
<th scope="row" class="text-nowrap">Quota (TB):</th>
<td id = "group_quota">{{ allocation_quota_tb|floatformat:2 }}</td>
<td id = "group_quota">{{ allocation.size|floatformat:2 }}</td>
</tr>
<tr>
<th style="background-color:#D3D3D3" bordercolor="red" scope="row" class="text-nowrap">Total Amount Due: </th>
{% cost_tb allocation_quota_tb as cost %}
{% cost_tb allocation.size as cost %}
{% if cost %}
<td style="background-color:#D3D3D3">{{ cost }}</td>
{% endif %}
Expand Down Expand Up @@ -466,10 +466,10 @@ <h3 class="d-inline"><i class="fas fa-users" aria-hidden="true"></i> Users in Al
{% else %}
<td data-sort = "{{user.usage_bytes}}" name = "usage_bytes">{{ user.usage|floatformat:1 }}{{ user.unit }}</td>
{% endif %}
{% if allocation_usage_bytes == None or allocation_usage_bytes == 0 %}
{% if allocation.usage_exact == None or allocation.usage_exact == 0 %}
<td>0</td>
{% else %}
<td>{{user.usage_bytes|div:allocation_usage_bytes|mul:100|floatformat:2 }}</td>
<td>{{user.usage_bytes|div:allocation.usage_exact|mul:100|floatformat:2 }}%</td>
{% endif %}
{% cost_bytes user.usage_bytes as cost %}
{% if cost %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,32 +125,32 @@ <h3><i class="fas fa-list" aria-hidden="true"></i> Usage Summary For: {{ allocat
<script>
const selectElement = document.getElementById('billingPeriod');
console.log(selectElement);
var price = {{ allocation_quota_bytes | div:1073741824|floatformat:2 }} * 1 / 12 * 50;
var price = {{ allocation.size_exact | div:1073741824|floatformat:2 }} * 1 / 12 * 50;
var n = price.toFixed(2);

document.getElementById("totalamount").innerHTML = n;
selectElement.addEventListener('change', (event) => {
if (event.target.value=="1month") {
var price = {{ allocation_quota_bytes | div:1073741824|floatformat:2 }} * 1 / 12 * 50;
var price = {{ allocation.size_exact | div:1073741824|floatformat:2 }} * 1 / 12 * 50;
var n = price.toFixed(2);

document.getElementById("totalamount").innerHTML = n;
}
// console.log(event.target.value);
if (event.target.value=="3month") {
var price = {{ allocation_quota_bytes | div:1073741824|floatformat:2 }} * 3 / 12 * 50;
var price = {{ allocation.size_exact | div:1073741824|floatformat:2 }} * 3 / 12 * 50;
var n = price.toFixed(2);

document.getElementById("totalamount").innerHTML = n;
}
if (event.target.value=="6month") {
var price = {{ allocation_quota_bytes | div:1073741824|floatformat:2 }} * 6 / 12 * 50;
var price = {{ allocation.size_exact | div:1073741824|floatformat:2 }} * 6 / 12 * 50;
var n = price.toFixed(2);

document.getElementById("totalamount").innerHTML = n;
}
if (event.target.value=="1year") {
var price = {{ allocation_quota_bytes | div:1073741824|floatformat:2 }} * 12 / 12 * 50;
var price = {{ allocation.size_exact | div:1073741824|floatformat:2 }} * 12 / 12 * 50;
var n = price.toFixed(2);

document.getElementById("totalamount").innerHTML = n;
Expand Down Expand Up @@ -210,13 +210,13 @@ <h3 class="d-inline"><i class="fas fa-users" aria-hidden="true"></i> Users in Al
<!-- <td>{{ user.usage}}</td> -->

<td data-sort = "{{user.usage}}">{{ user.usage }}</td>
<td>{{user.usage|div:1024|div:allocation_usage_bytes|mul:100|floatformat:2}}</td>
<td>{{4.167|mul:allocation_quota_bytes|div:1073741824|mul:user.usage|div:1024|div:allocation_usage_bytes|floatformat:2}}</td>
<td>{{user.usage|div:1024|div:allocation.usage_exact|mul:100|floatformat:2}}</td>
<td>{{4.167|mul:allocation.size_exact|div:1073741824|mul:user.usage|div:1024|div:allocation.usage_exact|floatformat:2}}</td>

{% comment %} <td>{{user.usage|mul:100 | div:allocation_usage_bytes|floatformat:2}}</td>
<td>{{user.usage|mul:100 | div:allocation_usage_bytes|floatformat:2|div:100|mul:allocation_quota_bytes|mul:50|div:12|floatformat:2}}</td> {% endcomment %}
{% comment %} <td>{{user.usage|mul:100 | div:allocation.usage_exact|floatformat:2}}</td>
<td>{{user.usage|mul:100 | div:allocation.usage_exact|floatformat:2|div:100|mul:allocation.size_exact|mul:50|div:12|floatformat:2}}</td> {% endcomment %}

<!-- <td>{{user.usage|mul:100 | div:allocation_usage_bytes|floatformat:2|mul:50|mul:50| div:12|floatformat:2}}</td> -->
<!-- <td>{{user.usage|mul:100 | div:allocation.usage_exact|floatformat:2|mul:50|mul:50| div:12|floatformat:2}}</td> -->

</tr>

Expand Down
8 changes: 4 additions & 4 deletions coldfront/core/allocation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ def test_allocation_detail_template_value_render(self):
self.client.force_login(self.admin_user, backend=BACKEND)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
# check that allocation_quota_tb has value
self.assertEqual(response.context['allocation_quota_bytes'], 109951162777600)
# check that allocation_usage_tb has value
self.assertEqual(response.context['allocation_usage_bytes'], 10995116277760)
# check that allocation.size_exact has value
self.assertEqual(response.context['allocation'].size_exact, 109951162777600)
# check that allocation.usage_exact has value
self.assertEqual(response.context['allocation'].usage_exact, 10995116277760)

def test_allocationdetail_requestchange_button(self):
"""Test visibility of "Request Change" button for different user types"""
Expand Down
Loading

0 comments on commit c2261b3

Please sign in to comment.