chore: updated stats 145e7d9e
Steve · 2025-11-13 21:01 1 file(s) · +52 −21
src/stats.ts +52 −21
7 7
		// OS Name
8 8
		const osName = await $`uname -s`.text();
9 9
		stats.os = osName.trim();
10 +
		const isLinux = stats.os === "Linux";
10 11
11 12
		// OS Version
12 13
		const osVersion = await $`uname -r`.text();
17 18
		stats.arch = arch.trim();
18 19
19 20
		// Uptime
20 -
		const uptimeSeconds = await $`sysctl -n kern.boottime`.text();
21 -
		const bootMatch = uptimeSeconds.match(/sec = (\d+)/);
22 -
		if (bootMatch) {
23 -
			const bootTime = Number.parseInt(bootMatch[1]);
24 -
			const now = Math.floor(Date.now() / 1000);
25 -
			const uptime = now - bootTime;
26 -
			const days = Math.floor(uptime / 86400);
27 -
			const hours = Math.floor((uptime % 86400) / 3600);
28 -
			const minutes = Math.floor((uptime % 3600) / 60);
21 +
		if (isLinux) {
22 +
			const uptimeData = await $`cat /proc/uptime`.text();
23 +
			const uptimeSeconds = Math.floor(
24 +
				Number.parseFloat(uptimeData.split(" ")[0]),
25 +
			);
26 +
			const days = Math.floor(uptimeSeconds / 86400);
27 +
			const hours = Math.floor((uptimeSeconds % 86400) / 3600);
28 +
			const minutes = Math.floor((uptimeSeconds % 3600) / 60);
29 29
			stats.uptime = `${days}d ${hours}h ${minutes}m`;
30 +
		} else {
31 +
			const uptimeSeconds = await $`sysctl -n kern.boottime`.text();
32 +
			const bootMatch = uptimeSeconds.match(/sec = (\d+)/);
33 +
			if (bootMatch) {
34 +
				const bootTime = Number.parseInt(bootMatch[1]);
35 +
				const now = Math.floor(Date.now() / 1000);
36 +
				const uptime = now - bootTime;
37 +
				const days = Math.floor(uptime / 86400);
38 +
				const hours = Math.floor((uptime % 86400) / 3600);
39 +
				const minutes = Math.floor((uptime % 3600) / 60);
40 +
				stats.uptime = `${days}d ${hours}h ${minutes}m`;
41 +
			}
30 42
		}
31 43
32 44
		// CPU Info
33 -
		const cpuBrand = await $`sysctl -n machdep.cpu.brand_string`.text();
34 -
		stats.cpu = cpuBrand.trim();
45 +
		if (isLinux) {
46 +
			const cpuInfo = await $`grep "model name" /proc/cpuinfo`.text();
47 +
			const firstCpu = cpuInfo.split("\n")[0];
48 +
			const cpuBrand = firstCpu.split(":")[1]?.trim() || "Unknown";
49 +
			stats.cpu = cpuBrand;
50 +
		} else {
51 +
			const cpuBrand = await $`sysctl -n machdep.cpu.brand_string`.text();
52 +
			stats.cpu = cpuBrand.trim();
53 +
		}
35 54
36 55
		// CPU Cores
37 -
		const cpuCores = await $`sysctl -n hw.ncpu`.text();
38 -
		stats.cores = cpuCores.trim();
56 +
		if (isLinux) {
57 +
			const cpuCores = await $`nproc`.text();
58 +
			stats.cores = cpuCores.trim();
59 +
		} else {
60 +
			const cpuCores = await $`sysctl -n hw.ncpu`.text();
61 +
			stats.cores = cpuCores.trim();
62 +
		}
39 63
40 64
		// Memory
41 -
		const memBytes = await $`sysctl -n hw.memsize`.text();
42 -
		const memGB = (
43 -
			Number.parseInt(memBytes.trim()) /
44 -
			1024 /
45 -
			1024 /
46 -
			1024
47 -
		).toFixed(1);
48 -
		stats.memory = `${memGB} GB`;
65 +
		if (isLinux) {
66 +
			const memInfo = await $`grep MemTotal /proc/meminfo`.text();
67 +
			const memKB = Number.parseInt(memInfo.split(/\s+/)[1]);
68 +
			const memGB = (memKB / 1024 / 1024).toFixed(1);
69 +
			stats.memory = `${memGB} GB`;
70 +
		} else {
71 +
			const memBytes = await $`sysctl -n hw.memsize`.text();
72 +
			const memGB = (
73 +
				Number.parseInt(memBytes.trim()) /
74 +
				1024 /
75 +
				1024 /
76 +
				1024
77 +
			).toFixed(1);
78 +
			stats.memory = `${memGB} GB`;
79 +
		}
49 80
50 81
		// Shell
51 82
		const shell = process.env.SHELL || "unknown";