feat: add deployment endpoint

This commit is contained in:
BennyKok
2023-12-14 22:36:57 +08:00
parent 0835d966f1
commit 7e05fae7b3
29 changed files with 1776 additions and 190 deletions
+46
View File
@@ -0,0 +1,46 @@
"use server";
import { db } from "@/db/db";
import { deploymentsTable } from "@/db/schema";
import { auth } from "@clerk/nextjs";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import "server-only";
export async function createDeployments(
workflow_id: string,
version_id: string,
machine_id: string,
environment: "production" | "staging"
) {
const { userId } = auth();
if (!userId) throw new Error("No user id");
// Same environment and same workflow
const existingDeployment = await db.query.deploymentsTable.findFirst({
where: and(
eq(deploymentsTable.workflow_id, workflow_id),
eq(deploymentsTable.environment, environment)
),
});
if (existingDeployment) {
await db
.update(deploymentsTable)
.set({
workflow_id,
workflow_version_id: version_id,
machine_id,
})
.where(eq(deploymentsTable.id, existingDeployment.id));
} else {
await db.insert(deploymentsTable).values({
user_id: userId,
workflow_id,
workflow_version_id: version_id,
machine_id,
environment,
});
}
revalidatePath(`/${workflow_id}`);
}
+3 -10
View File
@@ -43,14 +43,7 @@ export async function addMachine(name: string, endpoint: string) {
export async function deleteMachine(
machine_id: string
): Promise<{ message: string; error?: boolean }> {
try {
await db.delete(machinesTable).where(eq(machinesTable.id, machine_id));
revalidatePath("/machines");
return { message: "Machine Deleted" };
} catch (error: unknown) {
return {
message: `Error: ${error.detail}`,
error: true,
};
}
await db.delete(machinesTable).where(eq(machinesTable.id, machine_id));
revalidatePath("/machines");
return { message: "Machine Deleted" };
}
+20 -1
View File
@@ -1,5 +1,5 @@
import { db } from "@/db/db";
import { workflowRunsTable } from "@/db/schema";
import { deploymentsTable, workflowRunsTable } from "@/db/schema";
import { desc, eq } from "drizzle-orm";
export async function findAllRuns(workflow_id: string) {
@@ -21,3 +21,22 @@ export async function findAllRuns(workflow_id: string) {
},
});
}
export async function findAllDeployments(workflow_id: string) {
return await db.query.deploymentsTable.findMany({
where: eq(deploymentsTable.workflow_id, workflow_id),
orderBy: desc(deploymentsTable.environment),
with: {
machine: {
columns: {
name: true,
},
},
version: {
columns: {
version: true,
},
},
},
});
}
+16 -1
View File
@@ -1,12 +1,27 @@
"use server";
import { db } from "@/db/db";
import { workflowRunOutputs } from "@/db/schema";
import { workflowRunOutputs, workflowRunsTable } from "@/db/schema";
import { eq } from "drizzle-orm";
export async function getRunsOutput(run_id: string) {
// throw new Error("Not implemented");
return await db
.select()
.from(workflowRunOutputs)
.where(eq(workflowRunOutputs.run_id, run_id));
}
export async function getRunsData(run_id: string) {
// throw new Error("Not implemented");
return await db.query.workflowRunsTable.findFirst({
where: eq(workflowRunsTable.id, run_id),
with: {
outputs: {
columns: {
data: true,
},
},
},
});
}
+1 -1
View File
@@ -17,7 +17,7 @@ const s3Client = new S3({
forcePathStyle: true,
});
function replaceCDNUrl(url: string) {
export function replaceCDNUrl(url: string) {
url = url.replace(
process.env.SPACES_ENDPOINT!,
process.env.SPACES_ENDPOINT_CDN!