Elffuss BraidHub

kikocis/continue public

main · 4.5 KiBRawPermalink
1import { Box, Text } from "ink";
2import React, { useEffect, useState } from "react";
3
4import { services } from "../../services/index.js";
5import { ResourceUsage } from "../../services/ResourceMonitoringService.js";
6
7interface ResourceDebugBarProps {
8 visible: boolean;
9}
10
11function formatBytes(bytes: number): string {
12 const mb = bytes / 1024 / 1024;
13 return mb >= 1 ? `${mb.toFixed(1)}MB` : `${(bytes / 1024).toFixed(1)}KB`;
14}
15
16function formatPercent(percent: number): string {
17 return `${Math.round(percent)}%`;
18}
19
20function formatTime(ms: number): string {
21 if (ms < 1000) return `${Math.round(ms)}ms`;
22 return `${(ms / 1000).toFixed(1)}s`;
23}
24
25export function ResourceDebugBar({ visible }: ResourceDebugBarProps) {
26 const [resourceData, setResourceData] = useState<ResourceUsage | null>(null);
27 const [isError, setIsError] = useState(false);
28
29 useEffect(() => {
30 if (!visible) return;
31
32 const updateResourceData = () => {
33 try {
34 const usage = services.resourceMonitoring.getCurrentResourceUsage();
35 setResourceData(usage);
36 setIsError(false);
37 } catch {
38 setIsError(true);
39 }
40 };
41
42 // Initial update
43 updateResourceData();
44
45 // Update every 2 seconds
46 const intervalId = setInterval(updateResourceData, 2000);
47
48 return () => {
49 clearInterval(intervalId);
50 };
51 }, [visible]);
52
53 if (!visible) {
54 return null;
55 }
56
57 if (isError) {
58 return (
59 <Box paddingX={1}>
60 <Text color="red">Resource monitoring error</Text>
61 </Box>
62 );
63 }
64
65 if (!resourceData) {
66 return null;
67 }
68
69 const memoryPercent =
70 (resourceData.memory.rss / resourceData.system.totalMemory) * 100;
71 const cpuPercent = resourceData.cpu.percent || 0;
72 const heapPercent =
73 (resourceData.memory.heapUsed / resourceData.memory.heapTotal) * 100;
74
75 // Color coding based on usage levels
76 const getMemoryColor = () => {
77 if (memoryPercent > 80) return "red";
78 if (memoryPercent > 60) return "yellow";
79 return "green";
80 };
81
82 const getCpuColor = () => {
83 if (cpuPercent > 80) return "red";
84 if (cpuPercent > 60) return "yellow";
85 return "green";
86 };
87
88 const getEventLoopColor = () => {
89 if (resourceData.eventLoop.lag > 100) return "red";
90 if (resourceData.eventLoop.lag > 50) return "yellow";
91 return "green";
92 };
93
94 return (
95 <Box paddingX={1}>
96 <Text color="gray">Debug: </Text>
97
98 <Text color={getMemoryColor()}>
99 MEM: {formatBytes(resourceData.memory.rss)}
100 </Text>
101 <Text color="gray"> | </Text>
102
103 <Text color="gray">HEAP: {formatPercent(heapPercent)}</Text>
104 <Text color="gray"> | </Text>
105
106 <Text color={getCpuColor()}>CPU: {formatPercent(cpuPercent)}</Text>
107 <Text color="gray"> | </Text>
108
109 <Text color={getEventLoopColor()}>
110 LAG: {formatTime(resourceData.eventLoop.lag)}
111 </Text>
112
113 {resourceData.fileDescriptors && (
114 <>
115 <Text color="gray"> | </Text>
116 <Text color="gray">FD: {resourceData.fileDescriptors}</Text>
117 </>
118 )}
119
120 <Text color="gray"> | </Text>
121 <Text color="gray">
122 UP: {formatTime(resourceData.system.uptime * 1000)}
123 </Text>
124 </Box>
125 );
126}
127
128export function ResourceDebugDetails({ visible }: { visible: boolean }) {
129 const [summary, setSummary] = useState<any>(null);
130
131 useEffect(() => {
132 if (!visible) return;
133
134 const updateData = () => {
135 try {
136 const summaryData = services.resourceMonitoring.getResourceSummary();
137 setSummary(summaryData);
138 } catch {
139 // Handle error silently
140 }
141 };
142
143 updateData();
144 const intervalId = setInterval(updateData, 3000);
145
146 return () => clearInterval(intervalId);
147 }, [visible]);
148
149 if (!visible || !summary) {
150 return null;
151 }
152
153 return (
154 <Box
155 flexDirection="column"
156 borderStyle="single"
157 borderColor="gray"
158 paddingX={1}
159 marginTop={1}
160 >
161 <Text color="cyan" bold>
162 Resource Usage Summary
163 </Text>
164
165 <Box marginTop={1}>
166 <Text color="gray">Current: </Text>
167 <Text>{formatBytes(summary.current.memory.rss)} mem, </Text>
168 <Text>{formatPercent(summary.current.cpu.percent || 0)} cpu</Text>
169 </Box>
170
171 <Box>
172 <Text color="gray">Peak: </Text>
173 <Text>{formatBytes(summary.peak.memory)} mem, </Text>
174 <Text>{formatPercent(summary.peak.cpu)} cpu</Text>
175 </Box>
176
177 <Box>
178 <Text color="gray">Average: </Text>
179 <Text>{formatBytes(summary.average.memory)} mem, </Text>
180 <Text>{formatPercent(summary.average.cpu)} cpu</Text>
181 </Box>
182 </Box>
183 );
184}