diff --git a/CHANGELOG.md b/CHANGELOG.md index dbedcd0e..ce276714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Anything that changes what the dashboard, the admin or a widget can do gets a li ### Changed - **Clock in the compact bar.** Show an accent-coloured clock icon beside the time, matching the weather icon. +- **The system tiles show useful Mac capacity.** Disk usage uses the APFS data volume and shows available space; memory shows available RAM and swap. - **Homey is a declared connection now.** The Homey connection type built into Fremkit is gone: both Homey widgets bring their own, so one API key serves the two of them. An existing Homey connection is migrated when the server starts — same name, same address, same key — and the @@ -25,6 +26,7 @@ Anything that changes what the dashboard, the admin or a widget can do gets a li ### Added - **Local Docker and AI models.** Registry widgets can show up to 200 local containers and observed Ollama or MLX models. Fremkit refuses remote Docker contexts, distinguishes a missing CLI from a stopped daemon, and keeps the container list when stats fail. +- **The Uptime tile shows time since boot.** The reading comes from macOS and remains unavailable when the system cannot provide it. - **Borrow the Edge display.** Hide or show the dashboard from the helper menu without quitting it. Hidden state survives display rearrangements, and the mouse fence pauses while the Edge is borrowed. Touch and data collection continue; the dashboard reappears after a helper restart. - **Claude sessions: go to the session.** Touch a card and its window comes forward — the exact Orca pane, the Terminal.app or iTerm2 tab, the VS Code window on that folder, or at diff --git a/README.md b/README.md index 6cc37933..d8e88269 100644 --- a/README.md +++ b/README.md @@ -232,8 +232,9 @@ above state lines such as "Server: running" and "Touch: active". | `clipboard` | Recent clipboard entries, tap to copy back | — | — | | `clock` | Time, date and other cities | — | ● | | `cpu` | CPU load and temperature | — | ● | -| `disk` | Disk usage | — | — | -| `memory` | Memory usage | — | ● | +| `disk` | Disk usage and available space | — | — | +| `memory` | Memory usage, available RAM and swap | — | ● | +| `uptime` | Time since the Mac last booted | — | — | | `network` | Live network throughput, with a sparkline | — | ● | | `notifications` | Unread badges read from the Dock | — | — | | `pomodoro` | A work and break timer, driven by touch | — | ● | diff --git a/docs/widgets.md b/docs/widgets.md index 380db618..48e47907 100644 --- a/docs/widgets.md +++ b/docs/widgets.md @@ -16,8 +16,9 @@ To write your own, see [writing-widgets.md](writing-widgets.md). | [clipboard](#clipboard) | Recent clipboard entries, tap to copy back | — | — | 16 × 8 | | [clock](#clock) | Time, date and other cities | — | 5 cells | 16 × 4 | | [cpu](#cpu) | CPU load and temperature | — | 4 cells | 8 × 4 | -| [disk](#disk) | Disk usage | — | — | 16 × 4 | -| [memory](#memory) | Memory usage | — | 4 cells | 8 × 4 | +| [disk](#disk) | Disk usage and available space | — | — | 16 × 4 | +| [memory](#memory) | Memory usage, available RAM and swap | — | 4 cells | 8 × 4 | +| [uptime](#uptime) | Time since the Mac last booted | — | — | 8 × 4 | | [network](#network) | Live network throughput | — | 6 cells | 12 × 4 | | [notifications](#notifications) | Unread badges read from the Dock | — | — | 16 × 4 | | [pomodoro](#pomodoro) | A work and break timer | — | 5 cells | 8 × 6 | @@ -172,16 +173,22 @@ Minimum size 8 × 4, default 8 × 4. Compact width 4 cells. ## disk -*Disks* — usage of the startup disk and of every volume mounted under `/Volumes`. +*Disks* shows used and available capacity of the Mac disk and volumes under `/Volumes`. On APFS it prefers the data volume to avoid presenting the sealed system volume as the usable disk. The Mac disk uses container size minus reported available space so its numbers add up. System disk readings omit purgeable capacity, so free space may be lower than Finder shows. Minimum size 8 × 4, default 16 × 4. No settings. ## memory -*Memory* — memory usage. +*Memory* shows used and available RAM plus swap usage when the system reports it. Minimum size 8 × 4, default 8 × 4. Compact width 4 cells. No settings. +## uptime + +*Uptime* shows the time since the Mac last booted in days, hours and minutes. If the system cannot provide a reading, the widget shows it as unavailable. + +Minimum size 8 × 4, default 8 × 4. No settings. + ## network *Network* — live throughput of the interface the default route goes out of: the two rates as big diff --git a/server/src/providers/system.ts b/server/src/providers/system.ts index 04cd2c6f..48c82698 100644 --- a/server/src/providers/system.ts +++ b/server/src/providers/system.ts @@ -1,8 +1,50 @@ import si from 'systeminformation' +import { uptime } from 'node:os' import type { Provider } from './types.js' const round = (n: number) => Math.round(n * 10) / 10 +interface DiskReading { + mount: string + size: number + used: number + available?: number + use: number +} + +/** APFS shares capacity: the data volume represents the useful Mac disk. */ +export function normalizeDisks(disks: DiskReading[]) { + const data = disks.find((disk) => disk.mount === '/System/Volumes/Data') + return disks + .filter((disk) => (data ? disk === data : disk.mount === '/') + || disk.mount.startsWith('/Volumes/')) + .map((disk) => { + const available = typeof disk.available === 'number' + && Number.isFinite(disk.available) && disk.available >= 0 + ? disk.available : null + const isMacDisk = disk === data + const used = isMacDisk && available !== null ? Math.max(0, disk.size - available) : disk.used + return { + mount: isMacDisk ? '/' : disk.mount, + size: disk.size, + used, + available, + use: round(isMacDisk && available !== null && disk.size > 0 + ? used / disk.size * 100 : disk.use), + } + }) +} + +/** A denied uptime reading must not suppress CPU, memory or disk metrics. */ +function uptimeSeconds(): number | null { + try { + const seconds = uptime() + return Number.isFinite(seconds) && seconds >= 0 ? Math.floor(seconds) : null + } catch { + return null + } +} + export const systemProvider: Provider = { channel: 'system', intervalMs: 2000, @@ -11,11 +53,10 @@ export const systemProvider: Provider = { si.currentLoad(), si.mem(), si.fsSize(), si.networkStats(), si.cpuTemperature(), ]) return { + uptimeSeconds: uptimeSeconds(), cpu: { load: round(load.currentLoad), cores: load.cpus.map((c) => round(c.load)), temp: temp.main > 0 ? round(temp.main) : null }, - memory: { total: mem.total, used: mem.active, free: mem.available }, - disks: fs - .filter((d) => d.mount === '/' || d.mount.startsWith('/Volumes/')) - .map((d) => ({ mount: d.mount, size: d.size, used: d.used, use: round(d.use) })), + memory: { total: mem.total, used: mem.active, free: mem.available, swapUsed: mem.swapused, swapTotal: mem.swaptotal }, + disks: normalizeDisks(fs), network: net.map((n) => ({ iface: n.iface, rx: Math.max(0, Math.round(n.rx_sec ?? 0)), tx: Math.max(0, Math.round(n.tx_sec ?? 0)) })), } }, diff --git a/server/test/system-disks.test.ts b/server/test/system-disks.test.ts new file mode 100644 index 00000000..e9d58641 --- /dev/null +++ b/server/test/system-disks.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from 'vitest' +import { normalizeDisks } from '../src/providers/system.js' + +describe('Mac disk availability', () => { + it('prefers APFS Data and reconciles used and available bytes', () => { + const disks = normalizeDisks([ + { mount: '/', size: 1000, used: 50, available: 300, use: 10 }, + { mount: '/System/Volumes/Data', size: 1000, used: 650, available: 300, use: 68 }, + { mount: '/Volumes/External', size: 500, used: 10, available: 450, use: 2 }, + ]) + expect(disks).toEqual([ + { mount: '/', size: 1000, used: 700, available: 300, use: 70 }, + { mount: '/Volumes/External', size: 500, used: 10, available: 450, use: 2 }, + ]) + }) + + it('leaves unreported free space unknown', () => { + expect(normalizeDisks([{ mount: '/', size: 1000, used: 50, use: 5 }])[0].available).toBeNull() + }) +}) diff --git a/server/test/system-uptime.test.ts b/server/test/system-uptime.test.ts new file mode 100644 index 00000000..8fea6034 --- /dev/null +++ b/server/test/system-uptime.test.ts @@ -0,0 +1,35 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { uptime } from 'node:os' +import { systemProvider } from '../src/providers/system.js' + +vi.mock('node:os', () => ({ uptime: vi.fn() })) +vi.mock('systeminformation', () => ({ default: { + currentLoad: vi.fn(async () => ({ currentLoad: 12, cpus: [{ load: 12 }] })), + mem: vi.fn(async () => ({ total: 100, active: 50, available: 50, swapused: 0, swaptotal: 0 })), + fsSize: vi.fn(async () => []), + networkStats: vi.fn(async () => []), + cpuTemperature: vi.fn(async () => ({ main: 0 })), +} })) + +describe('OS uptime in the system snapshot', () => { + beforeEach(() => vi.mocked(uptime).mockReset()) + + it.each([0, 90061.9])('publishes system uptime %s seconds rather than process uptime', async (seconds) => { + vi.mocked(uptime).mockReturnValue(seconds) + expect(await systemProvider.poll!()).toMatchObject({ + uptimeSeconds: Math.floor(seconds), cpu: { load: 12 }, memory: { total: 100 }, + }) + }) + + it.each([-1, NaN, Infinity])('leaves invalid reading %s unknown', async (seconds) => { + vi.mocked(uptime).mockReturnValue(seconds) + expect(await systemProvider.poll!()).toMatchObject({ uptimeSeconds: null, cpu: { load: 12 } }) + }) + + it('keeps other metrics working when uptime access fails', async () => { + vi.mocked(uptime).mockImplementationOnce(() => { throw new Error('Permission denied') }) + expect(await systemProvider.poll!()).toMatchObject({ + uptimeSeconds: null, cpu: { load: 12 }, disks: [], network: [], + }) + }) +}) diff --git a/server/test/uptime-widget.test.ts b/server/test/uptime-widget.test.ts new file mode 100644 index 00000000..1e3dbf25 --- /dev/null +++ b/server/test/uptime-widget.test.ts @@ -0,0 +1,91 @@ +import { readFileSync } from 'node:fs' +import { runInNewContext } from 'node:vm' +import { describe, expect, it } from 'vitest' + +interface UptimeReading { + uptimeSeconds?: number | null + error?: string +} + +function loadWidget(locale = 'fr') { + const html = readFileSync(new URL('../../widgets/uptime/index.html', import.meta.url), 'utf8') + const script = / diff --git a/widgets/uptime/manifest.json b/widgets/uptime/manifest.json new file mode 100644 index 00000000..008305c2 --- /dev/null +++ b/widgets/uptime/manifest.json @@ -0,0 +1,6 @@ +{ "id": "uptime", + "name": { "fr": "Uptime", "en": "Uptime" }, + "version": "1.0.0", + "description": { "fr": "Durée depuis le dernier démarrage du système", "en": "Time since the last system boot" }, + "category": "system", + "icon": "timer", "minSize": [8, 4], "defaultSize": [8, 4], "subscriptions": ["system"] }