Skip to content

Conversation

apsantiso
Copy link
Collaborator

@apsantiso apsantiso commented Aug 21, 2025

⚠️ We're not going to merge this yet (August 27)

Branch off: #668

The original branch just started saving the file sizes delta in the usages table. This PR implements the aggregation of all the records in usages to calculate the current usage.

The target branch is set to feat/incremental-usage-1 for the time being just to show the differences between the branches, but this PR should point to master when feat/incremental-usage-1 gets merged.

You can check the performance of the aggregation, here it is the raw query:

WITH yearly_years AS (
    SELECT DISTINCT date_trunc('year', period) AS year
    FROM public.usages
    WHERE type = 'yearly' AND user_id = :userUuid
),
aggregated_data AS (
    -- Use yearly data where it exists
    SELECT
        date_trunc('year', period) AS year,
        SUM(delta) AS total_delta
    FROM public.usages
    WHERE type = 'yearly' AND user_id = :userUuid
    GROUP BY date_trunc('year', period)
    
    UNION ALL
    
    -- Use monthly + daily data for years without yearly data
    SELECT
        date_trunc('year', period) AS year,
        SUM(delta) AS total_delta
    FROM public.usages
    WHERE type IN ('monthly', 'daily')
      AND user_id = :userUuid
      AND date_trunc('year', period) NOT IN (SELECT year FROM yearly_years)
    GROUP BY date_trunc('year', period)
)
SELECT
    SUM(CASE WHEN year < date_trunc('year', CURRENT_DATE) THEN total_delta ELSE 0 END) AS historical_total,
    SUM(CASE WHEN year = date_trunc('year', CURRENT_DATE) THEN total_delta ELSE 0 END) AS current_year_total
FROM aggregated_data;

@apsantiso apsantiso changed the title Feat/incremental usage 2 [_] feat: switch to incrementally calculated usage Aug 21, 2025
@apsantiso apsantiso changed the base branch from master to feat/incremental-usage-1 August 21, 2025 14:55
@apsantiso apsantiso self-assigned this Aug 21, 2025
@apsantiso apsantiso force-pushed the feat/incremental-usage-2 branch from fa4c786 to 2490bae Compare August 21, 2025 22:27
@apsantiso apsantiso force-pushed the feat/incremental-usage-2 branch from 2490bae to 154069f Compare August 21, 2025 23:20
@apsantiso apsantiso added the enhancement New feature or request label Aug 21, 2025
@apsantiso apsantiso marked this pull request as ready for review August 21, 2025 23:28
@apsantiso apsantiso changed the title [_] feat: switch to incrementally calculated usage [PB-2394] feat: switch to incrementally calculated usage Aug 21, 2025
@apsantiso apsantiso requested a review from sg-gs August 21, 2025 23:41
WHERE
type = 'yearly'
AND user_id = :userUuid
GROUP BY
Copy link
Member

Choose a reason for hiding this comment

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

Why are you grouping by year if it is a yearly record? The record itself is an aggregation. Am I missing something?

type = 'monthly'
AND user_id = :userUuid
GROUP BY
date_trunc('year', period), date_trunc('month', period)
Copy link
Member

Choose a reason for hiding this comment

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

Same question here

type = 'monthly'
AND user_id = :userUuid
GROUP BY
date_trunc('year', period), date_trunc('month', period)
Copy link
Member

Choose a reason for hiding this comment

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

Same question here

type = 'daily'
AND user_id = :userUuid
GROUP BY
date_trunc('year', period), date_trunc('month', period)
Copy link
Member

Choose a reason for hiding this comment

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

And here, regarding to the year

Base automatically changed from feat/incremental-usage-1 to master August 25, 2025 14:23
@apsantiso apsantiso force-pushed the feat/incremental-usage-2 branch from 57684dc to c843619 Compare August 27, 2025 05:05
Copy link

@apsantiso apsantiso requested a review from sg-gs August 27, 2025 05:11
Copy link

This PR is stale because it has been open for more than 15 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants