重构:将 "lobster" 重命名为 "claw" 并添加国际化支持 (i18n)
This commit is contained in:
73
app/api/v1/claws/route.ts
Normal file
73
app/api/v1/claws/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { eq, desc, sql, and } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
import { claws, tasks } from "@/lib/db/schema";
|
||||
import { getActiveClawIds } from "@/lib/redis";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const limitParam = parseInt(searchParams.get("limit") ?? "50", 10);
|
||||
const limit = Math.min(Math.max(1, limitParam), 200);
|
||||
const region = searchParams.get("region");
|
||||
|
||||
const conditions = [];
|
||||
if (region) {
|
||||
conditions.push(eq(claws.region, region));
|
||||
}
|
||||
|
||||
const whereClause = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
|
||||
const clawRows = await db
|
||||
.select()
|
||||
.from(claws)
|
||||
.where(whereClause)
|
||||
.orderBy(desc(claws.lastHeartbeat))
|
||||
.limit(limit);
|
||||
|
||||
const totalResult = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(claws)
|
||||
.where(whereClause);
|
||||
const total = totalResult[0]?.count ?? 0;
|
||||
|
||||
const activeClawIds = await getActiveClawIds();
|
||||
const activeSet = new Set(activeClawIds);
|
||||
|
||||
const clawList = await Promise.all(
|
||||
clawRows.map(async (claw) => {
|
||||
const latestTaskRows = await db
|
||||
.select({
|
||||
summary: tasks.summary,
|
||||
timestamp: tasks.timestamp,
|
||||
durationMs: tasks.durationMs,
|
||||
})
|
||||
.from(tasks)
|
||||
.where(eq(tasks.clawId, claw.id))
|
||||
.orderBy(desc(tasks.timestamp))
|
||||
.limit(1);
|
||||
|
||||
const lastTask = latestTaskRows[0] ?? null;
|
||||
|
||||
return {
|
||||
id: claw.id,
|
||||
name: claw.name,
|
||||
model: claw.model,
|
||||
platform: claw.platform,
|
||||
city: claw.city,
|
||||
country: claw.country,
|
||||
isOnline: activeSet.has(claw.id),
|
||||
lastTask,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return NextResponse.json({ claws: clawList, total });
|
||||
} catch (error) {
|
||||
console.error("Claws error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user