@sethdouglasford/claude-flow
Version:
Claude Code Flow - Advanced AI-powered development workflows with SPARC methodology
274 lines âĸ 9.96 kB
JavaScript
/**
* System Monitor - Real-time monitoring of system processes
*/
import { colors } from "../../cliffy-compat.js";
import { SystemEvents } from "../../../utils/types.js";
import { eventBus } from "../../../core/event-bus.js";
export class SystemMonitor {
processManager;
events = [];
maxEvents = 100;
metricsInterval;
constructor(processManager) {
this.processManager = processManager;
this.setupEventListeners();
}
setupEventListeners() {
// System events
eventBus.on(SystemEvents.AGENT_SPAWNED, (data) => {
this.addEvent({
type: "agent_spawned",
timestamp: Date.now(),
data,
level: "info",
});
});
eventBus.on(SystemEvents.AGENT_TERMINATED, (data) => {
this.addEvent({
type: "agent_terminated",
timestamp: Date.now(),
data,
level: "warning",
});
});
eventBus.on(SystemEvents.TASK_ASSIGNED, (data) => {
this.addEvent({
type: "task_assigned",
timestamp: Date.now(),
data,
level: "info",
});
});
eventBus.on(SystemEvents.TASK_COMPLETED, (data) => {
this.addEvent({
type: "task_completed",
timestamp: Date.now(),
data,
level: "success",
});
});
eventBus.on(SystemEvents.TASK_FAILED, (data) => {
this.addEvent({
type: "task_failed",
timestamp: Date.now(),
data,
level: "error",
});
});
eventBus.on(SystemEvents.SYSTEM_ERROR, (data) => {
this.addEvent({
type: "system_error",
timestamp: Date.now(),
data,
level: "error",
});
});
// Process manager events
this.processManager.on("processStarted", ({ processId, process }) => {
this.addEvent({
type: "process_started",
timestamp: Date.now(),
data: { processId, processName: process.name },
level: "success",
});
});
this.processManager.on("processStopped", ({ processId }) => {
this.addEvent({
type: "process_stopped",
timestamp: Date.now(),
data: { processId },
level: "warning",
});
});
this.processManager.on("processError", ({ processId, error }) => {
this.addEvent({
type: "process_error",
timestamp: Date.now(),
data: { processId, error: error.message },
level: "error",
});
});
}
addEvent(event) {
this.events.unshift(event);
if (this.events.length > this.maxEvents) {
this.events.pop();
}
}
start() {
// Start collecting metrics
this.metricsInterval = setInterval(() => {
this.collectMetrics();
}, 5000);
}
stop() {
if (this.metricsInterval) {
clearInterval(this.metricsInterval);
}
}
collectMetrics() {
// Collect system metrics
const processes = this.processManager.getAllProcesses();
for (const process of processes) {
if (process.status === "running") {
// Simulate metrics collection (would integrate with actual monitoring)
process.metrics = {
...process.metrics,
cpu: Math.random() * 50,
memory: Math.random() * 200,
uptime: process.startTime ? Date.now() - process.startTime : 0,
};
}
}
}
getRecentEvents(count = 10) {
return this.events.slice(0, count);
}
printEventLog(count = 20) {
console.log(colors.cyan.bold("đ Recent System Events"));
console.log(colors.gray("â".repeat(80)));
const events = this.getRecentEvents(count);
for (const event of events) {
const timestamp = new Date(event.timestamp).toLocaleTimeString();
const icon = this.getEventIcon(event.type);
const color = this.getEventColor(event.level);
console.log(colors.gray(timestamp), icon, color(this.formatEventMessage(event)));
}
}
getEventIcon(type) {
const icons = {
agent_spawned: "đ¤",
agent_terminated: "đ",
task_assigned: "đ",
task_completed: "â
",
task_failed: "â",
system_error: "â ī¸",
process_started: "âļī¸",
process_stopped: "âšī¸",
process_error: "đ¨",
};
return icons[type] || "âĸ";
}
getEventColor(level) {
switch (level) {
case "success":
return colors.green;
case "info":
return colors.blue;
case "warning":
return colors.yellow;
case "error":
return colors.red;
default:
return colors.white;
}
}
formatEventMessage(event) {
const getErrorMessage = (error) => {
if (!error)
return "Unknown error";
if (typeof error === "string")
return error;
return error.message || "Unknown error";
};
switch (event.type) {
case "agent_spawned":
return `Agent spawned: ${event.data.agentId} (${event.data.profile?.type || "unknown"})`;
case "agent_terminated":
return `Agent terminated: ${event.data.agentId} - ${event.data.reason}`;
case "task_assigned":
return `Task ${event.data.taskId} assigned to ${event.data.agentId}`;
case "task_completed":
return `Task completed: ${event.data.taskId}`;
case "task_failed":
return `Task failed: ${event.data.taskId} - ${getErrorMessage(event.data.error)}`;
case "system_error":
return `System error in ${event.data.component}: ${getErrorMessage(event.data.error)}`;
case "process_started":
return `Process started: ${event.data.processName}`;
case "process_stopped":
return `Process stopped: ${event.data.processId}`;
case "process_error":
return `Process error: ${event.data.processId} - ${getErrorMessage(event.data.error)}`;
default:
return JSON.stringify(event.data);
}
}
printSystemHealth() {
const stats = this.processManager.getSystemStats();
const processes = this.processManager.getAllProcesses();
console.log(colors.cyan.bold("đĨ System Health"));
console.log(colors.gray("â".repeat(60)));
// Overall status
const healthStatus = stats.errorProcesses === 0 ?
colors.green("â Healthy") :
colors.red(`â Unhealthy (${stats.errorProcesses} errors)`);
console.log("Status:", healthStatus);
console.log("Uptime:", this.formatUptime(stats.systemUptime));
console.log();
// Process status
console.log(colors.white.bold("Process Status:"));
for (const process of processes) {
const status = this.getProcessStatusIcon(process.status);
const { metrics } = process;
let line = ` ${status} ${process.name.padEnd(20)}`;
if (metrics && process.status === "running") {
line += colors.gray(` CPU: ${metrics.cpu?.toFixed(1)}% `);
line += colors.gray(` MEM: ${metrics.memory?.toFixed(0)}MB`);
}
console.log(line);
}
console.log();
// System metrics
console.log(colors.white.bold("System Metrics:"));
console.log(` Active Processes: ${stats.runningProcesses}/${stats.totalProcesses}`);
console.log(` Recent Events: ${this.events.length}`);
// Recent errors
const recentErrors = this.events
.filter(e => e.level === "error")
.slice(0, 3);
if (recentErrors.length > 0) {
console.log();
console.log(colors.red.bold("Recent Errors:"));
for (const error of recentErrors) {
const time = new Date(error.timestamp).toLocaleTimeString();
console.log(colors.red(` ${time} - ${this.formatEventMessage(error)}`));
}
}
}
getProcessStatusIcon(status) {
switch (status) {
case "running":
return colors.green("â");
case "stopped":
return colors.gray("â");
case "starting":
return colors.yellow("â");
case "stopping":
return colors.yellow("â");
case "error":
return colors.red("â");
default:
return colors.gray("?");
}
}
formatUptime(ms) {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (days > 0) {
return `${days}d ${hours % 24}h ${minutes % 60}m`;
}
else if (hours > 0) {
return `${hours}h ${minutes % 60}m ${seconds % 60}s`;
}
else if (minutes > 0) {
return `${minutes}m ${seconds % 60}s`;
}
else {
return `${seconds}s`;
}
}
}
//# sourceMappingURL=system-monitor.js.map