Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions apps/sim/app/api/logs/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,18 @@ export async function GET(request: NextRequest) {
workflowUpdatedAt: workflow.updatedAt,
}

// Optimized query: Start by filtering workflows in the workspace with user permissions
// This ensures we scan only relevant logs instead of the entire table
const baseQuery = db
.select(selectColumns)
.from(workflowExecutionLogs)
.innerJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id))
.innerJoin(
workflow,
and(
eq(workflowExecutionLogs.workflowId, workflow.id),
eq(workflow.workspaceId, params.workspaceId) // Filter workspace during join!
)
)
.innerJoin(
permissions,
and(
Expand All @@ -107,8 +115,8 @@ export async function GET(request: NextRequest) {
)
)

// Build conditions for the joined query
let conditions: SQL | undefined = eq(workflow.workspaceId, params.workspaceId)
// Build additional conditions for the query
let conditions: SQL | undefined

// Filter by level
if (params.level && params.level !== 'all') {
Expand Down Expand Up @@ -176,11 +184,17 @@ export async function GET(request: NextRequest) {
.limit(params.limit)
.offset(params.offset)

// Get total count for pagination using the same join structure
// Get total count for pagination using the same optimized join structure
const countQuery = db
.select({ count: sql<number>`count(*)` })
.from(workflowExecutionLogs)
.innerJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id))
.innerJoin(
workflow,
and(
eq(workflowExecutionLogs.workflowId, workflow.id),
eq(workflow.workspaceId, params.workspaceId) // Same optimization
)
)
.innerJoin(
permissions,
and(
Expand Down
10 changes: 8 additions & 2 deletions apps/sim/app/api/v1/logs/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export async function GET(request: NextRequest) {
const conditions = buildLogFilters(filters)
const orderBy = getOrderBy(params.order)

// Build and execute query
// Build and execute query - optimized to filter workspace during join
const baseQuery = db
.select({
id: workflowExecutionLogs.id,
Expand All @@ -124,7 +124,13 @@ export async function GET(request: NextRequest) {
workflowDescription: workflow.description,
})
.from(workflowExecutionLogs)
.innerJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id))
.innerJoin(
workflow,
and(
eq(workflowExecutionLogs.workflowId, workflow.id),
eq(workflow.workspaceId, params.workspaceId) // Filter workspace during join!
)
)
.innerJoin(
permissions,
and(
Expand Down
25 changes: 8 additions & 17 deletions apps/sim/socket-server/database/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,8 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
try {
const { operation: op, target, payload, timestamp, userId } = operation

// Log high-frequency operations for monitoring
if (op === 'update-position' && Math.random() < 0.01) {
// Log 1% of position updates
logger.debug('Socket DB operation sample:', {
operation: op,
target,
workflowId: `${workflowId.substring(0, 8)}...`,
})
}

await db.transaction(async (tx) => {
// Update the workflow's last modified timestamp first
await tx
.update(workflow)
.set({ updatedAt: new Date(timestamp) })
.where(eq(workflow.id, workflowId))

// Handle different operation types within the transaction
// Handle different operation types within the transaction first
switch (target) {
case 'block':
await handleBlockOperationTx(tx, workflowId, op, payload, userId)
Expand All @@ -202,6 +186,13 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
default:
throw new Error(`Unknown operation target: ${target}`)
}

if (op !== 'update-position') {
await tx
.update(workflow)
.set({ updatedAt: new Date(timestamp) })
.where(eq(workflow.id, workflowId))
}
})

// Log slow operations for monitoring
Expand Down