I know it can be done with PHP/perl, I stopped coding in PHP/Perl for more than a decade now. I wanted to use JS tech stack to demonstrate for the workshop that I conduct, And I have many light-weight JS APIs, that don't want rewrite in PHP/Perl/Python now. BTW, what version of python is available? I prefer to use 3.12+
---
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const randomCPU = () => parseFloat((Math.random() * 100).toFixed(1));
const randomStatus = (cpu) => cpu > 80 ? "warning" : "healthy";
export default async function* serve(request) {
let length = parseInt(request.query_string?._length || 10, 10);
if (isNaN(length) || length < 1) length = 1;
if (length > 100) length = 100;
for (let i = 1; i <= length; i++) {
await sleep(100);
const cpu = randomCPU();
yield {
chunk_id: i,
event: "telemetry_pulse",
timestamp: new Date().toISOString(),
metrics: {
cpu_utilization: cpu,
memory_free_bytes: Math.floor(Math.random() * 1000000) + 10000000,
status: randomStatus(cpu)
},
engine: "ES Modules (.mjs)"
};
}
}
Just FYR. This JS generates max 100 dynamic streaming records, which will be consumed in browser UI to plot a graph or table or list...