feat(web): options to disable machine

This commit is contained in:
BennyKok
2023-12-21 13:39:36 +08:00
parent eda922a5e3
commit 61d3e2f65a
10 changed files with 744 additions and 55 deletions
+2 -11
View File
@@ -15,7 +15,7 @@ async function MachineListServer() {
return <div>No auth</div>;
}
const workflow = await db.query.machinesTable.findMany({
const machines = await db.query.machinesTable.findMany({
orderBy: desc(machinesTable.updated_at),
where: eq(machinesTable.user_id, userId),
});
@@ -23,16 +23,7 @@ async function MachineListServer() {
return (
<div className="w-full">
{/* <div>Machines</div> */}
<MachineList
data={workflow.map((x) => {
return {
id: x.id,
name: x.name,
date: x.updated_at,
endpoint: x.endpoint,
};
})}
/>
<MachineList data={machines} />
</div>
);
}
+36 -11
View File
@@ -11,6 +11,7 @@ import {
FormMessage,
Form,
} from "./ui/form";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
@@ -38,7 +39,13 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table";
import { addMachine, deleteMachine } from "@/server/curdMachine";
import type { MachineType } from "@/db/schema";
import {
addMachine,
deleteMachine,
disableMachine,
enableMachine,
} from "@/server/curdMachine";
import { zodResolver } from "@hookform/resolvers/zod";
import type {
ColumnDef,
@@ -59,12 +66,7 @@ import * as React from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
export type Machine = {
id: string;
name: string;
endpoint: string;
date: Date;
};
export type Machine = MachineType;
export const columns: ColumnDef<Machine>[] = [
{
@@ -106,7 +108,12 @@ export const columns: ColumnDef<Machine>[] = [
cell: ({ row }) => {
return (
// <a className="hover:underline" href={`/${row.original.id}`}>
row.getValue("name")
<div className="flex flex-row gap-2">
<div>{row.getValue("name")}</div>
{row.original.disabled && (
<Badge variant="destructive">Disabled</Badge>
)}
</div>
// </a>
);
},
@@ -137,7 +144,7 @@ export const columns: ColumnDef<Machine>[] = [
},
cell: ({ row }) => (
<div className="capitalize text-right">
{getRelativeTime(row.original.date)}
{getRelativeTime(row.original.updated_at)}
</div>
),
},
@@ -146,7 +153,7 @@ export const columns: ColumnDef<Machine>[] = [
id: "actions",
enableHiding: false,
cell: ({ row }) => {
const workflow = row.original;
const machine = row.original;
return (
<DropdownMenu>
@@ -161,11 +168,29 @@ export const columns: ColumnDef<Machine>[] = [
<DropdownMenuItem
className="text-destructive"
onClick={async () => {
callServerPromise(deleteMachine(workflow.id));
callServerPromise(deleteMachine(machine.id));
}}
>
Delete Machine
</DropdownMenuItem>
{machine.disabled ? (
<DropdownMenuItem
onClick={async () => {
callServerPromise(enableMachine(machine.id));
}}
>
Enable Machine
</DropdownMenuItem>
) : (
<DropdownMenuItem
className="text-destructive"
onClick={async () => {
callServerPromise(disableMachine(machine.id));
}}
>
Disable Machine
</DropdownMenuItem>
)}
{/* <DropdownMenuSeparator />
<DropdownMenuItem>View customer</DropdownMenuItem>
<DropdownMenuItem>View payment details</DropdownMenuItem> */}
+2
View File
@@ -176,6 +176,7 @@ export const machinesTable = dbSchema.table("machines", {
endpoint: text("endpoint").notNull(),
created_at: timestamp("created_at").defaultNow().notNull(),
updated_at: timestamp("updated_at").defaultNow().notNull(),
disabled: boolean("disabled").default(false).notNull(),
});
export const deploymentsTable = dbSchema.table("deployments", {
@@ -227,3 +228,4 @@ export const apiKeyTable = dbSchema.table("api_keys", {
export type UserType = InferSelectModel<typeof usersTable>;
export type WorkflowType = InferSelectModel<typeof workflowTable>;
export type MachineType = InferSelectModel<typeof machinesTable>;
+14 -10
View File
@@ -1,12 +1,12 @@
"use server";
import { withServerPromise } from "./withServerPromise";
import { db } from "@/db/db";
import { workflowRunsTable } from "@/db/schema";
import { machinesTable, workflowRunsTable } from "@/db/schema";
import { ComfyAPI_Run } from "@/types/ComfyAPI_Run";
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import "server-only";
import { withServerPromise } from "./withServerPromise";
export const createRun = withServerPromise(
async (
@@ -14,20 +14,24 @@ export const createRun = withServerPromise(
workflow_version_id: string,
machine_id: string,
inputs?: Record<string, string>,
isManualRun?: boolean,
isManualRun?: boolean
) => {
const machine = await db.query.machinesTable.findFirst({
where: eq(workflowRunsTable.id, machine_id),
where: and(
eq(machinesTable.id, machine_id),
eq(machinesTable.disabled, false)
),
});
if (!machine) {
throw new Error("Machine not found");
}
const workflow_version_data =
await db.query.workflowVersionTable.findFirst({
const workflow_version_data = await db.query.workflowVersionTable.findFirst(
{
where: eq(workflowRunsTable.id, workflow_version_id),
});
}
);
if (!workflow_version_data) {
throw new Error("Workflow version not found");
@@ -79,7 +83,7 @@ export const createRun = withServerPromise(
workflow_version_id: workflow_version_data.id,
workflow_inputs: inputs,
machine_id,
origin: isManualRun ? "manual" : "api"
origin: isManualRun ? "manual" : "api",
})
.returning();
@@ -89,5 +93,5 @@ export const createRun = withServerPromise(
workflow_run_id: workflow_run[0].id,
message: "Successful workflow run",
};
},
}
);
+38 -20
View File
@@ -1,30 +1,22 @@
"use server";
import { withServerPromise } from "./withServerPromise";
import { db } from "@/db/db";
import { machinesTable } from "@/db/schema";
import { auth } from "@clerk/nextjs";
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import "server-only";
// export async function addMachine(form: FormData) {
// const name = form.get("name") as string;
// const endpoint = form.get("endpoint") as string;
// await db.insert(machinesTable).values({
// name,
// endpoint,
// });
// revalidatePath("/machines");
// }
export async function getMachines() {
const { userId } = auth();
if (!userId) throw new Error("No user id");
const machines = await db
.select()
.from(machinesTable)
.where(eq(machinesTable.user_id, userId));
.where(
and(eq(machinesTable.user_id, userId), eq(machinesTable.disabled, false))
);
return machines;
}
@@ -40,10 +32,36 @@ export async function addMachine(name: string, endpoint: string) {
revalidatePath("/machines");
}
export async function deleteMachine(
machine_id: string
): Promise<{ message: string; error?: boolean }> {
await db.delete(machinesTable).where(eq(machinesTable.id, machine_id));
revalidatePath("/machines");
return { message: "Machine Deleted" };
}
export const deleteMachine = withServerPromise(
async (machine_id: string): Promise<{ message: string }> => {
await db.delete(machinesTable).where(eq(machinesTable.id, machine_id));
revalidatePath("/machines");
return { message: "Machine Deleted" };
}
);
export const disableMachine = withServerPromise(
async (machine_id: string): Promise<{ message: string }> => {
await db
.update(machinesTable)
.set({
disabled: true,
})
.where(eq(machinesTable.id, machine_id));
revalidatePath("/machines");
return { message: "Machine Disabled" };
}
);
export const enableMachine = withServerPromise(
async (machine_id: string): Promise<{ message: string }> => {
await db
.update(machinesTable)
.set({
disabled: false,
})
.where(eq(machinesTable.id, machine_id));
revalidatePath("/machines");
return { message: "Machine Enabled" };
}
);
+1 -2
View File
@@ -5,9 +5,8 @@ import { workflowTable } from "@/db/schema";
import { eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import "server-only";
export async function deleteWorkflow(workflow_id: string) {
await db.delete(workflowTable).where(eq(workflowTable.id, workflow_id));
revalidatePath("/");
}
+1 -1
View File
@@ -7,6 +7,6 @@ export async function wrapServerPromise<T>(result: Promise<T>) {
}
export function withServerPromise<T extends (...args: any[]) => Promise<any>>(
fn: T
): (...args: Parameters<T>) => Promise<ReturnType<T> | { error: string; }> {
): (...args: Parameters<T>) => Promise<ReturnType<T> | { error: string }> {
return (...args: Parameters<T>) => wrapServerPromise(fn(...args));
}