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

feat: calculate usage incrementally #436

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

apsantiso
Copy link
Collaborator

@apsantiso apsantiso commented Nov 21, 2024

The current method of calculating usage is overloading the database. Looping through all user-created files with a status other than "deleted" is unsustainable. This PR introduces the following improvements:

  • Scheduled Procedures: Introduce two procedures that should be triggered by a cron job:

    • A daily procedure to calculate usage changes from the previous day. Takes into account files since last day 00:00:00:000 and 23:59:59:9999
    • A monthly aggregation that combines all daily changes into a single yearly record.
  • Daily Cron Job Considerations: Ensure the daily procedure accounts for files that are created and deleted on the same day, where the net change (delta) would be zero.

  • Initial Monthly Row: Insert an initial monthly row the first time a user accesses the new /usage endpoint. This row reflects the total size of all the user's existing files until the last day (so it contains all files users created since they created their account until the last day)

  • Conditional Monthly Calculations: Begin generating monthly rows via the daily cron job only after the initial calculation is complete (i.e., when the user has at least one monthly row).

  • /usage endpoint uses all the rows inside usages table and also starts calculating files created - deleted since the most recent usage period (i.e, last usage period is 2024-11-24, we calculate on the fly the usage of files since 2024-11-25 until the actual date). Still pending to take into account if the last usage is a yearly row.

@apsantiso apsantiso self-assigned this Nov 26, 2024
Comment on lines 29 to 33
LEFT JOIN (
SELECT user_id, MAX(period) AS last_period
FROM public.usages
GROUP BY user_id
) mru
Copy link
Collaborator Author

@apsantiso apsantiso Nov 26, 2024

Choose a reason for hiding this comment

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

If a user does not have a record in usage, we would like not to calculate their usage so we can reduce edge cases such as an user having their last day usage calculated but files created before this implemented being ignored.

Copy link
Member

Choose a reason for hiding this comment

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

I do not get that. If we create the first row on demand for already existing users, which could be the issue?

Comment on lines 16 to 26
let mostRecentUsage =
await this.usageRepository.getMostRecentUsage(userUuid);

if (!mostRecentUsage) {
mostRecentUsage = await this.usageRepository.addFirstDailyUsage(userUuid);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If users do not have any usage row, we create one for them using all the files created until the last day. Related to: https://github.com/internxt/drive-server-wip/pull/436/files#r1858576469

@sg-gs sg-gs added the enhancement New feature or request label Nov 26, 2024
@apsantiso apsantiso force-pushed the feat/calculate-usage-incrementally branch 6 times, most recently from 73fd5ed to 0058b03 Compare December 5, 2024 14:52
@apsantiso apsantiso force-pushed the feat/calculate-usage-incrementally branch from 0058b03 to b3100c6 Compare December 9, 2024 18:59
@apsantiso apsantiso force-pushed the feat/calculate-usage-incrementally branch from f032e0f to 2f61538 Compare December 10, 2024 05:16
@apsantiso apsantiso force-pushed the feat/calculate-usage-incrementally branch from 2f61538 to f968a38 Compare December 10, 2024 05:43
@apsantiso apsantiso marked this pull request as ready for review December 10, 2024 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready-for-preview
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants