Skip to content

Commit

Permalink
Improve document logs table perfomance
Browse files Browse the repository at this point in the history
Doing something similar to what we did with evaluation results where we
filter by page and object the rows to be shown in the aggregated table
of logs. This should improve performance
  • Loading branch information
andresgutgon committed Oct 21, 2024
1 parent 9c92f04 commit a2cd5fb
Showing 1 changed file with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,99 @@
import { and, desc, eq } from 'drizzle-orm'
import { and, desc, eq, sum } from 'drizzle-orm'

import { Commit } from '../../browser'
import { database } from '../../client'
import {
createDocumentLogQuery,
getCommitFilter,
} from './_createDocumentLogQuery'
DocumentLogsWithErrorsRepository,
DocumentLogWithErrorScope,
} from '../../repositories'
import { commits, providerLogs } from '../../schema'
import { getCommitFilter } from './_createDocumentLogQuery'

function getRepositoryScopes(workspaceId: number, db = database) {
const documentLogsScope = new DocumentLogsWithErrorsRepository(
workspaceId,
db,
).scope
return { documentLogsScope }
}

function getCommonQueryConditions(
scope: DocumentLogWithErrorScope,
documentUuid: string,
draft?: Commit,
) {
return and(eq(scope.documentUuid, documentUuid), getCommitFilter(draft))
}

// TODO: Add test document log without provider should appear here
export function computeDocumentLogsWithMetadataQuery(
{
workspaceId,
documentUuid,
draft,
page = '1',
pageSize = '25',
}: {
workspaceId: number
documentUuid: string
draft?: Commit
page?: string
pageSize?: string
},
db = database,
) {
const { scope, baseQuery } = createDocumentLogQuery(workspaceId, db)
return baseQuery
.where(and(eq(scope.documentUuid, documentUuid), getCommitFilter(draft)))
.orderBy(desc(scope.createdAt))
const { documentLogsScope } = getRepositoryScopes(workspaceId, db)
const offset = (parseInt(page) - 1) * parseInt(pageSize)
const filteredSubQuery = db
.select({
id: documentLogsScope.id,
tokens: sum(providerLogs.tokens).mapWith(Number).as('tokens'),
duration: sum(providerLogs.duration).mapWith(Number).as('duration_in_ms'),
costInMillicents: sum(providerLogs.costInMillicents)
.mapWith(Number)
.as('cost_in_millicents'),
})
.from(documentLogsScope)
.leftJoin(
providerLogs,
eq(providerLogs.documentLogUuid, documentLogsScope.uuid),
)
.where(getCommonQueryConditions(documentLogsScope, documentUuid, draft))
.orderBy(desc(documentLogsScope.createdAt))
.limit(parseInt(pageSize))
.offset(offset)
.as('filteredDocumentLogsSubQuery')

const aggregatedFieldsSubQuery = db
.select({
id: documentLogsScope.id,
tokens: sum(providerLogs.tokens).mapWith(Number).as('tokens'),
duration: sum(providerLogs.duration).mapWith(Number).as('duration_in_ms'),
costInMillicents: sum(providerLogs.costInMillicents)
.mapWith(Number)
.as('cost_in_millicents'),
})
.from(documentLogsScope)
.innerJoin(filteredSubQuery, eq(filteredSubQuery.id, documentLogsScope.id))
.leftJoin(
providerLogs,
eq(providerLogs.documentLogUuid, documentLogsScope.uuid),
)
.groupBy(documentLogsScope.id)
.as('aggregatedFieldsSubQuery')

return db
.select({
...documentLogsScope._.selectedFields,
commit: commits,
tokens: aggregatedFieldsSubQuery.tokens,
duration: aggregatedFieldsSubQuery.duration,
costInMillicents: aggregatedFieldsSubQuery.costInMillicents,
})
.from(documentLogsScope)
.innerJoin(commits, eq(commits.id, documentLogsScope.commitId))
.innerJoin(
aggregatedFieldsSubQuery,
eq(aggregatedFieldsSubQuery.id, documentLogsScope.id),
)
}

0 comments on commit a2cd5fb

Please sign in to comment.