-
Notifications
You must be signed in to change notification settings - Fork 729
feat: extend enricher to cover extra fields [CM-1220] #4186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbani01
wants to merge
3
commits into
main
Choose a base branch
from
feat/extend_repo_enricher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
backend/src/osspckgs/migrations/V1780996561__repo_activity_snapshot.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| ALTER TABLE repos ADD COLUMN IF NOT EXISTS security_policy_enabled boolean; | ||
| ALTER TABLE repos ADD COLUMN IF NOT EXISTS security_file_enabled boolean; | ||
| ALTER TABLE repos ADD COLUMN IF NOT EXISTS snapshot_at timestamptz; | ||
|
mbani01 marked this conversation as resolved.
|
||
|
|
||
| CREATE TABLE IF NOT EXISTS repo_activity_snapshot ( | ||
| repo_id bigint PRIMARY KEY REFERENCES repos(id) ON DELETE CASCADE, | ||
| snapshot_at timestamptz NOT NULL, | ||
| window_months int NOT NULL DEFAULT 12, | ||
| -- commit activity | ||
| commits_last_12m int, | ||
| commits_last_6m int, | ||
| commits_prior_6m int, | ||
| -- PR health | ||
| prs_opened_last_12m int, | ||
| prs_merged_last_12m int, | ||
| prs_closed_unmerged_12m int, | ||
| pr_median_time_to_merge_hours int, | ||
| pr_median_time_to_first_response_hours int, | ||
| -- issue health | ||
| issues_opened_last_12m int, | ||
| issues_closed_last_12m int, | ||
| issues_opened_last_6m int, | ||
| issues_opened_prior_6m int, | ||
| issues_open_now int, | ||
| issue_median_time_to_close_hours int, | ||
| issue_median_time_to_first_response_hours int | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS repo_activity_snapshot_snapshot_at_idx | ||
| ON repo_activity_snapshot (snapshot_at); | ||
105 changes: 105 additions & 0 deletions
105
services/apps/packages_worker/src/enricher/computeMedians.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| const MS_PER_HOUR = 1000 * 60 * 60 | ||
|
|
||
| function median(values: number[]): number | null { | ||
| if (values.length === 0) return null | ||
| const sorted = [...values].sort((a, b) => a - b) | ||
| const middleIndex = Math.floor(sorted.length / 2) | ||
| return sorted.length % 2 === 0 | ||
| ? (sorted[middleIndex - 1] + sorted[middleIndex]) / 2 | ||
| : sorted[middleIndex] | ||
| } | ||
|
|
||
| function hoursBetween(startDate: string, endDate: string): number { | ||
| return (new Date(endDate).getTime() - new Date(startDate).getTime()) / MS_PER_HOUR | ||
| } | ||
|
|
||
| function toIntHours(hours: number | null): number | null { | ||
| return hours != null ? Math.round(hours) : null | ||
| } | ||
|
|
||
| // Shapes mirror exactly what GitHub GraphQL returns for comments/reviews nodes | ||
| export interface ResponseNode { | ||
| createdAt: string | ||
| author: { login: string } | null | ||
| } | ||
|
|
||
| export interface PrNode { | ||
| createdAt: string | ||
| mergedAt: string | null | ||
| author: { login: string } | null | ||
| comments: { nodes: ResponseNode[] } | ||
| reviews: { nodes: ResponseNode[] } | ||
| } | ||
|
|
||
| export interface IssueNode { | ||
| createdAt: string | ||
| closedAt: string | null | ||
| author: { login: string } | null | ||
| comments: { nodes: ResponseNode[] } | ||
| } | ||
|
|
||
| function firstNonAuthorResponseHours( | ||
| itemCreatedAt: string, | ||
| authorLogin: string | null, | ||
| responses: ResponseNode[], | ||
| ): number | null { | ||
| const firstResponse = responses.find( | ||
| (response) => response.author?.login && response.author.login !== authorLogin, | ||
| ) | ||
| return firstResponse ? hoursBetween(itemCreatedAt, firstResponse.createdAt) : null | ||
| } | ||
|
|
||
| export function computePrMedians(prs: PrNode[]): { | ||
| medianTimeToMergeHours: number | null | ||
| medianTimeToFirstResponseHours: number | null | ||
| } { | ||
| const mergeHours: number[] = [] | ||
| const firstResponseHours: number[] = [] | ||
|
|
||
| for (const pr of prs) { | ||
| if (pr.mergedAt != null) { | ||
| mergeHours.push(hoursBetween(pr.createdAt, pr.mergedAt)) | ||
| } | ||
|
|
||
| const allResponses = [...pr.comments.nodes, ...pr.reviews.nodes].sort( | ||
| (left, right) => new Date(left.createdAt).getTime() - new Date(right.createdAt).getTime(), | ||
| ) | ||
| const responseHours = firstNonAuthorResponseHours( | ||
| pr.createdAt, | ||
| pr.author?.login ?? null, | ||
| allResponses, | ||
| ) | ||
| if (responseHours != null) firstResponseHours.push(responseHours) | ||
| } | ||
|
|
||
| return { | ||
| medianTimeToMergeHours: toIntHours(median(mergeHours)), | ||
| medianTimeToFirstResponseHours: toIntHours(median(firstResponseHours)), | ||
| } | ||
| } | ||
|
|
||
| export function computeIssueMedians(issues: IssueNode[]): { | ||
| medianTimeToCloseHours: number | null | ||
| medianTimeToFirstResponseHours: number | null | ||
| } { | ||
| const closeHours: number[] = [] | ||
| const firstResponseHours: number[] = [] | ||
|
|
||
| for (const issue of issues) { | ||
| if (issue.closedAt != null) { | ||
| closeHours.push(hoursBetween(issue.createdAt, issue.closedAt)) | ||
| } | ||
|
|
||
| const responseHours = firstNonAuthorResponseHours( | ||
| issue.createdAt, | ||
| issue.author?.login ?? null, | ||
| issue.comments.nodes, | ||
| ) | ||
| if (responseHours != null) firstResponseHours.push(responseHours) | ||
| } | ||
|
|
||
| return { | ||
| medianTimeToCloseHours: toIntHours(median(closeHours)), | ||
| medianTimeToFirstResponseHours: toIntHours(median(firstResponseHours)), | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.