Skip to content

Commit

Permalink
Added tracking of stats
Browse files Browse the repository at this point in the history
  • Loading branch information
freyacodes committed Jul 18, 2017
1 parent c3a1736 commit d34ccfc
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
*.iml
/logs/
4 changes: 4 additions & 0 deletions LavalinkClient/src/main/java/lavalink/client/io/Lavalink.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public IPlayer getPlayer(String guildId) {
return players.computeIfAbsent(guildId, __ -> new LavalinkPlayer(socket, guildId));
}

public RemoteStats getLastStats() {
return socket.stats;
}

public void shutdown() {
socket.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class LavalinkSocket extends WebSocketClient {

private static final int TIMEOUT_MS = 5000;
private final Lavalink lavalink;
RemoteStats stats;

LavalinkSocket(Lavalink lavalink, URI serverUri, Draft protocolDraft, Map<String, String> headers) {
super(serverUri, protocolDraft, headers, TIMEOUT_MS);
Expand Down Expand Up @@ -102,6 +103,9 @@ public void onMessage(String message) {
((LavalinkPlayer) lavalink.getPlayer(json.getString("guildId")))
.provideState(json.getJSONObject("state"));
break;
case "stats":
stats = new RemoteStats(json);
break;
case "event":
try {
handleEvent(json);
Expand Down
118 changes: 118 additions & 0 deletions LavalinkClient/src/main/java/lavalink/client/io/RemoteStats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package lavalink.client.io;

import org.json.JSONObject;

public class RemoteStats {

private int players;
private int playingPlayers;
private long uptime;

// In bytes
private int memFree;
private int memUsed;
private int memAllocated;
private int memReservable;

private int cpuCores;
private double systemLoad;
private double lavalinkLoad;

private int avgFramesSentPerMinute = -1;
private int avgFramesNulledPerMinute = -1;
private int avgFramesDeficitPerMinute = -1;

RemoteStats(JSONObject json) {
players = json.getInt("players");
playingPlayers = json.getInt("playingPlayers");
uptime = json.getLong("uptime");

memFree = json.getJSONObject("memory").getInt("free");
memUsed = json.getJSONObject("memory").getInt("used");
memAllocated = json.getJSONObject("memory").getInt("allocated");
memReservable = json.getJSONObject("memory").getInt("reservable");

cpuCores = json.getJSONObject("cpu").getInt("cores");
systemLoad = json.getJSONObject("cpu").getDouble("systemLoad");
lavalinkLoad = json.getJSONObject("cpu").getDouble("lavalinkLoad");

JSONObject frames = json.optJSONObject("frameStats");

if (frames != null) {
avgFramesSentPerMinute = frames.getInt("sent");
avgFramesNulledPerMinute = frames.getInt("nulled");
avgFramesDeficitPerMinute = frames.getInt("deficit");
}
}

public int getPlayers() {
return players;
}

public int getPlayingPlayers() {
return playingPlayers;
}

public long getUptime() {
return uptime;
}

public int getMemFree() {
return memFree;
}

public int getMemUsed() {
return memUsed;
}

public int getMemAllocated() {
return memAllocated;
}

public int getMemReservable() {
return memReservable;
}

public int getCpuCores() {
return cpuCores;
}

public double getSystemLoad() {
return systemLoad;
}

public double getLavalinkLoad() {
return lavalinkLoad;
}

public int getAvgFramesSentPerMinute() {
return avgFramesSentPerMinute;
}

public int getAvgFramesNulledPerMinute() {
return avgFramesNulledPerMinute;
}

public int getAvgFramesDeficitPerMinute() {
return avgFramesDeficitPerMinute;
}

@Override
public String toString() {
return "RemoteStats{" +
"players=" + players +
", playingPlayers=" + playingPlayers +
", uptime=" + uptime +
", memFree=" + memFree +
", memUsed=" + memUsed +
", memAllocated=" + memAllocated +
", memReservable=" + memReservable +
", cpuCores=" + cpuCores +
", systemLoad=" + systemLoad +
", lavalinkLoad=" + lavalinkLoad +
", avgFramesSentPerMinute=" + avgFramesSentPerMinute +
", avgFramesNulledPerMinute=" + avgFramesNulledPerMinute +
", avgFramesDeficitPerMinute=" + avgFramesDeficitPerMinute +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lavalink.client.io;

import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class RemoteStatsTest {

private static final Logger log = LoggerFactory.getLogger(RemoteStatsTest.class);

@Test
void remoteStatsTest() {
JSONObject json = new JSONObject("{\"playingPlayers\":0,\"op\":\"stats\",\"memory\":{\"reservable\":1892155392,\"used\":67111552,\"free\":137885056,\"allocated\":204996608},\"players\":0,\"cpu\":{\"cores\":4,\"systemLoad\":0,\"lavalinkLoad\":0},\"uptime\":15754}");
RemoteStats stats = new RemoteStats(json);

Assertions.assertEquals(-1, stats.getAvgFramesSentPerMinute());
Assertions.assertEquals(-1, stats.getAvgFramesNulledPerMinute());
Assertions.assertEquals(-1, stats.getAvgFramesDeficitPerMinute());
log.info(stats + "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import lavalink.server.player.AudioLossCounter;
import lavalink.server.player.Player;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;

public class StatsTask implements Runnable {

private static final Logger log = LoggerFactory.getLogger(StatsTask.class);

private SocketContext context;

public StatsTask(SocketContext context) {
StatsTask(SocketContext context) {
this.context = context;
}

@Override
public void run() {
log.info("Sending stats");

JSONObject out = new JSONObject();
out.put("op", "stats");
out.put("players", context.getPlayers().size());
Expand Down

0 comments on commit d34ccfc

Please sign in to comment.