fix(metrics): correct merged PR count and merge rate calculation#340
Merged
Priyanshu-byte-coder merged 1 commit intoMay 19, 2026
Conversation
Two bugs in fetchPRMetrics() caused incorrect dashboard data for all users. Bug 1 - Wrong merged count: The GitHub Search API returns state "closed" for both merged PRs and PRs that were closed without merging (rejected, abandoned, spam). The old code counted all closed PRs as merged. Fixed by checking pull_request.merged_at, which is non-null only for actually merged PRs. Bug 2 - Wrong merge rate denominator: mergeRate was computed as merged / data.total_count where total_count is the user's all-time PR count (potentially thousands) while merged was derived from data.items, capped at 100 results. This produced a near-zero rate for any active user regardless of their actual merge history. Fixed by using data.items.length (the fetched sample size) as the denominator. Also tightened avgReviewHours to measure open-to-merge time using merged_at instead of closed_at, which excluded unmerged PRs implicitly but used the wrong timestamp for the end of the review window.
|
@anshul23102 is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel. A member of the Team first needs to authorize it. |
Priyanshu-byte-coder
approved these changes
May 19, 2026
Owner
Priyanshu-byte-coder
left a comment
There was a problem hiding this comment.
Correct. state: 'closed' in the GitHub Search API includes both merged and rejected PRs — checking pull_request.merged_at != null correctly distinguishes them. Using sample size instead of total_count for merge rate is the right fix — total_count is all-time and makes the rate near-zero for active users.
1d63eb1
into
Priyanshu-byte-coder:main
7 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #339
What was wrong
Two bugs in `fetchPRMetrics()` in `src/app/api/metrics/prs/route.ts` caused incorrect data for all users.
Bug 1 — Wrong merged count:
The GitHub Search API returns `state: "closed"` for both merged PRs and PRs closed without merging. The old code counted all closed PRs as merged. Fixed by checking `pull_request.merged_at`, which is non-null only for actually merged PRs.
Bug 2 — Wrong merge rate denominator:
`mergeRate` was computed as `merged / data.total_count` where `total_count` is the user's all-time PR count (potentially thousands) while `merged` was derived from `data.items`, capped at 100. This produced a near-zero rate for any active user. Fixed by using `data.items.length` (the fetched sample size) as the denominator.
Bug 3 — avgReviewHours used wrong end timestamp:
Average review time now uses `pull_request.merged_at` instead of `closed_at` and only counts actually merged PRs, giving a meaningful time-to-merge metric.
Files changed
Type of change