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

Support excluding time intervals during storage invoicing and fixed typo #174

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

QuanMPhm
Copy link
Contributor

@QuanMPhm QuanMPhm commented Aug 5, 2024

Closes #166. Added an optional CLI argument which allows passing in a txt file containing excluded time intervals. Each line in the txt file should contain a date range that we want to exclude from billing, formatted as
"%Y-%m-%d,%Y-%m-%d". An example line could be "2023-02-01,2023-02-03".

The time period excluded from billing starts from the first second of the start date, and ends on the last second of the end date. This means it is possible for the excluded date range to start and end on the same day.

Also fixed a minor typo.

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.

Wouldn't a simpler implementation be to calculate the several billable periods and then call calculate_quota_unit_hours multiple times adding up the result?


EDIT: Nevermind the above comment. I wrote it and then forgot to delete it.

src/coldfront_plugin_cloud/utils.py Outdated Show resolved Hide resolved
@knikolla
Copy link
Collaborator

knikolla commented Aug 5, 2024

Wouldn't a simpler implementation be to calculate the several billable periods and then call calculate_quota_unit_hours multiple times adding up the result?

Nevermind the above comment. I wrote it and then forgot to delete it.

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.

Two minor style comments that can be quickly resolved but otherwise looks good! Thanks!

[
datetime.datetime.strptime(start, "%Y-%m-%d"),
datetime.datetime.strptime(end, "%Y-%m-%d").replace(
hour=23, minute=59, second=59)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Apologies, I should have caught this in earlier iterations but it only really hit me yesterday evening.

This is inconsistent with how we are treating dates in the rest of this script.

Running the application with --start 2024-01-01 and --end 2024-01-02 will produce a 24 interval (from midnight to midnight), however an --excluded-date-ranges 2024-01-01,2024-01-02 would be a 48 hour interval.

Regardless of which is the better way, please treat dates as meaning 00:00 on that date to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Sorry for not realizing this design choice as well when implementing the feature.

Copy link
Contributor

@naved001 naved001 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, just couple of optional suggestions.

e_interval_end = _clamp_time(e_interval_end, start, end)
total_interval_duration -= (e_interval_end - e_interval_start).total_seconds()

return math.ceil(total_interval_duration)
Copy link
Contributor

@naved001 naved001 Aug 30, 2024

Choose a reason for hiding this comment

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

@knikolla it's possible that total_interval_duration could become negative if the user inadvertently supplies multiple overlapping intervals to exclude. That's obviously user error but it'd still be nice to add an assertion that total_interval_duration >=0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@naved001 I have solved a medium Leetcode problem and added a function that will merge overlapping excluded time intervals

src/coldfront_plugin_cloud/utils.py Show resolved Hide resolved
@joachimweyl
Copy link

@QuanMPhm have you had a chance to review Naved's suggestions?

@QuanMPhm
Copy link
Contributor Author

QuanMPhm commented Sep 6, 2024

@naved001 @knikolla I've added some small logic additions to the PR given Naved's feedback, so I will wait for everyone's approval again.

time = max_time
return time

def _merge_date_ranges(intervals_list: list):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I appreciate your enthusiasm in writing code to support this, but this then means that accepting overlapping intervals as an argument is a supported feature of the codebase, which we don't want to be the case.

Some explanations as to why:

  • The admin running the command will not be incentivized to fix the way they are storing these intervals.
  • It may also be the case that some other tools we're using do not implement these merging function, therefore using the same interval on OpenStack billing for example will return an error, but not here.
  • This is a complex function that we'd have to keep supporting moving forward in cases of bugs.

Please simply check for overlapping intervals or unordered start and end and error in case of malformed input.

Copy link
Contributor Author

@QuanMPhm QuanMPhm Sep 13, 2024

Choose a reason for hiding this comment

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

@knikolla Sorry for taking a while to ask this: Since removing support for merging intervals means overlapping intervals will result in faulty billing, I should throw an error for overlapping intervals as well?

I've removed the merging function, and perform assertions for overlaps and unordered start-end intervals.

Added an optional CLI argument which allows passing in a txt file
containing excluded time intervals. Each line in the txt file should
contain a date range that we want to exclude from billing, formatted as
 "%Y-%m-%d,%Y-%m-%d". An example line could be "2023-02-01,2023-02-03".

The time period excluded from billing starts from the first second of
the start date, and ends on the first second of the end date.
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.

Implement support for excluding time intervals during an invoicing period
4 participants