import { NextResponse } from "next/server"; import { getDeviceCommandCounts, getDeviceUsageStats } from "@/lib/db/queries/stats"; import { withRouteLogging } from "@/lib/api/with-route-logging"; export const GET = withRouteLogging("stats.devices", async () => { const [usage, commandCounts] = await Promise.all([getDeviceUsageStats(), getDeviceCommandCounts()]); const commandCountByDevice = new Map(commandCounts.map((c) => [c.deviceId, c.commandCount])); const devices = usage.map((d) => ({ ...d, commandCount: commandCountByDevice.get(d.deviceId) ?? 0 })); return NextResponse.json({ devices }); });