megumu
v0.0.1
Published
A simple way to monitor your os
Downloads
3
Readme
The name "megumu" is from Iizunamaru Megumu(飯綱丸 龍), a character from Touhou.
Installation
npm install megumu --save
Usage
Simple example:
import { cpu, memory } from "megumu";
// Get CPU usage
cpu.addEventListener(
{
type: "TICK",
},
(data) => {
console.log(data);
},
1000
);
cpu.startSampling();
// Get memory usage
memory.addEventListener(
{
type: "GE",
target: "OS",
value: "2GB",
duration: 10000,
},
(data) => {
// This callback is triggered only when the system memory usage is greater than 2GB and lasts for 10s
console.log(data);
},
1000
);
memory.startSampling();
API
Public API
Both cpu
and memory
have the same public API below:
addEventListener
function addEventListener(
meta: CpuMeta | MemoryMeta,
callback: Function,
interval: number
): () => void;
Add an event listener to the monitor, and return a function to remove the listener.
meta
: The meta data of the event listener, the specific type will be detailed below.callback
: The callback function to be triggered when the event is fired, the specific type will be detailed below.interval
: The interval of the monitor.return
: A function to remove the listener.
startSampling
function startSampling(): void;
Start the monitor.
stopSampling
function stopSampling(): void;
Stop the monitor.
destroy
function destroy(): void;
Stop the monitor and remove all event listeners.
CPU
type CpuTickMeta = {
type: "TICK"; // This type of meta will trigger the callback function every `interval`
sampleInterval?: number; // The sample interval of the cpu usage, need to be less than the `interval`
};
type CpuBasicMeta = {
type: "LE" | "GE"; // Whether the cpu usage is less than or greater than the value
duration: number; // Only trigger the callback function when the cpu usage lasts for `duration`
value: number | string; // e.g. 0.8, "80%"
sampleInterval?: number; // The sample interval of the cpu usage, need to be less than the `interval`
};
type CpuMeta = CpuBasicMeta | CpuTickMeta;
Callback
function addEventListener(
meta: CpuMeta,
callback: (meta: CpuInfo) => void,
interval: number
): () => void;
Memory
type MemoryTarget =
| "OS" // The total memory of the operating system
| "RSS"
| "HEAP_TOTAL"
| "HEAP_USED"
| "EXTERNAL"
| "ARRAY_BUFFER";
type MemoryTickMeta = {
target: MemoryTarget; // The target of the memory monitor
type: "TICK"; // This type of meta will trigger the callback function every `interval`
};
type MemoryBasicMeta = {
target: MemoryTarget; // The target of the memory monitor
type: "LE" | "GE"; // Whether the memory usage is less than or greater than the value
duration: number; // Only trigger the callback function when the memory usage lasts for `duration`
value: number | string; // e.g. 0.8(only for OS type), "80%"(only for OS type), "2GB", "200MB"
};
type MemoryMeta = MemoryBasicMeta | MemoryTickMeta;
Callback
type MemoryCallback<T> = T extends { target: "OS" }
? (memory: { total: number; free: number }) => void
: (memory: number) => void;
function addEventListener<T extends MemoryMeta>(
meta: T,
callback: MemoryCallback<T>,
interval: number
): () => void;
License
MIT