Files
simstudioai--sim/apps/sim/lib/data-drains/sources/audit-logs.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

80 lines
2.6 KiB
TypeScript

import { dbReplica } from '@sim/db'
import { auditLog } from '@sim/db/schema'
import { and, inArray, isNull, or, sql } from 'drizzle-orm'
import {
decodeTimeCursor,
encodeTimeCursor,
timeCursorOrderBy,
timeCursorPredicate,
timeCursorStabilityBound,
} from '@/lib/data-drains/sources/cursor'
import { getOrganizationWorkspaceIds } from '@/lib/data-drains/sources/helpers'
import type { Cursor, DrainSource, SourcePageInput } from '@/lib/data-drains/types'
type AuditLogRow = typeof auditLog.$inferSelect
/**
* Drains audit events scoped to the organization: rows from any of the org's
* workspaces, plus org-level rows (`workspace_id IS NULL`) where
* `metadata->>'organizationId'` matches. Audit-log writers consistently set
* `metadata.organizationId` for org-scoped actions even though the table has
* no dedicated FK column.
*/
async function* pages(input: SourcePageInput): AsyncIterable<AuditLogRow[]> {
const workspaceIds = await getOrganizationWorkspaceIds(input.organizationId)
const orgScopedClause = and(
isNull(auditLog.workspaceId),
sql`${auditLog.metadata}->>'organizationId' = ${input.organizationId}`
)
const scopeClause =
workspaceIds.length === 0
? orgScopedClause
: or(inArray(auditLog.workspaceId, workspaceIds), orgScopedClause)
let cursor = decodeTimeCursor(input.cursor)
while (!input.signal.aborted) {
const cursorClause = timeCursorPredicate(auditLog.createdAt, auditLog.id, cursor)
const rows = await dbReplica
.select()
.from(auditLog)
.where(and(scopeClause, timeCursorStabilityBound(auditLog.createdAt), cursorClause))
.orderBy(...timeCursorOrderBy(auditLog.createdAt, auditLog.id))
.limit(input.chunkSize)
if (rows.length === 0) return
yield rows
const last = rows[rows.length - 1]
cursor = { ts: last.createdAt.toISOString(), id: last.id }
if (rows.length < input.chunkSize) return
}
}
export const auditLogsSource: DrainSource<AuditLogRow> = {
type: 'audit_logs',
displayName: 'Audit logs',
pages,
serialize(row) {
return {
id: row.id,
workspaceId: row.workspaceId,
actorId: row.actorId,
actorName: row.actorName,
actorEmail: row.actorEmail,
action: row.action,
resourceType: row.resourceType,
resourceId: row.resourceId,
resourceName: row.resourceName,
description: row.description,
metadata: row.metadata,
ipAddress: row.ipAddress,
userAgent: row.userAgent,
createdAt: row.createdAt.toISOString(),
}
},
cursorAfter(row): Cursor {
return encodeTimeCursor({ ts: row.createdAt.toISOString(), id: row.id })
},
}