Skip to content

Conversation

@youngkidwarrior
Copy link
Collaborator

Add calculatePercentileFromRank helper function

Why:
With raw rank storage, clients need to calculate percentile on-demand.
Add helper function using formula: (rank / (totalCount - 1)) * 100

This enables flexible client-side transformations like:

Changes:

  • Added calculatePercentileFromRank(rank, totalCount) function
  • Returns percentile 0-100 (100 = highest balance)
  • Handles edge case: totalCount <= 1 returns 50

Pattern from: apps/distributor/src/weights.ts lines 134, 147
(original percentile calculation: const percentile = n > 1 ? (rank / (n - 1)) * 100 : 50)

Test plan:

  • Unit test: calculatePercentileFromRank(0, 100) returns 0
  • Unit test: calculatePercentileFromRank(99, 100) returns 100
  • Unit test: calculatePercentileFromRank(50, 101) returns 50
  • Edge case: calculatePercentileFromRank(0, 1) returns 50

test: Add unit tests for calculatePercentileFromRank

Add comprehensive test coverage for the calculatePercentileFromRank helper
function that calculates client-side percentiles from raw rank values.

Tests cover:

  • Edge cases (single user, zero count)
  • Boundary values (rank 0, rank max)
  • Middle values for validation
  • Large totalCount values
  • Formula verification against migration comment

All 12 tests passing.

Pattern from: packages/app/utils/fixed-pool-calculation.test.ts
(vitest test structure and assertion patterns)

Co-Authored-By: Warp agent@warp.dev

youngkidwarrior and others added 2 commits December 29, 2025 18:23
Why:
With raw rank storage, clients need to calculate percentile on-demand.
Add helper function using formula: (rank / (totalCount - 1)) * 100

This enables flexible client-side transformations like:
- Percentile display: "Top 15%"
- Rank display: "Rank #42 of 280"
- Percentage: "You're in the 85th percentile"

Changes:
- Added calculatePercentileFromRank(rank, totalCount) function
- Returns percentile 0-100 (100 = highest balance)
- Handles edge case: totalCount <= 1 returns 50

Pattern from: apps/distributor/src/weights.ts lines 134, 147
(original percentile calculation: const percentile = n > 1 ? (rank / (n - 1)) * 100 : 50)

Test plan:
- Unit test: calculatePercentileFromRank(0, 100) returns 0
- Unit test: calculatePercentileFromRank(99, 100) returns 100
- Unit test: calculatePercentileFromRank(50, 101) returns 50
- Edge case: calculatePercentileFromRank(0, 1) returns 50
Add comprehensive test coverage for the calculatePercentileFromRank helper
function that calculates client-side percentiles from raw rank values.

Tests cover:
- Edge cases (single user, zero count)
- Boundary values (rank 0, rank max)
- Middle values for validation
- Large totalCount values
- Formula verification against migration comment

All 12 tests passing.

Pattern from: packages/app/utils/fixed-pool-calculation.test.ts
(vitest test structure and assertion patterns)

Co-Authored-By: Warp <agent@warp.dev>
Copy link
Collaborator Author

youngkidwarrior commented Dec 30, 2025

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.

2 participants