Search in sources :

Example 21 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class JSONFactory method serversAsJSONMaps.

public Map<String, Object> serversAsJSONMaps() {
    Database db = dbSystem.getDatabase();
    long now = System.currentTimeMillis();
    long weekAgo = now - TimeUnit.DAYS.toMillis(7L);
    Formatter<Long> year = formatters.yearLong();
    Formatter<Double> decimals = formatters.decimals();
    Formatter<Long> timeAmount = formatters.timeAmount();
    Map<ServerUUID, Server> serverInformation = db.query(ServerQueries.fetchPlanServerInformation());
    ServerUUID proxyUUID = serverInformation.values().stream().filter(Server::isProxy).findFirst().map(Server::getUuid).orElse(null);
    Map<ServerUUID, Integer> serverUuidToId = new HashMap<>();
    for (Server server : serverInformation.values()) {
        server.getId().ifPresent(serverId -> serverUuidToId.put(server.getUuid(), serverId));
    }
    Map<Integer, List<TPS>> tpsDataByServerId = db.query(TPSQueries.fetchTPSDataOfAllServersBut(weekAgo, now, proxyUUID));
    Map<ServerUUID, Integer> totalPlayerCounts = db.query(PlayerCountQueries.newPlayerCounts(0, now));
    Map<ServerUUID, Integer> newPlayerCounts = db.query(PlayerCountQueries.newPlayerCounts(weekAgo, now));
    Map<ServerUUID, Integer> uniquePlayerCounts = db.query(PlayerCountQueries.uniquePlayerCounts(weekAgo, now));
    List<Map<String, Object>> servers = new ArrayList<>();
    serverInformation.entrySet().stream().sorted(Comparator.comparing(entry -> entry.getValue().getIdentifiableName().toLowerCase())).filter(entry -> entry.getValue().isNotProxy()).forEach(entry -> {
        ServerUUID serverUUID = entry.getKey();
        Map<String, Object> server = new HashMap<>();
        server.put("name", entry.getValue().getIdentifiableName());
        Optional<DateObj<Integer>> recentPeak = db.query(TPSQueries.fetchPeakPlayerCount(serverUUID, now - TimeUnit.DAYS.toMillis(2L)));
        Optional<DateObj<Integer>> allTimePeak = db.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID));
        server.put("last_peak_date", recentPeak.map(DateObj::getDate).map(year).orElse("-"));
        server.put("best_peak_date", allTimePeak.map(DateObj::getDate).map(year).orElse("-"));
        server.put("last_peak_players", recentPeak.map(DateObj::getValue).orElse(0));
        server.put("best_peak_players", allTimePeak.map(DateObj::getValue).orElse(0));
        TPSMutator tpsMonth = new TPSMutator(tpsDataByServerId.getOrDefault(serverUuidToId.get(serverUUID), Collections.emptyList()));
        server.put("playersOnline", tpsMonth.all().stream().map(tps -> new double[] { tps.getDate(), tps.getPlayers() }).toArray(double[][]::new));
        server.put("players", totalPlayerCounts.getOrDefault(serverUUID, 0));
        server.put("new_players", newPlayerCounts.getOrDefault(serverUUID, 0));
        server.put("unique_players", uniquePlayerCounts.getOrDefault(serverUUID, 0));
        TPSMutator tpsWeek = tpsMonth.filterDataBetween(weekAgo, now);
        double averageTPS = tpsWeek.averageTPS();
        server.put("avg_tps", averageTPS != -1 ? decimals.apply(averageTPS) : locale.get(HtmlLang.UNIT_NO_DATA).toString());
        server.put("low_tps_spikes", tpsWeek.lowTpsSpikeCount(config.get(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
        server.put("downtime", timeAmount.apply(tpsWeek.serverDownTime()));
        server.put("current_uptime", serverUptimeCalculator.getServerUptimeMillis(serverUUID).map(timeAmount).orElse(locale.getString(GenericLang.UNAVAILABLE)));
        Optional<TPS> online = tpsWeek.getLast();
        server.put("online", online.map(point -> point.getDate() >= now - TimeUnit.MINUTES.toMillis(3L) ? point.getPlayers() : "Possibly offline").orElse(locale.get(HtmlLang.UNIT_NO_DATA).toString()));
        servers.add(server);
    });
    return Collections.singletonMap("servers", servers);
}
Also used : HtmlLang(com.djrapitops.plan.settings.locale.lang.HtmlLang) java.util(java.util) PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) ServerTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.ServerTablePlayersQuery) ServerUUID(com.djrapitops.plan.identification.ServerUUID) TPSMutator(com.djrapitops.plan.delivery.domain.mutators.TPSMutator) Singleton(javax.inject.Singleton) Database(com.djrapitops.plan.storage.database.Database) DBSystem(com.djrapitops.plan.storage.database.DBSystem) Inject(javax.inject.Inject) Graphs(com.djrapitops.plan.delivery.rendering.json.graphs.Graphs) TimeSettings(com.djrapitops.plan.settings.config.paths.TimeSettings) NetworkTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.NetworkTablePlayersQuery) DateObj(com.djrapitops.plan.delivery.domain.DateObj) ServerUptimeCalculator(com.djrapitops.plan.gathering.ServerUptimeCalculator) ServerDto(com.djrapitops.plan.delivery.domain.datatransfer.ServerDto) ExtensionTabData(com.djrapitops.plan.extension.implementation.results.ExtensionTabData) SessionCache(com.djrapitops.plan.gathering.cache.SessionCache) Maps(com.djrapitops.plan.utilities.java.Maps) SessionsMutator(com.djrapitops.plan.delivery.domain.mutators.SessionsMutator) com.djrapitops.plan.gathering.domain(com.djrapitops.plan.gathering.domain) ServerInfo(com.djrapitops.plan.identification.ServerInfo) GenericLang(com.djrapitops.plan.settings.locale.lang.GenericLang) ExtensionServerTableDataQuery(com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerTableDataQuery) PlayerKillMutator(com.djrapitops.plan.delivery.domain.mutators.PlayerKillMutator) Formatters(com.djrapitops.plan.delivery.formatting.Formatters) SessionStartComparator(com.djrapitops.plan.utilities.comparators.SessionStartComparator) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Server(com.djrapitops.plan.identification.Server) Locale(com.djrapitops.plan.settings.locale.Locale) com.djrapitops.plan.storage.database.queries.objects(com.djrapitops.plan.storage.database.queries.objects) DisplaySettings(com.djrapitops.plan.settings.config.paths.DisplaySettings) Formatter(com.djrapitops.plan.delivery.formatting.Formatter) PlayerCountQueries(com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries) Server(com.djrapitops.plan.identification.Server) Database(com.djrapitops.plan.storage.database.Database) ServerUUID(com.djrapitops.plan.identification.ServerUUID) DateObj(com.djrapitops.plan.delivery.domain.DateObj) TPSMutator(com.djrapitops.plan.delivery.domain.mutators.TPSMutator)

Example 22 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class PlayerJSONCreator method playerExtensionData.

public List<ExtensionsDto> playerExtensionData(UUID playerUUID) {
    Database database = dbSystem.getDatabase();
    Map<ServerUUID, List<ExtensionData>> extensionPlayerData = database.query(new ExtensionPlayerDataQuery(playerUUID));
    Map<ServerUUID, Server> servers = database.query(ServerQueries.fetchPlanServerInformation());
    List<ExtensionsDto> playerData = new ArrayList<>();
    for (Map.Entry<ServerUUID, Server> entry : servers.entrySet()) {
        ServerUUID serverUUID = entry.getKey();
        playerData.add(new ExtensionsDto(playerUUID.toString(), serverUUID.toString(), entry.getValue().getIdentifiableName(), extensionPlayerData.getOrDefault(serverUUID, Collections.emptyList())));
    }
    return playerData;
}
Also used : ExtensionsDto(com.djrapitops.plan.delivery.domain.datatransfer.extension.ExtensionsDto) ServerUUID(com.djrapitops.plan.identification.ServerUUID) ExtensionPlayerDataQuery(com.djrapitops.plan.extension.implementation.storage.queries.ExtensionPlayerDataQuery) Server(com.djrapitops.plan.identification.Server) Database(com.djrapitops.plan.storage.database.Database)

Example 23 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class LinkCommands method onServerCommand.

/**
 * Implementation of server subcommand, used to get link to server page.
 *
 * @param sender    Sender of command.
 * @param arguments Given arguments.
 */
public void onServerCommand(CMDSender sender, Arguments arguments) {
    Server server;
    String identifier = arguments.concatenate(" ");
    if (arguments.isEmpty()) {
        server = serverInfo.getServer();
    } else {
        server = dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(identifier)).filter(s -> !s.isProxy()).orElseThrow(() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_SERVER_NOT_FOUND, identifier)));
    }
    String address = getAddress(sender) + "/server/" + Html.encodeToURL(server.getName());
    sender.buildMessage().addPart(colors.getMainColor() + locale.getString(CommandLang.LINK_SERVER)).apply(builder -> linkTo(builder, sender, address)).send();
}
Also used : Permissions(com.djrapitops.plan.settings.Permissions) ServerInfo(com.djrapitops.plan.identification.ServerInfo) CMDSender(com.djrapitops.plan.commands.use.CMDSender) CommandLang(com.djrapitops.plan.settings.locale.lang.CommandLang) Html(com.djrapitops.plan.delivery.rendering.html.Html) MessageBuilder(com.djrapitops.plan.commands.use.MessageBuilder) UUID(java.util.UUID) Singleton(javax.inject.Singleton) Database(com.djrapitops.plan.storage.database.Database) DBSystem(com.djrapitops.plan.storage.database.DBSystem) Inject(javax.inject.Inject) Addresses(com.djrapitops.plan.delivery.webserver.Addresses) List(java.util.List) Server(com.djrapitops.plan.identification.Server) WebUserQueries(com.djrapitops.plan.storage.database.queries.objects.WebUserQueries) ColorScheme(com.djrapitops.plan.commands.use.ColorScheme) Identifiers(com.djrapitops.plan.identification.Identifiers) Locale(com.djrapitops.plan.settings.locale.Locale) User(com.djrapitops.plan.delivery.domain.auth.User) Arguments(com.djrapitops.plan.commands.use.Arguments) ServerQueries(com.djrapitops.plan.storage.database.queries.objects.ServerQueries) Server(com.djrapitops.plan.identification.Server)

Example 24 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class ServerQueriesTest method uninstallingServerStopsItFromBeingReturnedInServerQuery.

@Test
default void uninstallingServerStopsItFromBeingReturnedInServerQuery() {
    db().executeTransaction(new SetServerAsUninstalledTransaction(serverUUID()));
    Optional<Server> found = db().query(ServerQueries.fetchServerMatchingIdentifier(serverUUID()));
    assertFalse(found.isPresent());
}
Also used : SetServerAsUninstalledTransaction(com.djrapitops.plan.storage.database.transactions.commands.SetServerAsUninstalledTransaction) Server(com.djrapitops.plan.identification.Server) Test(org.junit.jupiter.api.Test)

Example 25 with Server

use of com.djrapitops.plan.identification.Server in project Plan by plan-player-analytics.

the class ServerQueriesTest method uninstallingServerStopsItFromBeingReturnedInServersQuery.

@Test
default void uninstallingServerStopsItFromBeingReturnedInServersQuery() {
    db().executeTransaction(new SetServerAsUninstalledTransaction(serverUUID()));
    Collection<Server> found = db().query(ServerQueries.fetchPlanServerInformationCollection());
    assertTrue(found.isEmpty());
}
Also used : SetServerAsUninstalledTransaction(com.djrapitops.plan.storage.database.transactions.commands.SetServerAsUninstalledTransaction) Server(com.djrapitops.plan.identification.Server) Test(org.junit.jupiter.api.Test)

Aggregations

Server (com.djrapitops.plan.identification.Server)28 ServerUUID (com.djrapitops.plan.identification.ServerUUID)11 StoreServerInformationTransaction (com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction)9 Test (org.junit.jupiter.api.Test)7 PreparedStatement (java.sql.PreparedStatement)6 Database (com.djrapitops.plan.storage.database.Database)5 QueryStatement (com.djrapitops.plan.storage.database.queries.QueryStatement)5 ResultSet (java.sql.ResultSet)5 SessionsMutator (com.djrapitops.plan.delivery.domain.mutators.SessionsMutator)3 EnableException (com.djrapitops.plan.exceptions.EnableException)3 ServerInfo (com.djrapitops.plan.identification.ServerInfo)3 PlanConfig (com.djrapitops.plan.settings.config.PlanConfig)3 DBSystem (com.djrapitops.plan.storage.database.DBSystem)3 RemoveEverythingTransaction (com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction)3 SetServerAsUninstalledTransaction (com.djrapitops.plan.storage.database.transactions.commands.SetServerAsUninstalledTransaction)3 User (com.djrapitops.plan.delivery.domain.auth.User)2 ExtensionPlayerDataQuery (com.djrapitops.plan.extension.implementation.storage.queries.ExtensionPlayerDataQuery)2 Locale (com.djrapitops.plan.settings.locale.Locale)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2