-
Notifications
You must be signed in to change notification settings - Fork 246
chore: hydrate prompt spans with old data #1749
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
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
154 changes: 154 additions & 0 deletions
154
packages/core/src/jobs/job-definitions/maintenance/migrateSpansJob.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,154 @@ | ||
import { Job } from 'bullmq' | ||
import { and, asc, eq, gt, inArray, isNotNull, isNull, or } from 'drizzle-orm' | ||
import { commits, documentLogs, spans } from '../../../schema' | ||
import Transaction from '../../../lib/Transaction' | ||
import { Result } from '../../../lib/Result' | ||
import { SpanType } from '@latitude-data/constants' | ||
|
||
type MigrateSpansJobData = { | ||
workspaceId: number | ||
} | ||
|
||
export const migrateSpansJob = async (job: Job<MigrateSpansJobData>) => { | ||
const { workspaceId } = job.data | ||
|
||
// Calculate cutoff date (30 days ago) | ||
const cutoffDate = new Date() | ||
cutoffDate.setDate(cutoffDate.getDate() - 30) | ||
|
||
// Cache for commits to avoid redundant queries | ||
const commitCache = new Map<number, string>() | ||
|
||
return await new Transaction().call(async (tx) => { | ||
const batchSize = 1000 | ||
let cursor: { startedAt: Date; id: string } | null = null | ||
let processedSpans = 0 | ||
|
||
while (true) { | ||
// Query spans that need migration: have documentLogUuid but missing documentUuid/commitUuid/experimentId | ||
// Only process spans of type 'prompt' | ||
// Use cursor-based pagination for efficiency | ||
let whereClause = and( | ||
eq(spans.workspaceId, workspaceId), | ||
eq(spans.type, SpanType.Prompt), | ||
gt(spans.startedAt, cutoffDate), | ||
isNotNull(spans.documentLogUuid), | ||
isNull(spans.documentUuid), | ||
isNull(spans.commitUuid), | ||
isNull(spans.experimentId), | ||
) | ||
|
||
if (cursor) { | ||
whereClause = and( | ||
whereClause, | ||
or( | ||
gt(spans.startedAt, cursor.startedAt), | ||
and(eq(spans.startedAt, cursor.startedAt), gt(spans.id, cursor.id)), | ||
), | ||
) | ||
} | ||
|
||
const spansToMigrate = await tx | ||
.select({ | ||
id: spans.id, | ||
traceId: spans.traceId, | ||
documentLogUuid: spans.documentLogUuid, | ||
startedAt: spans.startedAt, | ||
}) | ||
.from(spans) | ||
.where(whereClause) | ||
.orderBy(asc(spans.startedAt), asc(spans.id)) | ||
.limit(batchSize) | ||
|
||
if (spansToMigrate.length === 0) break | ||
|
||
// Update cursor for next batch | ||
const lastSpan = spansToMigrate[spansToMigrate.length - 1] | ||
cursor = { startedAt: lastSpan.startedAt, id: lastSpan.id } | ||
|
||
// Collect unique document log UUIDs | ||
const documentLogUuids = spansToMigrate.map((s) => s.documentLogUuid!) | ||
const uniqueDocumentLogUuids = [...new Set(documentLogUuids)] | ||
|
||
// Fetch document logs data | ||
const documentLogsData = await tx | ||
.select({ | ||
uuid: documentLogs.uuid, | ||
documentUuid: documentLogs.documentUuid, | ||
commitId: documentLogs.commitId, | ||
experimentId: documentLogs.experimentId, | ||
}) | ||
.from(documentLogs) | ||
.where(inArray(documentLogs.uuid, uniqueDocumentLogUuids)) | ||
|
||
// Create maps for quick lookup | ||
const documentLogMap = new Map( | ||
documentLogsData.map((dl) => [dl.uuid, dl]), | ||
) | ||
|
||
// Collect unique commit IDs that need UUID lookup | ||
const commitIds = documentLogsData | ||
.map((dl) => dl.commitId) | ||
.filter((id): id is number => id !== null && !commitCache.has(id)) | ||
|
||
// Fetch commit UUIDs for uncached commits | ||
if (commitIds.length > 0) { | ||
const commitsData = await tx | ||
.select({ | ||
id: commits.id, | ||
uuid: commits.uuid, | ||
}) | ||
.from(commits) | ||
.where(inArray(commits.id, commitIds)) | ||
|
||
commitsData.forEach((c) => commitCache.set(c.id, c.uuid)) | ||
} | ||
|
||
// Prepare bulk update data | ||
const updateData = spansToMigrate | ||
.map((span) => { | ||
const docLog = documentLogMap.get(span.documentLogUuid!) | ||
if (!docLog) return null | ||
|
||
const commitUuid = docLog.commitId | ||
? commitCache.get(docLog.commitId) || null | ||
: null | ||
|
||
return { | ||
id: span.id, | ||
traceId: span.traceId, | ||
documentUuid: docLog.documentUuid, | ||
commitUuid, | ||
experimentId: docLog.experimentId, | ||
} | ||
}) | ||
.filter( | ||
(update): update is NonNullable<typeof update> => update !== null, | ||
) | ||
|
||
if (updateData.length > 0) { | ||
// Update spans individually since we need to set specific values | ||
for (const update of updateData) { | ||
await tx | ||
.update(spans) | ||
.set({ | ||
documentUuid: update.documentUuid, | ||
commitUuid: update.commitUuid, | ||
experimentId: update.experimentId, | ||
}) | ||
.where( | ||
and(eq(spans.traceId, update.traceId), eq(spans.id, update.id)), | ||
) | ||
.execute() | ||
} | ||
} | ||
|
||
processedSpans += spansToMigrate.length | ||
|
||
// If we got less than batch size, we've processed all spans | ||
if (spansToMigrate.length < batchSize) break | ||
} | ||
|
||
return Result.ok({ processedSpans }) | ||
}) | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/core/src/jobs/job-definitions/maintenance/migrateSpansJobs.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,23 @@ | ||
import { database } from '../../../client' | ||
import { workspaces } from '../../../schema' | ||
import { queues } from '../../queues' | ||
|
||
export const migrateSpansJobs = async () => { | ||
const freeWorkspaces = await database | ||
.select({ | ||
id: workspaces.id, | ||
}) | ||
.from(workspaces) | ||
|
||
let _enqueuedJobs = 0 | ||
|
||
for (const workspace of freeWorkspaces) { | ||
const { maintenanceQueue } = await queues() | ||
await maintenanceQueue.add( | ||
'migrateSpansJob', | ||
{ workspaceId: workspace.id }, | ||
{ attempts: 1 }, | ||
) | ||
_enqueuedJobs++ | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah the comment is wrong (copy pasted)