Skip to content

Commit

Permalink
feat: drop parent feedback cache materialized view (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt authored Feb 14, 2025
1 parent 14f154b commit fdb8217
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
45 changes: 38 additions & 7 deletions packages/backend/src/api/v1/runs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ function getRunQuery(ctx: Context, isExport = false) {
from
public.run r
left join external_user eu on r.external_user_id = eu.id
left join run_parent_feedback_cache rpfc on r.id = rpfc.id
left join template_version tv on r.template_version_id = tv.id
left join template t on tv.template_id = t.id
left join evaluation_result_v2 er on r.id = er.run_id
Expand Down Expand Up @@ -646,18 +645,35 @@ runs.get("/", async (ctx: Context) => {
eu.last_seen as user_last_seen,
eu.props as user_props,
t.slug as template_slug,
rpfc.feedback as parent_feedback
parent_feedback.feedback as parent_feedback
from
public.run r
left join external_user eu on r.external_user_id = eu.id
left join run_parent_feedback_cache rpfc on r.id = rpfc.id
left join template_version tv on r.template_version_id = tv.id
left join template t on tv.template_id = t.id
left join evaluation_result_v2 er on r.id = er.run_id
left join evaluator e on er.evaluator_id = e.id
cross join lateral (
select jsonb_path_query_array(er.result, '$.input[*].topic') || jsonb_path_query_array(er.result, '$.output[*].topic')
) topics(topics)
left join lateral (
with recursive parent_runs as (
select id, parent_run_id, feedback from run where id = r.id
union all
select
r.id, r.parent_run_id, r.feedback from run r
join parent_runs on parent_runs.parent_run_id = r.id
where
r.parent_run_id is not null
and r.type = 'chat'
)
select
feedback
from
parent_runs
where
parent_runs.id != r.id
) parent_feedback on true
where
r.project_id = ${projectId}
${parentRunCheck}
Expand Down Expand Up @@ -705,15 +721,32 @@ runs.get("/count", async (ctx: Context) => {
eu.last_seen as user_last_seen,
eu.props as user_props,
t.slug as template_slug,
rpfc.feedback as parent_feedback
parent_feedback.feedback as parent_feedback
from
public.run r
left join external_user eu on r.external_user_id = eu.id
left join run_parent_feedback_cache rpfc on r.id = rpfc.id
left join template_version tv on r.template_version_id = tv.id
left join template t on tv.template_id = t.id
left join evaluation_result_v2 er on r.id = er.run_id
left join evaluator e on er.evaluator_id = e.id
left join lateral (
with recursive parent_runs as (
select id, parent_run_id, feedback from run where id = r.id
union all
select
r.id, r.parent_run_id, r.feedback from run r
join parent_runs on parent_runs.parent_run_id = r.id
where
r.parent_run_id is not null
and r.type = 'chat'
)
select
feedback
from
parent_runs
where
parent_runs.id != r.id
) parent_feedback on true
where
r.project_id = ${projectId}
${parentRunCheck}
Expand Down Expand Up @@ -924,7 +957,6 @@ runs.get("/:id", async (ctx) => {
run r
left join run_score rs on r.id = rs.run_id
left join external_user eu on r.external_user_id = eu.id
left join run_parent_feedback_cache rpfc on r.id = rpfc.id
left join template_version tv on r.template_version_id = tv.id
left join template t on tv.template_id = t.id
left join evaluation_result_v2 er on r.id = er.run_id
Expand Down Expand Up @@ -1360,7 +1392,6 @@ function buildBaseRunsQuery(ctx: Context) {
from
public.run r
left join external_user eu on r.external_user_id = eu.id
left join run_parent_feedback_cache rpfc on r.id = rpfc.id
left join template_version tv ON r.template_version_id = tv.id
left join template t on tv.template_id = t.id
left join evaluation_result_v2 er ON r.id = er.run_id
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/checks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ export const CHECK_RUNNERS: CheckRunner[] = [

if (key === "comment") {
// comment is a special case because there can be infinite values
return sql`(r.feedback->${key} is not null or rpfc.feedback->${key} is not null)`;
return sql`(r.feedback->${key} is not null or parent_feedback.feedback->${key} is not null)`;
} else if (key === "thumb") {
return sql`(r.feedback->>'thumbs' = ${value} or rpfc.feedback->>'thumbs' = ${value} or r.feedback->>'thumb' = ${value} or rpfc.feedback->>'thumb' = ${value})`;
return sql`(r.feedback->>'thumbs' = ${value} or parent_feedback.feedback->>'thumbs' = ${value} or r.feedback->>'thumb' = ${value} or parent_feedback.feedback->>'thumb' = ${value})`;
} else {
return sql`(r.feedback->>${key} = ${value} OR rpfc.feedback->>${key} = ${value})`;
return sql`(r.feedback->>${key} = ${value} OR parent_feedback.feedback->>${key} = ${value})`;
}
}),
);
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/checks/runChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ async function sqlEval(sqlFragment: any, run: any): Promise<boolean> {
from
temp_run r
left join external_user eu on r.external_user_id = eu.id
left join run_parent_feedback_cache rpfc on r.id = rpfc.id
left join template_version tv on r.template_version_id = tv.id
left join template t on tv.template_id = t.id
left join evaluation_result_v2 er on r.id = er.run_id
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/jobs/materialized-views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function startMaterializedViewRefreshJob() {
return;
}
try {
const views = ["run_parent_feedback_cache", "metadata_cache"];
const views = ["metadata_cache"];

while (true) {
for (const view of views) {
Expand All @@ -19,7 +19,7 @@ export async function startMaterializedViewRefreshJob() {
);
}

await sleep(60000);
await sleep(5 * 60 * 1000);
}
} catch (error) {
console.error(error);
Expand Down
1 change: 1 addition & 0 deletions packages/db/0063.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop materialized view if exists run_parent_feedback_cache;

0 comments on commit fdb8217

Please sign in to comment.