Skip to content

Commit

Permalink
refactor(repositories): update repository inheritance to use Reposito…
Browse files Browse the repository at this point in the history
…ryLegacy and RepositoryV2

This commit refactors the repository classes to inherit from either RepositoryLegacy or RepositoryV2 instead of the original Repository class. The changes are made to improve the baseline performance of the repository classes.
Legacy repositories make use of subqueries which can often be slow. New repository uses drizzle's dynamic queries which are much faster. This change should improve the performance of the document logs section.

- Updated various repository classes to extend RepositoryLegacy.
- Introduced RepositoryV2 for new repository implementations.
- Adjusted method calls and query scopes to align with the new repository structure.
- Ensured that all references to the old Repository class are replaced with the appropriate new classes.

This refactor aims to streamline the transition to the new repository structure while maintaining backward compatibility with legacy code.
  • Loading branch information
geclos committed Oct 24, 2024
1 parent 48358b8 commit 7fb7454
Show file tree
Hide file tree
Showing 29 changed files with 281 additions and 247 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/repositories/apiKeysRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { eq, getTableColumns } from 'drizzle-orm'
import { ApiKey } from '../browser'
import { NotFoundError, Result } from '../lib'
import { apiKeys } from '../schema'
import Repository from './repository'
import RepositoryLegacy from './repository'

const tt = getTableColumns(apiKeys)

export class ApiKeysRepository extends Repository<typeof tt, ApiKey> {
export class ApiKeysRepository extends RepositoryLegacy<typeof tt, ApiKey> {
get scope() {
return this.db
.select(tt)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/claimedRewardsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { and, eq, getTableColumns, isNull, not, or, sum } from 'drizzle-orm'
import { ClaimedReward, RewardType } from '../browser'
import { Result } from '../lib'
import { claimedRewards } from '../schema/models/claimedRewards'
import Repository from './repository'
import RepositoryLegacy from './repository'

const tt = getTableColumns(claimedRewards)

export class ClaimedRewardsRepository extends Repository<
export class ClaimedRewardsRepository extends RepositoryLegacy<
typeof tt,
ClaimedReward
> {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/commitsRepository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
recomputeChanges,
RecomputedChanges,
} from '../../services/documents/recomputeChanges'
import Repository from '../repository'
import RepositoryLegacy from '../repository'
import { buildCommitsScope, columnSelection } from './utils/buildCommitsScope'
import { getHeadCommitForProject } from './utils/getHeadCommit'

Expand Down Expand Up @@ -47,7 +47,7 @@ function filterByStatusQuery({
}
}

export class CommitsRepository extends Repository<
export class CommitsRepository extends RepositoryLegacy<
typeof columnSelection,
Commit
> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { DocumentVersionsRepository } from '../documentVersionsRepository'
import { EvaluationResultsRepository } from '../evaluationResultsRepository'
import { EvaluationsRepository } from '../evaluationsRepository'
import Repository from '../repository'
import RepositoryLegacy from '../repository'

const tt = getTableColumns(connectedEvaluations)

Expand All @@ -36,7 +36,7 @@ export type ConnectedDocumentWithMetadata = DocumentVersion & {
modalValueCount: number
}

export class ConnectedEvaluationsRepository extends Repository<
export class ConnectedEvaluationsRepository extends RepositoryLegacy<
typeof tt,
ConnectedEvaluation
> {
Expand Down Expand Up @@ -128,29 +128,27 @@ export class ConnectedEvaluationsRepository extends Repository<
),
)

const evaluationResultsRepo = new EvaluationResultsRepository(
this.workspaceId,
this.db,
)
const { scope } = new EvaluationResultsRepository(this.workspaceId, this.db)
const evaluationResultsScope = scope.as('evaluation_results_repo')
const selectedEvaluationResults = this.db
.$with('selected_evaluation_results')
.as(
this.db
.select({
...evaluationResultsRepo.scope._.selectedFields,
...evaluationResultsScope._.selectedFields,
...getTableColumns(documentLogs),
...getTableColumns(providerLogs),
})
.from(evaluationResultsRepo.scope)
.from(evaluationResultsScope)
.innerJoin(
documentLogs,
eq(documentLogs.id, evaluationResultsRepo.scope.documentLogId),
eq(documentLogs.id, evaluationResultsScope.documentLogId),
)
.innerJoin(
providerLogs,
eq(providerLogs.id, evaluationResultsRepo.scope.providerLogId),
eq(providerLogs.id, evaluationResultsScope.providerLogId),
)
.where(eq(evaluationResultsRepo.scope.evaluationId, evaluationId)),
.where(eq(evaluationResultsScope.evaluationId, evaluationId)),
)

const aggregatedResults = this.db
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/datasetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { eq, sql } from 'drizzle-orm'

import { Dataset } from '../browser'
import { datasets, users } from '../schema'
import Repository from './repository'
import RepositoryLegacy from './repository'

export const datasetColumns = {
id: datasets.id,
Expand All @@ -19,7 +19,7 @@ export const datasetColumns = {
name: sql`${users.name}`.as('users_name'),
},
}
export class DatasetsRepository extends Repository<
export class DatasetsRepository extends RepositoryLegacy<
typeof datasetColumns,
Dataset
> {
Expand Down
11 changes: 4 additions & 7 deletions packages/core/src/repositories/documentLogsRepository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
runErrors,
workspaces,
} from '../../schema'
import Repository from '../repository'
import Repository from '../repositoryV2'

export type DocumentLogWithMetadata = DocumentLog & {
commit: Commit
Expand All @@ -20,7 +20,7 @@ export type DocumentLogWithMetadata = DocumentLog & {

const tt = getTableColumns(documentLogs)

export class DocumentLogsRepository extends Repository<typeof tt, DocumentLog> {
export class DocumentLogsRepository extends Repository<DocumentLog> {
get scope() {
return this.db
.select(tt)
Expand All @@ -39,14 +39,11 @@ export class DocumentLogsRepository extends Repository<typeof tt, DocumentLog> {
),
)
.where(and(isNull(runErrors.id), eq(workspaces.id, this.workspaceId)))
.as('documentLogsScope')
.$dynamic()
}

async findByUuid(uuid: string) {
const result = await this.db
.select()
.from(this.scope)
.where(eq(this.scope.uuid, uuid))
const result = await this.scope.where(eq(documentLogs.uuid, uuid))

if (!result.length) {
return Result.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Commit, DocumentVersion } from '../../browser'
import { NotFoundError, Result } from '../../lib'
import { commits, documentVersions, projects } from '../../schema'
import { CommitsRepository } from '../commitsRepository'
import Repository from '../repository'
import RepositoryLegacy from '../repository'

function mergeDocuments(
...documentsArr: DocumentVersion[][]
Expand Down Expand Up @@ -46,7 +46,7 @@ const tt = {
projectId: sql<number>`${projects.id}::int`.as('projectId'),
}

export class DocumentVersionsRepository extends Repository<
export class DocumentVersionsRepository extends RepositoryLegacy<
typeof tt,
DocumentVersion & { mergedAt: Date | null; projectId: number }
> {
Expand Down
22 changes: 10 additions & 12 deletions packages/core/src/repositories/evaluationResultsRepository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
evaluations,
runErrors,
} from '../../schema'
import Repository from '../repository'
import Repository from '../repositoryV2'

export const evaluationResultDto = {
...getTableColumns(evaluationResults),
Expand All @@ -50,10 +50,7 @@ export type EvaluationResultWithMetadata = EvaluationResultDto & {
documentContentHash: string
}

export class EvaluationResultsRepository extends Repository<
typeof evaluationResultDto,
EvaluationResultDto
> {
export class EvaluationResultsRepository extends Repository<EvaluationResultDto> {
get scope() {
return this.db
.select(evaluationResultDto)
Expand Down Expand Up @@ -104,7 +101,7 @@ export class EvaluationResultsRepository extends Repository<
eq(evaluations.workspaceId, this.workspaceId),
),
)
.as('evaluationResultsBaseQuery')
.$dynamic()
}

async findByContentHash({
Expand All @@ -114,17 +111,18 @@ export class EvaluationResultsRepository extends Repository<
evaluationId: number
contentHash: string
}) {
const result = await this.db
.select(this.scope._.selectedFields)
.from(this.scope)
.innerJoin(documentLogs, eq(documentLogs.id, this.scope.documentLogId))
const result = await this.scope
.innerJoin(
documentLogs,
eq(documentLogs.id, evaluationResults.documentLogId),
)
.where(
and(
eq(this.scope.evaluationId, evaluationId),
eq(evaluationResults.evaluationId, evaluationId),
eq(documentLogs.contentHash, contentHash),
),
)
.orderBy(desc(this.scope.createdAt))
.orderBy(desc(evaluationResults.createdAt))

return Result.ok(result.map(this.parseResult))
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/evaluationsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
evaluations,
llmAsJudgeEvaluationMetadatas,
} from '../schema'
import Repository from './repository'
import RepositoryLegacy from './repository'

const tt = {
...getTableColumns(evaluations),
Expand All @@ -27,7 +27,7 @@ const tt = {
},
}

export class EvaluationsRepository extends Repository<
export class EvaluationsRepository extends RepositoryLegacy<
typeof tt,
EvaluationDto
> {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/latitudeApiKeysRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { eq, getTableColumns } from 'drizzle-orm'

import { ApiKey } from '../browser'
import { apiKeys } from '../schema'
import Repository from './repository'
import RepositoryLegacy from './repository'

const tt = getTableColumns(apiKeys)

export class LatitudeApiKeysRepository extends Repository<typeof tt, ApiKey> {
export class LatitudeApiKeysRepository extends RepositoryLegacy<typeof tt, ApiKey> {
get scope() {
return this.db
.select(tt)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/membershipsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { eq, getTableColumns } from 'drizzle-orm'
import { Membership } from '../browser'
import { NotFoundError, Result } from '../lib'
import { memberships } from '../schema'
import Repository from './repository'
import RepositoryLegacy from './repository'

const tt = getTableColumns(memberships)

export class MembershipsRepository extends Repository<typeof tt, Membership> {
export class MembershipsRepository extends RepositoryLegacy<typeof tt, Membership> {
get scope() {
return this.db
.select()
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/projectsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
import { Project } from '../browser'
import { NotFoundError, Result } from '../lib'
import { commits, documentVersions, projects } from '../schema'
import Repository from './repository'
import RepositoryLegacy from './repository'

const NOT_FOUND_MSG = 'Project not found'

const tt = getTableColumns(projects)

export class ProjectsRepository extends Repository<typeof tt, Project> {
export class ProjectsRepository extends RepositoryLegacy<typeof tt, Project> {
get scope() {
return this.db
.select(tt)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/repositories/providerApiKeysRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { and, eq, getTableColumns, isNull } from 'drizzle-orm'
import { ProviderApiKey } from '../browser'
import { NotFoundError, Result } from '../lib'
import { providerApiKeys } from '../schema'
import Repository from './repository'
import RepositoryLegacy from './repository'

const tt = getTableColumns(providerApiKeys)

export class ProviderApiKeysRepository extends Repository<
export class ProviderApiKeysRepository extends RepositoryLegacy<
typeof tt,
ProviderApiKey
> {
Expand Down
37 changes: 14 additions & 23 deletions packages/core/src/repositories/providerLogsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {
providerLogs,
workspaces,
} from '../schema'
import Repository, { QueryOptions } from './repository'
import { QueryOptions } from './repository'
import Repository from './repositoryV2'

const tt = getTableColumns(providerLogs)

export class ProviderLogsRepository extends Repository<typeof tt, ProviderLog> {
export class ProviderLogsRepository extends Repository<ProviderLog> {
get scope() {
return this.db
.select(tt)
Expand All @@ -22,15 +23,11 @@ export class ProviderLogsRepository extends Repository<typeof tt, ProviderLog> {
eq(providerApiKeys.id, providerLogs.providerId),
)
.innerJoin(workspaces, eq(workspaces.id, providerApiKeys.workspaceId))
.as('providerLogsScope')
.$dynamic()
}

async findByUuid(uuid: string) {
const result = await this.db
.select()
.from(this.scope)
.where(eq(this.scope.uuid, uuid))
.limit(1)
const result = await this.scope.where(eq(providerLogs.uuid, uuid)).limit(1)

if (!result.length) {
return Result.error(new NotFoundError('ProviderLog not found'))
Expand All @@ -40,15 +37,13 @@ export class ProviderLogsRepository extends Repository<typeof tt, ProviderLog> {
}

async findByDocumentUuid(documentUuid: string, opts: QueryOptions = {}) {
const query = this.db
.select(this.scope._.selectedFields)
.from(this.scope)
const query = this.scope
.innerJoin(
documentLogs,
eq(documentLogs.uuid, this.scope.documentLogUuid),
eq(documentLogs.uuid, providerLogs.documentLogUuid),
)
.where(eq(documentLogs.documentUuid, documentUuid))
.orderBy(asc(this.scope.generatedAt))
.orderBy(asc(providerLogs.generatedAt))

if (opts.limit !== undefined) {
query.limit(opts.limit)
Expand All @@ -67,11 +62,9 @@ export class ProviderLogsRepository extends Repository<typeof tt, ProviderLog> {
return Result.error(new NotFoundError('documentLogUuid is required'))
}

const result = await this.db
.select()
.from(this.scope)
.where(eq(this.scope.documentLogUuid, documentLogUuid))
.orderBy(desc(this.scope.generatedAt))
const result = await this.scope
.where(eq(providerLogs.documentLogUuid, documentLogUuid))
.orderBy(desc(providerLogs.generatedAt))
.limit(1)

if (!result.length) {
Expand All @@ -85,11 +78,9 @@ export class ProviderLogsRepository extends Repository<typeof tt, ProviderLog> {
documentLogUuid: string,
opts: QueryOptions = {},
) {
const query = this.db
.select()
.from(this.scope)
.where(eq(this.scope.documentLogUuid, documentLogUuid))
.orderBy(asc(this.scope.generatedAt))
const query = this.scope
.where(eq(providerLogs.documentLogUuid, documentLogUuid))
.orderBy(asc(providerLogs.generatedAt))

if (opts.limit !== undefined) {
query.limit(opts.limit)
Expand Down
Loading

0 comments on commit 7fb7454

Please sign in to comment.