-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(logs): split summary/detail contracts to make trace tab gate type-safe #4431
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3bbf9f2
fix(logs): split summary/detail contracts to make trace tab gate type…
waleedlatif1 b0c1862
fix(logs): audit follow-ups — render side-effect, stats invalidation,…
waleedlatif1 824f00a
fix(logs): mirror SQL NULLS LAST in JS merge for cursor consistency
waleedlatif1 fd7ea59
fix(logs): final-audit follow-ups — stable tab callback, byExecution …
waleedlatif1 8d86023
refactor(logs): migrate stores/components to contract types
waleedlatif1 ff938c7
refactor(logs): address PR review feedback
waleedlatif1 2f249ca
fix(logs): exclude job logs when level filter is workflow-only
waleedlatif1 a95a80f
chore(logs): drop dead utils — mapToExecutionLog and friends
waleedlatif1 5a91f6c
chore(logs): drop unused LOG_COLUMN_ORDER and LogColumnKey
waleedlatif1 c92ea70
fix(logs): hydrate filters from URL synchronously on mount
waleedlatif1 70fc328
Merge remote-tracking branch 'origin/staging' into waleedlatif1/logs-…
waleedlatif1 e98e80a
chore(logs): trim verbose comments added during PR
waleedlatif1 849ea64
fix(logs): guard navigation arrows when selected log is off-page
waleedlatif1 fb0ae86
fix(logs): sync active-tab callback before paint to keep keyboard gua…
waleedlatif1 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,183 +1,36 @@ | ||
| import { db } from '@sim/db' | ||
| import { | ||
| jobExecutionLogs, | ||
| permissions, | ||
| workflow, | ||
| workflowDeploymentVersion, | ||
| workflowExecutionLogs, | ||
| } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { logIdParamsSchema } from '@/lib/api/contracts/logs' | ||
| import { getLogDetailContract } from '@/lib/api/contracts/logs' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { fetchLogDetail } from '@/lib/logs/fetch-log-detail' | ||
|
|
||
| const logger = createLogger('LogDetailsByIdAPI') | ||
|
|
||
| export const revalidate = 0 | ||
|
|
||
| export const GET = withRouteHandler( | ||
| async (_request: NextRequest, { params }: { params: Promise<{ id: string }> }) => { | ||
| const requestId = generateRequestId() | ||
|
|
||
| try { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| logger.warn(`[${requestId}] Unauthorized log details access attempt`) | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const userId = session.user.id | ||
| const { id } = logIdParamsSchema.parse(await params) | ||
|
|
||
| const rows = await db | ||
| .select({ | ||
| id: workflowExecutionLogs.id, | ||
| workflowId: workflowExecutionLogs.workflowId, | ||
| executionId: workflowExecutionLogs.executionId, | ||
| stateSnapshotId: workflowExecutionLogs.stateSnapshotId, | ||
| deploymentVersionId: workflowExecutionLogs.deploymentVersionId, | ||
| level: workflowExecutionLogs.level, | ||
| status: workflowExecutionLogs.status, | ||
| trigger: workflowExecutionLogs.trigger, | ||
| startedAt: workflowExecutionLogs.startedAt, | ||
| endedAt: workflowExecutionLogs.endedAt, | ||
| totalDurationMs: workflowExecutionLogs.totalDurationMs, | ||
| executionData: workflowExecutionLogs.executionData, | ||
| cost: workflowExecutionLogs.cost, | ||
| files: workflowExecutionLogs.files, | ||
| createdAt: workflowExecutionLogs.createdAt, | ||
| workflowName: workflow.name, | ||
| workflowDescription: workflow.description, | ||
| workflowColor: workflow.color, | ||
| workflowFolderId: workflow.folderId, | ||
| workflowUserId: workflow.userId, | ||
| workflowWorkspaceId: workflow.workspaceId, | ||
| workflowCreatedAt: workflow.createdAt, | ||
| workflowUpdatedAt: workflow.updatedAt, | ||
| deploymentVersion: workflowDeploymentVersion.version, | ||
| deploymentVersionName: workflowDeploymentVersion.name, | ||
| }) | ||
| .from(workflowExecutionLogs) | ||
| .leftJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id)) | ||
| .leftJoin( | ||
| workflowDeploymentVersion, | ||
| eq(workflowDeploymentVersion.id, workflowExecutionLogs.deploymentVersionId) | ||
| ) | ||
| .innerJoin( | ||
| permissions, | ||
| and( | ||
| eq(permissions.entityType, 'workspace'), | ||
| eq(permissions.entityId, workflowExecutionLogs.workspaceId), | ||
| eq(permissions.userId, userId) | ||
| ) | ||
| ) | ||
| .where(eq(workflowExecutionLogs.id, id)) | ||
| .limit(1) | ||
|
|
||
| const log = rows[0] | ||
|
|
||
| // Fallback: check job_execution_logs | ||
| if (!log) { | ||
| const jobRows = await db | ||
| .select({ | ||
| id: jobExecutionLogs.id, | ||
| executionId: jobExecutionLogs.executionId, | ||
| level: jobExecutionLogs.level, | ||
| status: jobExecutionLogs.status, | ||
| trigger: jobExecutionLogs.trigger, | ||
| startedAt: jobExecutionLogs.startedAt, | ||
| endedAt: jobExecutionLogs.endedAt, | ||
| totalDurationMs: jobExecutionLogs.totalDurationMs, | ||
| executionData: jobExecutionLogs.executionData, | ||
| cost: jobExecutionLogs.cost, | ||
| createdAt: jobExecutionLogs.createdAt, | ||
| }) | ||
| .from(jobExecutionLogs) | ||
| .innerJoin( | ||
| permissions, | ||
| and( | ||
| eq(permissions.entityType, 'workspace'), | ||
| eq(permissions.entityId, jobExecutionLogs.workspaceId), | ||
| eq(permissions.userId, userId) | ||
| ) | ||
| ) | ||
| .where(eq(jobExecutionLogs.id, id)) | ||
| .limit(1) | ||
|
|
||
| const jobLog = jobRows[0] | ||
| if (!jobLog) { | ||
| return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
| } | ||
| async (request: NextRequest, context: { params: Promise<{ id: string }> }) => { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const execData = jobLog.executionData as Record<string, any> | null | ||
| const response = { | ||
| id: jobLog.id, | ||
| workflowId: null, | ||
| executionId: jobLog.executionId, | ||
| deploymentVersionId: null, | ||
| deploymentVersion: null, | ||
| deploymentVersionName: null, | ||
| level: jobLog.level, | ||
| status: jobLog.status, | ||
| duration: jobLog.totalDurationMs ? `${jobLog.totalDurationMs}ms` : null, | ||
| trigger: jobLog.trigger, | ||
| createdAt: jobLog.startedAt.toISOString(), | ||
| workflow: null, | ||
| jobTitle: (execData?.trigger?.source as string) || null, | ||
| executionData: { | ||
| totalDuration: jobLog.totalDurationMs, | ||
| ...execData, | ||
| enhanced: true, | ||
| }, | ||
| cost: jobLog.cost as any, | ||
| } | ||
| const parsed = await parseRequest(getLogDetailContract, request, context) | ||
| if (!parsed.success) return parsed.response | ||
|
|
||
| return NextResponse.json({ data: response }) | ||
| } | ||
| const { id } = parsed.data.params | ||
| const { workspaceId } = parsed.data.query | ||
|
|
||
| const workflowSummary = log.workflowId | ||
| ? { | ||
| id: log.workflowId, | ||
| name: log.workflowName, | ||
| description: log.workflowDescription, | ||
| color: log.workflowColor, | ||
| folderId: log.workflowFolderId, | ||
| userId: log.workflowUserId, | ||
| workspaceId: log.workflowWorkspaceId, | ||
| createdAt: log.workflowCreatedAt, | ||
| updatedAt: log.workflowUpdatedAt, | ||
| } | ||
| : null | ||
| const data = await fetchLogDetail({ | ||
| userId: session.user.id, | ||
| workspaceId, | ||
| lookupColumn: 'id', | ||
| lookupValue: id, | ||
| }) | ||
|
|
||
| const response = { | ||
| id: log.id, | ||
| workflowId: log.workflowId, | ||
| executionId: log.executionId, | ||
| deploymentVersionId: log.deploymentVersionId, | ||
| deploymentVersion: log.deploymentVersion ?? null, | ||
| deploymentVersionName: log.deploymentVersionName ?? null, | ||
| level: log.level, | ||
| status: log.status, | ||
| duration: log.totalDurationMs ? `${log.totalDurationMs}ms` : null, | ||
| trigger: log.trigger, | ||
| createdAt: log.startedAt.toISOString(), | ||
| files: log.files || undefined, | ||
| workflow: workflowSummary, | ||
| executionData: { | ||
| totalDuration: log.totalDurationMs, | ||
| ...(log.executionData as any), | ||
| enhanced: true, | ||
| }, | ||
| cost: log.cost as any, | ||
| } | ||
| if (!data) return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
|
|
||
| return NextResponse.json({ data: response }) | ||
| } catch (error: any) { | ||
| logger.error(`[${requestId}] log details fetch error`, error) | ||
| return NextResponse.json({ error: error.message }, { status: 500 }) | ||
| } | ||
| logger.debug('Fetched log detail', { id, workspaceId }) | ||
| return NextResponse.json({ data }) | ||
| } | ||
| ) |
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,36 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { getLogByExecutionIdContract } from '@/lib/api/contracts/logs' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { fetchLogDetail } from '@/lib/logs/fetch-log-detail' | ||
|
|
||
| const logger = createLogger('LogDetailsByExecutionAPI') | ||
|
|
||
| export const GET = withRouteHandler( | ||
| async (request: NextRequest, context: { params: Promise<{ executionId: string }> }) => { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const parsed = await parseRequest(getLogByExecutionIdContract, request, context) | ||
| if (!parsed.success) return parsed.response | ||
|
|
||
| const { executionId } = parsed.data.params | ||
| const { workspaceId } = parsed.data.query | ||
|
|
||
| const data = await fetchLogDetail({ | ||
| userId: session.user.id, | ||
| workspaceId, | ||
| lookupColumn: 'executionId', | ||
| lookupValue: executionId, | ||
| }) | ||
|
|
||
| if (!data) return NextResponse.json({ error: 'Not found' }, { status: 404 }) | ||
|
|
||
| logger.debug('Fetched log by execution id', { executionId, workspaceId }) | ||
| return NextResponse.json({ data }) | ||
| } | ||
| ) | ||
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.