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

Added rounding to Cost and Rate for storage invoice #161

Merged
merged 1 commit into from
May 31, 2024

Conversation

QuanMPhm
Copy link
Contributor

@QuanMPhm QuanMPhm commented May 15, 2024

Closes #157, Cost will now be rounded to 2 decimal points
Rate will be rounded to 6 decimal points, since the Openshift Gb/hr rates at the time of this commit is $0.000009

@QuanMPhm QuanMPhm requested a review from knikolla May 15, 2024 20:27
@knikolla knikolla requested a review from naved001 May 28, 2024 20:48
Copy link
Collaborator

@knikolla knikolla left a comment

Choose a reason for hiding this comment

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

Since rate is already a Decimal I don't think this changes the representation format and therefore it wouldn't solve issue #157 which is the purpose of this? Have you tested this @QuanMPhm

@knikolla
Copy link
Collaborator

Did some quick googling. The format method with the f flag on a Decimal seems to give us what we want.

>>> a
Decimal('9E-9')
>>> str(a)
'9E-9'
>>> format(a, 'f')
'0.000000009'

Cost will now be rounded to 2 decimal points
Rate will be rounded to 6 decimal points, since the Openshift Gb/hr rates at the
time of this commit is $0.000009
Copy link
Collaborator

@knikolla knikolla left a comment

Choose a reason for hiding this comment

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

You have fixed the rounding but you haven't fixed the way rate is written to file.

@QuanMPhm
Copy link
Contributor Author

@knikolla @naved001 Sorry, I'll do some testing and get back to this soon.

@QuanMPhm
Copy link
Contributor Author

@knikolla @naved001 I reverted the change to rates, so now an InvoiceRow is initialized as such:

row = InvoiceRow(
        InvoiceMonth=options['invoice_month'],
        Project_Name=allocation.get_attribute(attributes.ALLOCATION_PROJECT_NAME),
        Project_ID=allocation.get_attribute(attributes.ALLOCATION_PROJECT_ID),
        PI=allocation.project.pi.email,
        Institution_Specific_Code=allocation.get_attribute(
            attributes.ALLOCATION_INSTITUTION_SPECIFIC_CODE
        ) or "N/A",
        Invoice_Type_Hours=time,
        Invoice_Type=su_name,
        Rate=rate,    # Not casting to Decimal
        Cost=(time * rate).quantize(Decimal('.01'), rounding=ROUND_HALF_UP)
)

When I run the script with this command:
coldfront calculate_storage_gb_hours --openstack-gb-rate 0.000009 --openshift-gb-rate 0.000009

My output invoice seems to be the desired output?

Invoice Month,Project - Allocation,Project - Allocation ID,Manager (PI),Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost
2024-05,,,cgray@example.com,,,,N/A,1234,OpenStack Storage,0.000009,0.01

@knikolla
Copy link
Collaborator

@knikolla @naved001 I reverted the change to rates, so now an InvoiceRow is initialized as such:

row = InvoiceRow(
        InvoiceMonth=options['invoice_month'],
        Project_Name=allocation.get_attribute(attributes.ALLOCATION_PROJECT_NAME),
        Project_ID=allocation.get_attribute(attributes.ALLOCATION_PROJECT_ID),
        PI=allocation.project.pi.email,
        Institution_Specific_Code=allocation.get_attribute(
            attributes.ALLOCATION_INSTITUTION_SPECIFIC_CODE
        ) or "N/A",
        Invoice_Type_Hours=time,
        Invoice_Type=su_name,
        Rate=rate,    # Not casting to Decimal
        Cost=(time * rate).quantize(Decimal('.01'), rounding=ROUND_HALF_UP)
)

When I run the script with this command: coldfront calculate_storage_gb_hours --openstack-gb-rate 0.000009 --openshift-gb-rate 0.000009

My output invoice seems to be the desired output?

Invoice Month,Project - Allocation,Project - Allocation ID,Manager (PI),Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost
2024-05,,,cgray@example.com,,,,N/A,1234,OpenStack Storage,0.000009,0.01

I see, then perhaps the bug is with how process_csv_report writes down the Rate when it writes the invoices.

If you go to Invoices/2024-04/ and look at any of the invoices there produced by process_csv_report the rate for Storage is written as 9.00E-06

@knikolla
Copy link
Collaborator

knikolla commented May 31, 2024

@QuanMPhm It seems to be that the issue is not with how it is written in this file but in process_csv_repoirt.

Rate in process_csv_report is read as a float and therefore when it's to file it is persisted like that.

>>> a = "0.000009"
>>> float(a)
9e-06
>>> Decimal(a)
Decimal('0.000009')

@knikolla knikolla merged commit 6a2d53a into nerc-project:main May 31, 2024
4 checks passed
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.

Output 0.000009 instead of 9.00E-06 in invoicing and write cost with 2 decimal points
3 participants