Skip to content

Conversation

apsantiso
Copy link
Collaborator

@apsantiso apsantiso commented Aug 20, 2025

⚠️ Important: This PR implements the calculation logic but does NOT change the response returned to users. The /usage endpoint continues to return the existing implementation.

Implements incremental usage calculation logic to track user storage usage over time. This PR adds the calculation and delta storage functionality without changing the current user-facing response, allowing us to monitor the new system's behavior before switching over.

How a request to/usage works

  1. Find last usage record: Query the latest usage record by period field (last_usage)
  2. Handle first-time users: If no usage record exists, calculate delta from user creation date to yesterday and create initial usage record, then skip to step 4
  3. Handle existing users: If last_usage exists, check if last_usage.period === currentDate - 1 day:
  • Yes: Proceed to step 4
  • No: Calculate delta from last_usage.period to currentDate - 1 day, save this delta in usage table with yesterday's date
  1. Calculate historical usage: Sum all deltas from usage table (PENDING)
  2. Calculate today's usage: Sum current day delta directly from files table (PENDING)
  3. Return total: Sum both results and return to user (PENDING)

How replacing a file works

  1. File update: The file is replaced successfully
  2. Did file size changed?: We calculate delta by comparing the old file size and the new file size
  3. Save delta if needed: If there's a change, we store delta with daily type

Why are files created and updated on the same day ignored?

These files will be included in the next delta calculation (the following day). If we also save a delta for them on the current day, it would result in double-counting and incorrect calculations.

@apsantiso apsantiso force-pushed the feat/incremental-usage-1 branch from 353ee64 to ba42a8d Compare August 20, 2025 04:31
@apsantiso apsantiso changed the title feat: add get usage incrementally calculation in background when the … [_] feat: add get usage incrementally calculation in background when the … Aug 20, 2025
@apsantiso apsantiso force-pushed the feat/incremental-usage-1 branch from a020810 to f8aa7a0 Compare August 21, 2025 02:03
@apsantiso apsantiso force-pushed the feat/incremental-usage-1 branch from f8aa7a0 to 158535e Compare August 21, 2025 02:10
@apsantiso apsantiso changed the title [_] feat: add get usage incrementally calculation in background when the … [_] feat: calculate yesterday usage and first usage on first /usage call Aug 21, 2025
@apsantiso apsantiso changed the title [_] feat: calculate yesterday usage and first usage on first /usage call [PB-2394] feat: calculate yesterday usage and first usage on first /usage call Aug 21, 2025
@apsantiso apsantiso self-assigned this Aug 21, 2025
@apsantiso apsantiso added the enhancement New feature or request label Aug 21, 2025
@apsantiso apsantiso requested a review from sg-gs August 21, 2025 02:53
@apsantiso apsantiso force-pushed the feat/incremental-usage-1 branch from 1dfd6b6 to d9dea6a Compare August 21, 2025 02:58
@apsantiso apsantiso force-pushed the feat/incremental-usage-1 branch from 1b05dc2 to 04a203a Compare August 21, 2025 05:41
Copy link

@apsantiso apsantiso merged commit 231786e into master Aug 25, 2025
12 checks passed
@apsantiso apsantiso deleted the feat/incremental-usage-1 branch August 25, 2025 14:23
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