Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: add ui for background migrations (#4602)

Max Deichmann0eb129e1867c
diff --git a/web/src/components/VersionLabel.tsx b/web/src/components/VersionLabel.tsx
index 57d5b5f70b..6a06af2fc4 100644
--- a/web/src/components/VersionLabel.tsx
+++ b/web/src/components/VersionLabel.tsx
@@ -1,4 +1,5 @@
import {
+ ArrowUp10,
BadgeCheck,
Github,
HardDriveDownload,
@@ -23,8 +24,20 @@ import { env } from "@/src/env.mjs";
import { cn } from "@/src/utils/tailwind";
import { usePlan } from "@/src/features/entitlements/hooks";
import { isSelfHostedPlan, planLabels } from "@langfuse/shared";
+import { StatusBadge } from "@/src/components/layouts/status-badge";
export const VersionLabel = ({ className }: { className?: string }) => {
+ const backgroundMigrationStatus = api.backgroundMigrations.status.useQuery(
+ undefined,
+ {
+ refetchOnMount: false,
+ refetchOnWindowFocus: false,
+ refetchOnReconnect: false,
+ enabled: !env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION, // do not check for updates on Langfuse Cloud
+ onError: (error) => console.error("checkUpdate error", error), // do not render default error message
+ },
+ );
+
const checkUpdate = api.public.checkUpdate.useQuery(undefined, {
refetchOnMount: false,
refetchOnWindowFocus: false,
@@ -32,6 +45,7 @@ export const VersionLabel = ({ className }: { className?: string }) => {
enabled: !env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION, // do not check for updates on Langfuse Cloud
onError: (error) => console.error("checkUpdate error", error), // do not render default error message
});
+
const plan = usePlan();
const isLangfuseCloud = Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);
@@ -50,10 +64,13 @@ export const VersionLabel = ({ className }: { className?: string }) => {
: // null on cloud
null;
+ const showBackgroundMigrationStatus =
+ !isLangfuseCloud &&
+ backgroundMigrationStatus.data &&
+ backgroundMigrationStatus.data.status !== "FINISHED";
+
const hasUpdate =
- !env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION &&
- checkUpdate.data &&
- checkUpdate.data.updateType;
+ !isLangfuseCloud && checkUpdate.data && checkUpdate.data.updateType;
const color =
checkUpdate.data?.updateType === "major"
@@ -68,7 +85,16 @@ export const VersionLabel = ({ className }: { className?: string }) => {
<Button variant="ghost" size="xs" className={cn("text-xs", className)}>
{VERSION}
{selfHostedPlanLabel ? ` ${selfHostedPlanLabel.short}` : null}
- {hasUpdate && <ArrowUp className={`ml-1 h-3 w-3 ${color}`} />}
+ {showBackgroundMigrationStatus && (
+ <StatusBadge
+ type={backgroundMigrationStatus.data?.status.toLowerCase()}
+ showText={false}
+ className="bg-transparent"
+ />
+ )}
+ {hasUpdate && !showBackgroundMigrationStatus && (
+ <ArrowUp className={`ml-1 h-3 w-3 ${color}`} />
+ )}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent onClick={(e) => e.stopPropagation()}>
@@ -104,6 +130,19 @@ export const VersionLabel = ({ className }: { className?: string }) => {
Releases
</Link>
</DropdownMenuItem>
+ <DropdownMenuItem asChild>
+ <Link href="/background-migrations">
+ <ArrowUp10 size={16} className="mr-2" />
+ Background Migrations
+ {showBackgroundMigrationStatus && (
+ <StatusBadge
+ type={backgroundMigrationStatus.data?.status.toLowerCase()}
+ showText={false}
+ className="bg-transparent"
+ />
+ )}
+ </Link>
+ </DropdownMenuItem>
<DropdownMenuItem asChild>
<Link href="https://langfuse.com/changelog" target="_blank">
<Newspaper size={16} className="mr-2" />
diff --git a/web/src/components/layouts/status-badge.tsx b/web/src/components/layouts/status-badge.tsx
index 94412bf6f8..dfd26fb1c5 100644
--- a/web/src/components/layouts/status-badge.tsx
+++ b/web/src/components/layouts/status-badge.tsx
@@ -15,10 +15,12 @@ export const StatusBadge = ({
type,
isLive = true,
className,
+ showText = true,
}: {
type: Status | (string & {});
isLive?: boolean;
className?: string;
+ showText?: boolean;
}) => {
let badgeColor = "bg-muted-gray text-primary";
let dotColor = "bg-muted-foreground";
@@ -66,7 +68,7 @@ export const StatusBadge = ({
></span>
</span>
)}
- <span>{type}</span>
+ {showText && <span>{type}</span>}
</div>
);
};
diff --git a/web/src/features/background-migrations/components/background-migrations.tsx b/web/src/features/background-migrations/components/background-migrations.tsx
new file mode 100644
index 0000000000..61c5166845
--- /dev/null
+++ b/web/src/features/background-migrations/components/background-migrations.tsx
@@ -0,0 +1,104 @@
+import Header from "@/src/components/layouts/header";
+import { DataTable } from "@/src/components/table/data-table";
+import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";
+import { type LangfuseColumnDef } from "@/src/components/table/types";
+import { api } from "@/src/utils/api";
+import { type BackgroundMigration } from "@langfuse/shared";
+import { RetryBackgroundMigration } from "@/src/features/background-migrations/components/retry-background-migration";
+import { StatusBadge } from "@/src/components/layouts/status-badge";
+
+export default function BackgroundMigrationsTable() {
+ const backgroundMigrations = api.backgroundMigrations.all.useQuery();
+
+ const columns = [
+ {
+ accessorKey: "name",
+ id: "name",
+ enableColumnFilter: false,
+ header: "Name",
+ },
+ {
+ accessorKey: "script",
+ id: "script",
+ enableColumnFilter: false,
+ header: "Script",
+ },
+ {
+ accessorKey: "args",
+ id: "args",
+ enableColumnFilter: false,
+ header: "Args",
+ size: 80,
+ cell: (row) => JSON.stringify(row.getValue()),
+ },
+ {
+ id: "status",
+ header: "Status",
+ size: 80,
+ cell: (row) => {
+ const failedAt = row.row.original.failedAt;
+ if (failedAt) {
+ return <StatusBadge type={"failed"} className="capitalize" />;
+ }
+ const finishedAt = row.row.original.finishedAt;
+ if (finishedAt) {
+ return <StatusBadge type={"finished"} className="capitalize" />;
+ }
+ return <StatusBadge type={"active"} className="capitalize" />;
+ },
+ },
+ {
+ accessorKey: "failedReason",
+ id: "failedReason",
+ enableColumnFilter: false,
+ header: "Failed Reason",
+ },
+ {
+ accessorKey: "state",
+ id: "state",
+ enableColumnFilter: false,
+ header: "State",
+ cell: (row) => JSON.stringify(row.getValue()),
+ },
+ {
+ id: "actions",
+ header: "Actions",
+ size: 65,
+ cell: (row) => {
+ const name = row.row.original.name;
+ const isRetryable = row.row.original.failedAt !== null;
+ return (
+ <RetryBackgroundMigration
+ backgroundMigrationName={name}
+ isRetryable={isRetryable}
+ />
+ );
+ },
+ },
+ ] as LangfuseColumnDef<BackgroundMigration>[];
+
+ return (
+ <>
+ <Header title="Background Migrations" />
+ <DataTableToolbar columns={columns} />
+ <DataTable
+ columns={columns}
+ data={
+ backgroundMigrations.isLoading
+ ? { isLoading: true, isError: false }
+ : backgroundMigrations.isError
+ ? {
+ isLoading: false,
+ isError: true,
+ error: backgroundMigrations.error.message,
+ }
+ : {
+ isLoading: false,
+ isError: false,
+ data: backgroundMigrations.data.migrations,
+ }
+ }
+ />
+ </>
+ );
+}
diff --git a/web/src/features/background-migrations/components/retry-background-migration.tsx b/web/src/features/background-migrations/components/retry-background-migration.tsx
new file mode 100644
index 0000000000..f33ca27693
--- /dev/null
+++ b/web/src/features/background-migrations/components/retry-background-migration.tsx
@@ -0,0 +1,59 @@
+import { Button } from "@/src/components/ui/button";
+import { api } from "@/src/utils/api";
+import { useState } from "react";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/src/components/ui/popover";
+import { RotateCcw } from "lucide-react";
+
+export function RetryBackgroundMigration({
+ backgroundMigrationName,
+ isRetryable,
+}: {
+ backgroundMigrationName: string;
+ isRetryable: boolean;
+}) {
+ const utils = api.useUtils();
+ const [isOpen, setIsOpen] = useState(false);
+
+ const mutRetryBackgroundMigration =
+ api.backgroundMigrations.retry.useMutation({
+ onSuccess: () => {
+ void utils.backgroundMigrations.invalidate();
+ },
+ });
+
+ return (
+ <Popover open={isOpen} onOpenChange={() => setIsOpen(!isOpen)}>
+ <PopoverTrigger asChild>
+ <Button variant="ghost" size="xs" disabled={!isRetryable}>
+ <RotateCcw className="h-4 w-4" />
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent>
+ <h2 className="text-md mb-3 font-semibold">Please confirm</h2>
+ <p className="mb-3 text-sm">
+ This action schedules the migration for retry. Restart the worker
+ containers to re-initiate the migration.
+ </p>
+ <div className="flex justify-end space-x-4">
+ <Button
+ type="button"
+ variant="default"
+ loading={mutRetryBackgroundMigration.isLoading}
+ onClick={() => {
+ void mutRetryBackgroundMigration.mutateAsync({
+ name: backgroundMigrationName,
+ });
+ setIsOpen(false);
+ }}
+ >
+ Retry Background Migration
+ </Button>
+ </div>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/web/src/features/background-migrations/server/background-migrations-router.ts b/web/src/features/background-migrations/server/background-migrations-router.ts
new file mode 100644
index 0000000000..de73b283bc
--- /dev/null
+++ b/web/src/features/background-migrations/server/background-migrations-router.ts
@@ -0,0 +1,67 @@
+import { z } from "zod";
+import { createTRPCRouter, protectedProcedure } from "@/src/server/api/trpc";
+import { TRPCError } from "@trpc/server";
+
+export const backgroundMigrationsRouter = createTRPCRouter({
+ all: protectedProcedure.query(async ({ ctx }) => {
+ const backgroundMigrations = await ctx.prisma.backgroundMigration.findMany({
+ orderBy: {
+ name: "asc",
+ },
+ });
+
+ return { migrations: backgroundMigrations };
+ }),
+ status: protectedProcedure.query(async ({ ctx }) => {
+ const backgroundMigrations = await ctx.prisma.backgroundMigration.findMany({
+ orderBy: {
+ name: "asc",
+ },
+ });
+
+ if (backgroundMigrations.some((m) => m.failedAt !== null)) {
+ return { status: "FAILED" };
+ }
+
+ if (
+ backgroundMigrations.some(
+ (m) => m.finishedAt === null && m.failedAt === null,
+ )
+ ) {
+ return { status: "ACTIVE" };
+ }
+
+ return { status: "FINISHED" };
+ }),
+ retry: protectedProcedure
+ .input(z.object({ name: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ const backgroundMigration =
+ await ctx.prisma.backgroundMigration.findUnique({
+ where: {
+ name: input.name,
+ },
+ });
+
+ if (!backgroundMigration) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Background migration not found",
+ });
+ }
+
+ await ctx.prisma.backgroundMigration.update({
+ where: {
+ name: input.name,
+ },
+ data: {
+ state: {},
+ failedAt: null,
+ failedReason: null,
+ finishedAt: null,
+ },
+ });
+
+ return { backgroundMigration };
+ }),
+});
diff --git a/web/src/pages/background-migrations.tsx b/web/src/pages/background-migrations.tsx
new file mode 100644
index 0000000000..510d407715
--- /dev/null
+++ b/web/src/pages/background-migrations.tsx
@@ -0,0 +1,5 @@
+import BackgroundMigrationsTable from "@/src/features/background-migrations/components/background-migrations";
+
+export default function BackgroundMigrationsPage() {
+ return <BackgroundMigrationsTable />;
+}
diff --git a/web/src/server/api/root.ts b/web/src/server/api/root.ts
index 5719f029e6..93a7420155 100644
--- a/web/src/server/api/root.ts
+++ b/web/src/server/api/root.ts
@@ -28,6 +28,7 @@ import { queueRouter } from "@/src/ee/features/annotation-queues/server/annotati
import { queueItemRouter } from "@/src/ee/features/annotation-queues/server/annotationQueueItems";
import { experimentsRouter } from "@/src/ee/features/experiments/server/router";
import { mediaRouter } from "@/src/server/api/routers/media";
+import { backgroundMigrationsRouter } from "@/src/features/background-migrations/server/background-migrations-router";
/**
* This is the primary router for your server.
@@ -64,6 +65,7 @@ export const appRouter = createTRPCRouter({
uiCustomization: uiCustomizationRouter,
comments: commentsRouter,
media: mediaRouter,
+ backgroundMigrations: backgroundMigrationsRouter,
});
// export type definition of API