- 替换 react-simple-maps/d3-geo/topojson-client 为 react-map-gl + maplibre-gl - 使用 CARTO dark-matter 免费暗色瓦片,自带国家/城市名标注 - 基于 locale 动态切换地图标注语言(name:zh / name_en) - MapLibre 原生 heatmap + circle 双层渲染替代 SVG 热力图 - 提取 MapPopup 组件,配合 react-map-gl Popup 实现点击弹窗 - continent page 改为 dynamic import (ssr: false) - dev 模式去掉 Turbopack 以兼容 maplibre-gl - 删除 heatmap-layer.tsx 和 react-simple-maps 类型声明
76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
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 sort = searchParams.get("sort") ?? "activity";
|
|
|
|
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(sort === "newest" ? desc(claws.createdAt) : 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),
|
|
createdAt: claw.createdAt,
|
|
lastTask,
|
|
};
|
|
})
|
|
);
|
|
|
|
return NextResponse.json({ claws: clawList, total });
|
|
} catch (error) {
|
|
console.error("Claws error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|