Skip to content

Commit

Permalink
fix slow query
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Mar 29, 2024
1 parent 729ba6f commit 3782c3a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
LUNARY_PUBLIC_KEY: 259d2d94-9446-478a-ae04-484de705b522
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
timeout-minutes: 1
run: npm run start:backend & npx wait-on http://localhost:3333/v1/health
run: npm run start:backend & npx wait-on http://localhost:3333/v1/health

- name: Start frontend
env:
Expand Down
5 changes: 2 additions & 3 deletions packages/backend/src/api/v1/runs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const formatRun = (run: any) => ({
isPublic: run.isPublic,
feedback: run.feedback,
parentFeedback: run.parentFeedback,
parentFeedbackRunId: run.parentFeedbackRunId,

type: run.type,
name: run.name,
createdAt: run.createdAt,
Expand Down Expand Up @@ -123,8 +123,7 @@ runs.get("/", async (ctx: Context) => {
eu.created_at as user_created_at,
eu.last_seen as user_last_seen,
eu.props as user_props,
rpfc.feedback as parent_feedback,
rpfc.feedback_run_id as parent_feedback_run_id
rpfc.feedback as parent_feedback
from
run r
left join external_user eu on r.external_user_id = eu.id
Expand Down
26 changes: 13 additions & 13 deletions packages/db/0008.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


DROP MATERIALIZED VIEW IF EXISTS feedback_cache;

create materialized view feedback_cache as
Expand Down Expand Up @@ -65,36 +63,38 @@ group by
create unique index on feedback_cache (project_id, feedback);
create index on feedback_cache(project_id);

create index if not exists idx_run_id_parent_run_id on run (id, parent_run_id);
create index if not exists idx_run_feedback_null ON run (id, parent_run_id) WHERE feedback IS NULL;
create index if not exists idx_run_parent_run_id_feedback ON run (parent_run_id, feedback);
CREATE INDEX if not exists idx_run_id_parent_run_id_feedback ON run (id, parent_run_id, feedback);

create materialized view run_parent_feedback_cache as
WITH RECURSIVE run_feedback AS (
SELECT
r.id,
r.parent_run_id,
r.feedback,
r.id AS feedback_run_id
1 AS depth
FROM
run r
UNION
UNION ALL
SELECT
rf.id,
r.parent_run_id,
COALESCE(r.feedback, rf.feedback),
CASE WHEN r.feedback IS NOT NULL THEN r.id ELSE rf.feedback_run_id END
rf.depth + 1
FROM
run_feedback rf
JOIN run r ON rf.parent_run_id = r.id
WHERE
rf.depth < 5 AND rf.feedback IS NULL
)
SELECT
id,
feedback,
feedback_run_id
feedback
FROM
run_feedback
WHERE feedback is not null and feedback_run_id != id
GROUP BY
id,
feedback,
feedback_run_id;
WHERE
feedback IS NOT NULL;

create unique index on run_parent_feedback_cache(id);

70 changes: 37 additions & 33 deletions packages/frontend/components/blocks/Feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
IconThumbDown,
IconThumbUp,
} from "@tabler/icons-react"
import { useEffect } from "react"
import { useEffect, useState } from "react"
import analytics from "../../utils/analytics"
import { useFixedColorScheme } from "@/utils/hooks"

Expand All @@ -34,37 +34,41 @@ export default function Feedback({
})

return (
<Indicator inline disabled={!isFromParent} color="red" size={10}>
{/* <Tooltip disabled={!isFromParent} label={"Feedback from parent run"}> */}
<Group
style={{
padding: isFromParent ? "3px 6px" : "",
borderRadius: 6,
border: isFromParent
? `1px solid var(--mantine-color-default-border)`
: "",
}}
>
{data?.thumbs === "up" && <IconThumbUp {...getIconProps("green")} />}
{data?.thumbs === "down" && <IconThumbDown {...getIconProps("red")} />}
{typeof data?.rating === "number" && (
<Group gap={3}>
{Array.from({ length: data.rating }).map((_, i) => (
<IconStar key={i} {...getIconProps("yellow")} />
))}
</Group>
)}
{data?.emoji && <span>{data.emoji}</span>}
{typeof data?.comment === "string" && (
/* Support for comment == "" in the filters */
<Tooltip label={data.comment} disabled={!data.comment}>
<IconMessage {...getIconProps("teal")} />
</Tooltip>
)}
{data?.retried && (
<IconRefresh {...getIconProps("violet")} fillOpacity={0} />
)}
</Group>
</Indicator>
<Tooltip label="Feedback from parent run" position="bottom">
<Indicator inline disabled={!isFromParent} color="red" size={10}>
{/* <Tooltip disabled={!isFromParent} label={"Feedback from parent run"}> */}
<Group
style={{
padding: isFromParent ? "3px 6px" : "",
borderRadius: 6,
border: isFromParent
? `1px solid var(--mantine-color-default-border)`
: "",
}}
>
{data?.thumbs === "up" && <IconThumbUp {...getIconProps("green")} />}
{data?.thumbs === "down" && (
<IconThumbDown {...getIconProps("red")} />
)}
{typeof data?.rating === "number" && (
<Group gap={3}>
{Array.from({ length: data.rating }).map((_, i) => (
<IconStar key={i} {...getIconProps("yellow")} />
))}
</Group>
)}
{data?.emoji && <span>{data.emoji}</span>}
{typeof data?.comment === "string" && (
/* Support for comment == "" in the filters */
<Tooltip label={`“${data.comment}”`} disabled={!data.comment}>
<IconMessage {...getIconProps("teal")} />
</Tooltip>
)}
{data?.retried && (
<IconRefresh {...getIconProps("violet")} fillOpacity={0} />
)}
</Group>
</Indicator>
</Tooltip>
)
}
21 changes: 9 additions & 12 deletions packages/frontend/components/layout/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,16 @@ export default function Navbar() {
setSendingEmail(true)

const ok = await errorHandler(
fetch(
`${process.env.NEXT_PUBLIC_API_URL || window.location.origin}/v1/users/send-verification`,
{
method: "POST",
body: JSON.stringify({
email: user?.email,
name: user?.name,
}),
headers: {
"Content-Type": "application/json",
},
fetch(`${process.env.API_URL}/v1/users/send-verification`, {
method: "POST",
body: JSON.stringify({
email: user?.email,
name: user?.name,
}),
headers: {
"Content-Type": "application/json",
},
),
}),
)

if (ok) {
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/pages/team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function SAMLConfig() {
<Table.Td>Sign on URL:</Table.Td>
<Table.Td>
<CopyInput
value={`${process.env.NEXT_PUBLIC_API_URL || window.location.origin}/login`}
value={`${process.env.NEXT_PUBLIC_APP_URL || window.location.origin}/login`}
/>
</Table.Td>
</Table.Tr>
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/utils/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function feedbackColumn(withRelatedRuns = false) {
const run = props.row.original

const feedback = run.feedback || run.parentFeedback
const isParentFeedback = run.parentFeedbackRunId
const isParentFeedback = !run.feedback && run.parentFeedback

return <Feedback data={feedback} isFromParent={isParentFeedback} />
}
Expand Down

0 comments on commit 3782c3a

Please sign in to comment.