From 414b82b0a6e2ff679221ecb97fd639392caa8409 Mon Sep 17 00:00:00 2001 From: Michel Jung Date: Thu, 18 Oct 2018 18:37:44 +0200 Subject: [PATCH] Count online players by country --- .../config/integration/WebsocketAdapterConfig.java | 6 ++++++ .../java/com/faforever/server/geoip/GeoIpService.java | 2 +- .../server/player/OnlinePlayerRepository.java | 3 +++ .../com/faforever/server/player/PlayerService.java | 10 ++++++++++ .../main/java/com/faforever/server/stats/Metrics.java | 1 + .../com/faforever/server/player/PlayerServiceTest.java | 10 +++++++--- 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/faf-java-server-app/src/main/java/com/faforever/server/config/integration/WebsocketAdapterConfig.java b/faf-java-server-app/src/main/java/com/faforever/server/config/integration/WebsocketAdapterConfig.java index 63c5f82..119650c 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/config/integration/WebsocketAdapterConfig.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/config/integration/WebsocketAdapterConfig.java @@ -31,6 +31,7 @@ import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.WebSocketSession; @@ -195,6 +196,11 @@ public void afterSessionStarted(WebSocketSession session, MessageChannel outputC extractClientDetailsOrNull(sessionPrincipal) .ifPresent(clientDetails -> clientDetails.setClientConnection(clientConnection)); + if (!(sessionPrincipal instanceof Authentication)) { + throw new IllegalStateException("Session principal needs to be a subclass of Authentication"); + } + clientConnection.setAuthentication((Authentication) sessionPrincipal); + extractUserDetailsOrNull(sessionPrincipal) .ifPresent(userDetails -> { userDetails.setClientConnection(clientConnection); diff --git a/faf-java-server-app/src/main/java/com/faforever/server/geoip/GeoIpService.java b/faf-java-server-app/src/main/java/com/faforever/server/geoip/GeoIpService.java index e092949..d2b3b63 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/geoip/GeoIpService.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/geoip/GeoIpService.java @@ -72,7 +72,7 @@ public Optional lookupTimezone(InetAddress inetAddress) { return lookupCity(inetAddress).map(city -> city.getLocation().getTimeZone()).map(TimeZone::getTimeZone); } - private Optional lookupCity(InetAddress inetAddress) { + public Optional lookupCity(InetAddress inetAddress) { Assert.state(databaseReader != null, "Database has not been initialized"); return noCatch(() -> { try { diff --git a/faf-java-server-app/src/main/java/com/faforever/server/player/OnlinePlayerRepository.java b/faf-java-server-app/src/main/java/com/faforever/server/player/OnlinePlayerRepository.java index 5f5ee21..1667ab9 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/player/OnlinePlayerRepository.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/player/OnlinePlayerRepository.java @@ -2,8 +2,11 @@ import org.springframework.data.repository.CrudRepository; +import java.util.List; import java.util.Optional; public interface OnlinePlayerRepository extends CrudRepository { Optional findByLogin(String login); + + List findAllByCountry(String country); } diff --git a/faf-java-server-app/src/main/java/com/faforever/server/player/PlayerService.java b/faf-java-server-app/src/main/java/com/faforever/server/player/PlayerService.java index 7a6cd7f..c8ef755 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/player/PlayerService.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/player/PlayerService.java @@ -4,6 +4,7 @@ import com.faforever.server.client.ClientService; import com.faforever.server.geoip.GeoIpService; import com.faforever.server.stats.Metrics; +import com.google.common.base.Strings; import io.micrometer.core.instrument.Gauge; import io.micrometer.core.instrument.MeterRegistry; import lombok.extern.slf4j.Slf4j; @@ -26,15 +27,18 @@ @MXBean public class PlayerService { + private final MeterRegistry meterRegistry; private final OnlinePlayerRepository onlinePlayerRepository; private final ClientService clientService; private final ApplicationEventPublisher eventPublisher; private final GeoIpService geoIpService; public static final String TAG_PLAYER_GAME_STATE = "gameState"; + private static final String TAG_PLAYER_GAME_COUNTRY = "country"; public PlayerService(ClientService clientService, MeterRegistry meterRegistry, OnlinePlayerRepository onlinePlayerRepository, ApplicationEventPublisher eventPublisher, GeoIpService geoIpService) { this.clientService = clientService; + this.meterRegistry = meterRegistry; this.onlinePlayerRepository = onlinePlayerRepository; this.eventPublisher = eventPublisher; this.geoIpService = geoIpService; @@ -61,6 +65,12 @@ public void setPlayerOnline(Player player) { eventPublisher.publishEvent(new PlayerOnlineEvent(this, player)); announceOnline(player); + + String country = player.getCountry(); + Gauge.builder(Metrics.PLAYERS_BY_LOCATION, onlinePlayerRepository, repo -> repo.findAllByCountry(country).size()) + .description("The number of online players in country " + country) + .tag(TAG_PLAYER_GAME_COUNTRY, Strings.nullToEmpty(country)) + .register(meterRegistry); } public void removePlayer(Player player) { diff --git a/faf-java-server-app/src/main/java/com/faforever/server/stats/Metrics.java b/faf-java-server-app/src/main/java/com/faforever/server/stats/Metrics.java index 7665857..a5c6493 100644 --- a/faf-java-server-app/src/main/java/com/faforever/server/stats/Metrics.java +++ b/faf-java-server-app/src/main/java/com/faforever/server/stats/Metrics.java @@ -4,4 +4,5 @@ public class Metrics { public static final String CLIENTS = "counter.clients"; public static final String GAMES = "counter.games"; public static final String PLAYERS = "counter.players"; + public static final String PLAYERS_BY_LOCATION = "counter.playersByLocation"; } diff --git a/faf-java-server-app/src/test/java/com/faforever/server/player/PlayerServiceTest.java b/faf-java-server-app/src/test/java/com/faforever/server/player/PlayerServiceTest.java index 899b8df..cbdfbe6 100644 --- a/faf-java-server-app/src/test/java/com/faforever/server/player/PlayerServiceTest.java +++ b/faf-java-server-app/src/test/java/com/faforever/server/player/PlayerServiceTest.java @@ -18,6 +18,7 @@ import java.net.InetAddress; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -45,11 +46,9 @@ public class PlayerServiceTest { @Mock private GeoIpService geoIpService; - private OnlinePlayerRepository onlinePlayerRepository; - @Before public void setUp() throws Exception { - onlinePlayerRepository = new FakeOnlinePlayerRepository(); + OnlinePlayerRepository onlinePlayerRepository = new FakeOnlinePlayerRepository(); player = (Player) new Player().setId(1); player.setLogin("JUnit"); instance = new PlayerService(clientService, meterRegistry, onlinePlayerRepository, eventPublisher, geoIpService); @@ -175,6 +174,11 @@ public Optional findByLogin(String login) { .filter(player1 -> player1.getLogin().equals(login)) .findFirst(); } + + @Override + public List findAllByCountry(String country) { + return players.values().stream().filter(player -> player.getCountry() != null).collect(Collectors.toList()); + } } }