Files
Backend/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java

71 lines
2.7 KiB
Java
Raw Normal View History

2024-04-18 00:25:44 +01:00
package xyz.mcutils.backend.websocket;
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import xyz.mcutils.backend.Main;
import xyz.mcutils.backend.common.Timer;
import xyz.mcutils.backend.model.metric.WebsocketMetrics;
import xyz.mcutils.backend.service.MetricService;
2024-04-18 02:26:02 +01:00
import xyz.mcutils.backend.service.metric.metrics.TotalPlayerLookupsMetric;
2024-04-18 00:25:44 +01:00
import xyz.mcutils.backend.service.metric.metrics.TotalRequestsMetric;
2024-04-18 02:26:02 +01:00
import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric;
2024-04-18 00:25:44 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Log4j2(topic = "WebSocket/Metrics")
public class MetricsWebSocketHandler extends TextWebSocketHandler {
private final long interval = TimeUnit.SECONDS.toMillis(5);
2024-04-18 03:06:39 +01:00
public static final List<WebSocketSession> SESSIONS = new ArrayList<>();
2024-04-18 00:25:44 +01:00
private final MetricService metricService;
2024-04-18 00:25:44 +01:00
public MetricsWebSocketHandler(MetricService metricService) {
this.metricService = metricService;
2024-04-18 00:25:44 +01:00
Timer.scheduleRepeating(() -> {
2024-04-18 03:06:39 +01:00
for (WebSocketSession session : SESSIONS) {
sendMetrics(session);
2024-04-18 00:25:44 +01:00
}
}, interval, interval);
}
/**
* Sends the metrics to the client.
*
* @param session the session to send the metrics to
*/
private void sendMetrics(WebSocketSession session) {
try {
WebsocketMetrics metrics = new WebsocketMetrics(Map.of(
2024-04-18 02:26:02 +01:00
"totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue(),
"totalServerLookups", metricService.getMetric(TotalServerLookupsMetric.class).getValue(),
"totalPlayerLookups", metricService.getMetric(TotalPlayerLookupsMetric.class).getValue()
));
session.sendMessage(new TextMessage(Main.GSON.toJson(metrics)));
} catch (Exception e) {
log.error("An error occurred while sending metrics to the client", e);
}
}
2024-04-18 00:25:44 +01:00
@Override
public void afterConnectionEstablished(WebSocketSession session) {
log.info("WebSocket connection established with session id: {}", session.getId());
sendMetrics(session);
2024-04-18 03:06:39 +01:00
SESSIONS.add(session);
2024-04-18 00:25:44 +01:00
}
@Override
public void afterConnectionClosed(WebSocketSession session, @NotNull CloseStatus status) {
log.info("WebSocket connection closed with session id: {}", session.getId());
2024-04-18 03:06:39 +01:00
SESSIONS.remove(session);
2024-04-18 00:25:44 +01:00
}
}