Skip to content

Commit

Permalink
Change code to follow some feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
haikalvidya committed Nov 6, 2024
1 parent 0ffea34 commit 8a0fdb3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { authOptions } from '@/lib/auth';
import { fetcher } from '@/lib/utils';
import { isCurrentUserMemberOfProject } from '@/lib/db/utils';
import { db } from '@/lib/db/drizzle';
import { and, asc, eq, sql } from 'drizzle-orm';
import {
evaluationResults,
evaluations,
evaluationScores
} from '@/lib/db/schema';
import { and, asc, eq, sql } from 'drizzle-orm';
} from '@/lib/db/migrations/schema';

export async function GET(
req: Request,
Expand All @@ -24,21 +24,21 @@ export async function GET(
);
}

const getEvaluation = db
.select()
.from(evaluations)
.where(
and(
eq(evaluations.id, evaluationId),
eq(evaluations.projectId, projectId)
)
);
const getEvaluation = db.query.evaluations.findFirst({
where: and(
eq(evaluations.id, evaluationId),
eq(evaluations.projectId, projectId)
)
});

const subQueryScoreCte = db.$with('scores').as(
db
.select({
resultId: evaluationScores.resultId,
scores: sql`jsonb_object_agg(${evaluationScores.name}, ${evaluationScores.score})`
cteScores:
sql`jsonb_object_agg(${evaluationScores.name}, ${evaluationScores.score})`.as(
'cte_scores'
)
})
.from(evaluationScores)
.groupBy(evaluationScores.resultId)
Expand All @@ -52,7 +52,7 @@ export async function GET(
data: evaluationResults.data,
target: evaluationResults.target,
executorOutput: evaluationResults.executorOutput,
scores: subQueryScoreCte.scores,
scores: subQueryScoreCte.cteScores,
traceId: evaluationResults.traceId
})
.from(evaluationResults)
Expand All @@ -72,7 +72,7 @@ export async function GET(
]);

const result = {
evaluation: evaluation[0],
evaluation: evaluation,
results
};

Expand Down
15 changes: 5 additions & 10 deletions frontend/app/api/projects/[projectId]/evaluations/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { evaluations } from '@/lib/db/migrations/schema';
import { and, desc, eq, inArray } from 'drizzle-orm';
import { isCurrentUserMemberOfProject, paginatedGet } from '@/lib/db/utils';
import { Evaluation } from '@/lib/evaluation/types';
import { NextRequest } from 'next/server';

export async function GET(
req: Request,
req: NextRequest,
{ params }: { params: { projectId: string } }
): Promise<Response> {
const projectId = params.projectId;

const { searchParams } = new URL(req.url);
const groupId = searchParams.get('groupId');
const groupId = req.nextUrl.searchParams.get('groupId');

if (!(await isCurrentUserMemberOfProject(projectId))) {
return new Response(
Expand All @@ -33,14 +32,10 @@ export async function GET(

return Response.json(result);
} else {
const baseQuery = db.$with('base').as(db.select().from(evaluations));

const result = await paginatedGet<any, Evaluation>({
table: evaluations,
baseFilters: [eq(sql`project_id`, projectId)],
filters: [],
baseQuery,
orderBy: desc(sql`created_at`)
filters: [eq(evaluations.projectId, projectId)],
orderBy: desc(evaluations.createdAt)
});

return Response.json(result);
Expand Down

0 comments on commit 8a0fdb3

Please sign in to comment.