Skip to content

Commit 7ff2e0c

Browse files
committed
send resource usage to frontend
1 parent 80c313a commit 7ff2e0c

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

client/src/features/overview/pages/ServerInstance.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ function ServerInstance() {
2828
useEffect(() => {
2929
if (!instance) return;
3030

31+
// Fetch metrics immediately on page load
32+
const fetchMetrics = async () => {
33+
try {
34+
const response = await axiosInstance.get(`/api/metrics/instance?pod=${instance.podName}`);
35+
if (response.data.success) {
36+
setMetrics(response.data.data.metrics);
37+
}
38+
} catch (error) {
39+
console.error('Failed to fetch initial metrics:', error);
40+
}
41+
};
42+
43+
fetchMetrics();
44+
45+
// Set up WebSocket listener for real-time updates
3146
const metricsListener = new InstanceMetricsListener((data) => {
3247
if (data.podName === instance.podName) {
3348
setMetrics(data.metrics);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {ApiEndpoint, AuthType} from '../types';
2+
import InstanceMetricsService from '../../services/instanceMetricsService';
3+
import {InstanceResourceMetrics} from '../../../../shared/model/instance';
4+
5+
export interface GetInstanceMetricsResponse {
6+
podName: string;
7+
metrics: InstanceResourceMetrics;
8+
}
9+
10+
export const getInstanceMetricsEndpoint: ApiEndpoint<unknown, GetInstanceMetricsResponse> = {
11+
path: '/api/metrics/instance',
12+
method: 'get',
13+
auth: AuthType.Basic,
14+
handler: async (req, res) => {
15+
try {
16+
const { pod } = req.query;
17+
18+
if (!pod || typeof pod !== 'string') {
19+
return res.status(400).json({
20+
success: false,
21+
error: 'Pod name is required'
22+
});
23+
}
24+
25+
const metricsService = InstanceMetricsService.getInstance();
26+
const metrics = await metricsService.getMetricsForPod(pod);
27+
28+
res.json({
29+
success: true,
30+
data: {
31+
podName: pod,
32+
metrics
33+
}
34+
});
35+
} catch (error) {
36+
console.error('Failed to fetch instance metrics:', error);
37+
res.status(500).json({
38+
success: false,
39+
error: 'Failed to fetch instance metrics'
40+
});
41+
}
42+
}
43+
};

server/src/features/api/controllers/apiManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {resetPasswordEndpoint} from "../../../api/users/resetPassword";
3131
import {deleteUserEndpoint} from "../../../api/users/deleteUser";
3232
import {getCpuMetricsEndpoint} from "../../../api/metrics/getCpuMetrics";
3333
import {getMemoryMetricsEndpoint} from "../../../api/metrics/getMemoryMetrics";
34+
import {getInstanceMetricsEndpoint} from "../../../api/metrics/getInstanceMetrics";
3435
import {getDeploymentsEndpoint} from "../../../api/deployments/getDeployments";
3536
import {ApiEndpoint, AuthType} from "../../../api/types";
3637
import {handleAdminAuth, handleBasicAuth} from "../../../middleware/auth";
@@ -127,6 +128,7 @@ export default class ApiManager {
127128
//Metrics
128129
this.addEndpoint(getCpuMetricsEndpoint);
129130
this.addEndpoint(getMemoryMetricsEndpoint);
131+
this.addEndpoint(getInstanceMetricsEndpoint);
130132

131133
//File Sessions
132134
this.addEndpoint(createSessionEndpoint);

server/src/services/instanceMetricsService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class InstanceMetricsService {
5454
}
5555
}
5656

57+
public async getMetricsForPod(podName: string, namespace: string = 'default'): Promise<InstanceResourceMetrics> {
58+
const instance = { podName } as Instance;
59+
return await this.getInstanceMetrics(instance);
60+
}
61+
5762
private async broadcastMetrics(): Promise<void> {
5863
if (!this.socketManager) {
5964
console.warn('SocketManager not set, skipping metrics broadcast');

0 commit comments

Comments
 (0)