Quantumbraid's picture
download
raw
2.39 kB
import { NextResponse } from 'next/server';
import si from 'systeminformation';
import os from 'os';
import { CpuInfo } from '@/types';
import { cached } from '@/server/apiCache';
import { loadMacstats } from '@/server/macstats';
const isMac = os.platform() === 'darwin';
async function getCpuInfo(): Promise<CpuInfo> {
const cpuInfoRaw = await si.cpu();
let cpuInfo: CpuInfo;
if (isMac) {
try {
const ms = loadMacstats();
if (!ms) throw new Error('macstats unavailable');
const ramData = ms.getRAMUsageSync();
const cpuData = ms.getCpuDataSync();
cpuInfo = {
name: `${cpuInfoRaw.manufacturer} ${cpuInfoRaw.brand}`,
cores: cpuInfoRaw.cores,
temperature: cpuData.temperature || 0,
totalMemory: ramData.total / (1024 * 1024),
availableMemory: ramData.free / (1024 * 1024),
freeMemory: ramData.free / (1024 * 1024),
currentLoad: (await si.currentLoad()).currentLoad || 0,
};
} catch {
// Fallback to systeminformation if macstats fails
const memoryData = await si.mem();
cpuInfo = {
name: `${cpuInfoRaw.manufacturer} ${cpuInfoRaw.brand}`,
cores: cpuInfoRaw.cores,
temperature: (await si.cpuTemperature()).main || 0,
totalMemory: memoryData.total / (1024 * 1024),
availableMemory: memoryData.available / (1024 * 1024),
freeMemory: memoryData.free / (1024 * 1024),
currentLoad: (await si.currentLoad()).currentLoad || 0,
};
}
} else {
const memoryData = await si.mem();
cpuInfo = {
name: `${cpuInfoRaw.manufacturer} ${cpuInfoRaw.brand}`,
cores: cpuInfoRaw.cores,
temperature: (await si.cpuTemperature()).main || 0,
totalMemory: memoryData.total / (1024 * 1024),
availableMemory: memoryData.available / (1024 * 1024),
freeMemory: memoryData.free / (1024 * 1024),
currentLoad: (await si.currentLoad()).currentLoad || 0,
};
}
return cpuInfo;
}
export async function GET() {
try {
const cpuInfo = await cached('cpu-info', getCpuInfo);
return NextResponse.json(cpuInfo);
} catch (error) {
console.error('Error fetching CPU stats:', error);
return NextResponse.json(
{
error: `Failed to fetch CPU stats: ${error instanceof Error ? error.message : String(error)}`,
},
{ status: 500 },
);
}
}

Xet Storage Details

Size:
2.39 kB
·
Xet hash:
3f13d1713eedd4151bfc545abbe7354c851fdfe3257e957dccb2f486d6c36028

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.