Skip to content

feat: aggregate user retention cohorts in set queries - #1726

Merged
RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
shogun444:feat/user-retention-aggregate-queries
Sep 28, 2026
Merged

RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
shogun444:feat/user-retention-aggregate-queries

Conversation

@shogun444

Copy link
Copy Markdown
Contributor

Description

Refactors KpiService.calculateUserRetention() to replace nested per-cohort queries and unbounded IN (:...cohortUserIds) clauses with set-based grouped queries, preventing database strain and scaling bottlenecks.

Closes #1535

Changes

  • Set-Based Aggregation (src/utils/masking/kpi.service.ts):
    • Replaced userRepository.find() and per-cohort nested loops with 2 grouped aggregate queries:
      1. Grouped cohort counts across precomputed cohort windows.
      2. Inner-joined user retention events grouped by (cohort_month, retained_month).
    • Maintained exact date boundaries using JS parameters to prevent calendar vs rolling 30-day discrepancies.
    • Preserved existing Prometheus gauge labels (cohort_month, retained_month) and values.
  • Unit Tests (src/utils/masking/kpi.service.spec.ts):
    • Updated mocks to cover createQueryBuilder aggregate chains (innerJoin, groupBy, getRawMany).
    • Verified retention rates and confirmed the removal of IN (:...cohortUserIds) calls.

Acceptance Criteria Checklist

  • Retention computed without per-cohort nested query loops or unbounded IN-lists
  • Reported retention values remain identical
  • Unit tests updated and CI passing
  • Starred the repository

@drips-wave

drips-wave Bot commented Sep 28, 2026

Copy link
Copy Markdown

@shogun444 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER
RUKAYAT-CODER merged commit f0998ec into rinafcode:main Sep 28, 2026
3 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.

KPI user-retention calculation runs nested per-cohort queries

2 participants