Laboratorio domestico
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
// The pretend home server for the demo of homelab (../../demo-kit/README.md): the same answers the program
// gives (../server/homelab_panel.py), with invented values. Every name in here is made up. Unlike the program's
// default, the demo allows restart, stop and start: nothing real is behind them.
(function () {
var GB = 1073741824;
var MB = 1048576;
var KB = 1024;
var CORES = 4;
var MEMORY_TOTAL = 8 * GB;
var HOST_BOOTED = Date.now() - (23 * 86400 + 6 * 3600 + 41 * 60) * 1000;
var RESTART_MS = 3000;
// photo-library stops answering its health check for a while in every cycle, and recovers by itself.
var SICK_EVERY = 56;
var SICK_FROM = 24;
var SICK_FOR = 15;
var ctx = czDemo.context;
var day = 86400000;
// cpu: percent of one core while idle-ish; swing and period: how it wanders; memory: bytes it settles at.
var services = [
{ id: '7c1e09ab52d4', name: 'reverse-proxy', image: 'homelab/reverse-proxy:2.8', ports: ['80', '443'], cpu: 1.6, swing: 0.5, period: 9, memory: 46 * MB, up: 23 * day, restarts: 0 },
{ id: '3f9a1c0b7d21', name: 'media-server', image: 'homelab/media-server:1.32', ports: ['8096'], cpu: 21, swing: 0.7, period: 17, memory: 840 * MB, up: 6.2 * day, restarts: 1 },
{ id: 'b48d2e6f1a90', name: 'photo-library', image: 'homelab/photo-library:0.9', ports: ['2342'], cpu: 7, swing: 0.6, period: 13, memory: 620 * MB, up: 2.4 * day, restarts: 4 },
{ id: '90aa3c5e77f1', name: 'home-automation', image: 'homelab/home-automation:2026.11', ports: ['8123'], cpu: 3.4, swing: 0.4, period: 11, memory: 410 * MB, up: 11.3 * day, restarts: 0 },
{ id: 'e2d7f0146c3b', name: 'git-forge', image: 'homelab/git-forge:1.22', ports: ['3000', '2222'], cpu: 1.1, swing: 0.8, period: 21, memory: 212 * MB, up: 23 * day, restarts: 0 },
{ id: '5b6c8d9e0f12', name: 'dns-filter', image: 'homelab/dns-filter:5.18', ports: ['53', '8053'], cpu: 0.7, swing: 0.5, period: 7, memory: 58 * MB, up: 23 * day, restarts: 0 },
{ id: 'a1f4e8c2d603', name: 'metrics', image: 'homelab/metrics:3.1', ports: ['9090'], cpu: 4.2, swing: 0.3, period: 15, memory: 336 * MB, up: 4.1 * day, restarts: 2 },
{ id: '0d3b5a7c9e11', name: 'backup-runner', image: 'homelab/backup-runner:0.17', ports: [], cpu: 2.5, swing: 0.5, period: 10, memory: 128 * MB, up: 0, restarts: 0, stopped: true },
].map(function (service, index) {
return {
def: service,
phase: index * 1.7,
state: service.stopped ? 'stopped' : 'running',
startedAt: Date.now() - service.up,
restarts: service.restarts,
restartUntil: 0,
skipCycle: -1,
cpu: 0,
memory: 0,
};
});
ctx.state.services = services;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function find(id) {
for (var i = 0; i < services.length; i++) if (services[i].def.id === id) return services[i];
return null;
}
function alive(service) {
return service.state === 'running' || service.state === 'unhealthy';
}
// Moves the pretend machine on to now: restarts finish, the health check fails and recovers, loads wander.
var settledAt = 0;
function settle() {
var now = Date.now();
var t = ctx.seconds();
var cycle = Math.floor(t / SICK_EVERY);
var inCycle = t % SICK_EVERY;
var sickNow = inCycle >= SICK_FROM && inCycle < SICK_FROM + SICK_FOR;
var fresh = now - settledAt >= 900;
if (fresh) settledAt = now;
services.forEach(function (service) {
var def = service.def;
if (service.state === 'restarting' && now >= service.restartUntil) {
service.state = 'running';
service.startedAt = now;
}
if (def.name === 'photo-library') {
if (service.state === 'running' && sickNow && service.skipCycle !== cycle) service.state = 'unhealthy';
else if (service.state === 'unhealthy' && !sickNow) service.state = 'running';
}
if (!fresh) return;
if (!alive(service)) {
service.cpu = 0;
service.memory = 0;
return;
}
var age = (now - service.startedAt) / 1000;
var wave = 1 + def.swing * Math.sin((t / def.period) * 2 * Math.PI + service.phase) + def.swing * 0.4 * Math.sin(t / 3.1 + service.phase * 2);
var cpu = def.cpu * wave * (0.85 + ctx.rand() * 0.3);
if (age < 8) cpu += def.cpu * 2.5 * (1 - age / 8) + 6 * (1 - age / 8); // starting up is work
if (service.state === 'unhealthy') cpu = def.cpu * 4.5 + ctx.between(0, 14); // stuck in a loop
service.cpu = clamp(cpu, 0.1, 100 * CORES);
var warm = clamp(age / 20, 0.25, 1); // memory fills up after a start
service.memory = def.memory * warm * (1 + 0.05 * Math.sin(t / 37 + service.phase)) * (service.state === 'unhealthy' ? 1.3 : 1);
});
}
function view(service) {
var running = alive(service);
return {
id: service.def.id,
name: service.def.name,
image: service.def.image,
state: service.state,
uptimeSeconds: running ? Math.floor((Date.now() - service.startedAt) / 1000) : 0,
cpuPercent: Math.round(service.cpu * 10) / 10,
memoryBytes: Math.round(service.memory),
ports: service.def.ports,
restarts: service.restarts,
};
}
function action(apply) {
return function (request) {
settle();
var service = find(decodeURIComponent(request.path.split('/')[2] || ''));
if (!service) return ctx.respond(404, { error: 'unknown-service', message: 'There is no service with this id.' });
apply(service);
return view(service);
};
}
var routes = {
'GET /load': function () {
settle();
var t = ctx.seconds();
var cpuSum = 0;
var memorySum = 0;
var media = 0;
services.forEach(function (service) {
cpuSum += service.cpu;
memorySum += service.memory;
if (service.def.name === 'media-server') media = service.cpu;
});
var cpu = clamp(2.5 + cpuSum / CORES + ctx.between(-0.6, 0.6), 0.5, 100);
var base = (cpu / 100) * CORES;
return {
hostname: 'homelab',
simulated: false,
uptimeSeconds: Math.floor((Date.now() - HOST_BOOTED) / 1000),
load: [
Math.round((base * 1.08 + 0.12 + 0.05 * Math.sin(t / 5)) * 100) / 100,
Math.round((0.52 + 0.06 * Math.sin(t / 60)) * 100) / 100,
Math.round((0.47 + 0.03 * Math.sin(t / 170)) * 100) / 100,
],
cpuPercent: Math.round(cpu * 10) / 10,
memory: { used: Math.round(1.25 * GB + memorySum), total: MEMORY_TOTAL },
disks: [
{ mount: '/', used: Math.round(21.4 * GB), total: 59 * GB },
{ mount: '/mnt/tank', used: Math.round(2.61 * 1024 * GB + t * 2.2 * MB), total: Math.round(3.58 * 1024 * GB) },
{ mount: '/mnt/backup', used: Math.round(1.52 * 1024 * GB), total: Math.round(1.79 * 1024 * GB) },
],
temperatureC: Math.round((43 + cpu * 0.42 + 1.2 * Math.sin(t / 23)) * 10) / 10,
network: {
rxBytesPerSec: Math.round(clamp(520 * KB + cpuSum * 12 * KB + 260 * KB * Math.sin(t / 6) + 140 * KB * Math.sin(t / 2.3) + ctx.between(0, 90 * KB), 4 * KB, 900 * MB)),
txBytesPerSec: Math.round(clamp(60 * KB + Math.min(media, 40) * 70 * KB * (1 + 0.25 * Math.sin(t / 4)) + ctx.between(0, 80 * KB), 4 * KB, 900 * MB)),
},
};
},
'GET /services': function () {
settle();
return services.map(view);
},
};
services.forEach(function (service) {
var path = '/services/' + service.def.id + '/';
routes['POST ' + path + 'restart'] = action(function (target) {
if (target.state === 'restarting') return;
target.state = 'restarting';
target.restartUntil = Date.now() + RESTART_MS;
target.restarts += 1;
target.skipCycle = Math.floor(ctx.seconds() / SICK_EVERY); // a restart cures it, until the next time
target.cpu = 0;
target.memory = 0;
});
routes['POST ' + path + 'stop'] = action(function (target) {
target.state = 'stopped';
target.cpu = 0;
target.memory = 0;
});
routes['POST ' + path + 'start'] = action(function (target) {
if (target.state !== 'stopped') return;
target.state = 'running';
target.startedAt = Date.now();
target.skipCycle = Math.floor(ctx.seconds() / SICK_EVERY);
});
});
czDemo.define({ device: { name: 'homelab', title: 'Home lab' }, routes: routes });
})();