-
Notifications
You must be signed in to change notification settings - Fork 330
feat(web): add GET /api/connections admin endpoint for monitoring connection sync state #1517
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
Harsh23Kashyap
wants to merge
8
commits into
sourcebot-dev:main
Choose a base branch
from
Harsh23Kashyap:feat/connections-status-api
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c2decc2
feat(web): add GET /api/connections admin endpoint
Harsh23Kashyap 9185852
test(web): cover /api/connections auth, pagination, and response shape
Harsh23Kashyap e03f503
docs(openapi): register the public /api/connections endpoint
Harsh23Kashyap cc0290b
docs(openapi): regenerate public OpenAPI spec with /api/connections
Harsh23Kashyap 315bdab
docs(navigation): add /api/connections to the System API Reference group
Harsh23Kashyap 0027189
style(web): brace the Link-header if in /api/connections
Harsh23Kashyap 5c0e4b4
test(web): populate the connection.config mock so the regression test…
Harsh23Kashyap 0564eb1
chore: link the changelog entry to PR #1517 instead of issue #1516
Harsh23Kashyap 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
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
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
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
83 changes: 83 additions & 0 deletions
83
packages/web/src/app/api/(server)/connections/listConnectionsApi.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,83 @@ | ||
| import 'server-only'; | ||
|
|
||
| import { ConnectionSyncJobStatus } from '@sourcebot/db'; | ||
| import { withAuth } from '@/middleware/withAuth'; | ||
| import { sew } from '@/middleware/sew'; | ||
|
|
||
| export interface ListConnectionsParams { | ||
| page: number; | ||
| perPage: number; | ||
| } | ||
|
|
||
| export const listConnections = async ( | ||
| { page, perPage }: ListConnectionsParams, | ||
| ) => sew(() => | ||
| withAuth(async ({ prisma, org }) => { | ||
| const skip = (page - 1) * perPage; | ||
|
|
||
| const [connections, totalCount] = await Promise.all([ | ||
| prisma.connection.findMany({ | ||
| where: { orgId: org.id }, | ||
| orderBy: { name: 'asc' }, | ||
| skip, | ||
| take: perPage, | ||
| include: { | ||
| _count: { | ||
| select: { | ||
| repos: true, | ||
| syncJobs: true, | ||
| }, | ||
| }, | ||
| syncJobs: { | ||
| orderBy: { createdAt: 'desc' }, | ||
| take: 1, | ||
| }, | ||
| }, | ||
| }), | ||
| prisma.connection.count({ where: { orgId: org.id } }), | ||
| ]); | ||
|
|
||
| // Count in-flight jobs (PENDING + IN_PROGRESS) per connection in | ||
| // a single grouped query rather than N+1 small counts. | ||
| const inFlightByConnection = await prisma.connectionSyncJob.groupBy({ | ||
| by: ['connectionId'], | ||
| where: { | ||
| connectionId: { in: connections.map((c) => c.id) }, | ||
| status: { | ||
| in: [ConnectionSyncJobStatus.PENDING, ConnectionSyncJobStatus.IN_PROGRESS], | ||
| }, | ||
| }, | ||
| _count: { _all: true }, | ||
| }); | ||
| const inFlightMap = new Map( | ||
| inFlightByConnection.map((row) => [row.connectionId, row._count._all]), | ||
| ); | ||
|
|
||
| return { | ||
| data: connections.map((connection) => { | ||
| const latestJob = connection.syncJobs[0] ?? null; | ||
| return { | ||
| id: connection.id, | ||
| name: connection.name, | ||
| connectionType: connection.connectionType, | ||
| isDeclarative: connection.isDeclarative, | ||
| syncedAt: connection.syncedAt, | ||
| createdAt: connection.createdAt, | ||
| updatedAt: connection.updatedAt, | ||
| repoCount: connection._count.repos, | ||
| inFlightJobCount: inFlightMap.get(connection.id) ?? 0, | ||
| latestJob: latestJob | ||
| ? { | ||
| id: latestJob.id, | ||
| status: latestJob.status, | ||
| createdAt: latestJob.createdAt, | ||
| completedAt: latestJob.completedAt, | ||
| errorMessage: latestJob.errorMessage, | ||
| } | ||
| : null, | ||
| }; | ||
| }), | ||
| totalCount, | ||
| }; | ||
| }), | ||
| ); |
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.