diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java index ad9407cdb..b0702dfa0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/SessionQueries.java @@ -720,6 +720,25 @@ public class SessionQueries { }; } + public static Query afkTime(long after, long before) { + String sql = SELECT + "SUM(" + SessionsTable.AFK_TIME + ") as afk_time" + + FROM + SessionsTable.TABLE_NAME + + WHERE + SessionsTable.SESSION_END + ">=?" + + AND + SessionsTable.SESSION_START + "<=?"; + return new QueryStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setLong(1, after); + statement.setLong(2, before); + } + + @Override + public Long processResults(ResultSet set) throws SQLException { + return set.next() ? set.getLong("afk_time") : 0L; + } + }; + } + public static Query> playtimePerServer(long after, long before) { String sql = SELECT + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime," + diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkOverviewJSONParser.java b/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkOverviewJSONParser.java index a4ac36d4f..f68396a63 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkOverviewJSONParser.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkOverviewJSONParser.java @@ -37,7 +37,7 @@ import java.util.*; import java.util.concurrent.TimeUnit; /** - * Parses JSON payload for /server-page Server Overview tab. + * Parses JSON payload for /network-page Network Overview tab. * * @author Rsl1122 */ diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkPlayerBaseOverviewJSONParser.java b/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkPlayerBaseOverviewJSONParser.java index 4eb6cbef3..ef55a01c6 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkPlayerBaseOverviewJSONParser.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkPlayerBaseOverviewJSONParser.java @@ -34,7 +34,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; /** - * Parses JSON payload for /server-page Playerbase Overview tab. + * Parses JSON payload for /network-page Playerbase Overview tab. * * @author Rsl1122 */ diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkSessionsOverviewJSONParser.java b/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkSessionsOverviewJSONParser.java new file mode 100644 index 000000000..79d84c887 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/json/network/NetworkSessionsOverviewJSONParser.java @@ -0,0 +1,75 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.system.json.network; + +import com.djrapitops.plan.db.Database; +import com.djrapitops.plan.db.access.queries.objects.SessionQueries; +import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.utilities.formatting.Formatter; +import com.djrapitops.plan.utilities.formatting.Formatters; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * Parses JSON payload for /network-page Sessions tab. + * + * @author Rsl1122 + */ +@Singleton +public class NetworkSessionsOverviewJSONParser implements NetworkTabJSONParser> { + + private DBSystem dbSystem; + + private Formatter timeAmount; + private Formatter percentage; + + @Inject + public NetworkSessionsOverviewJSONParser( + DBSystem dbSystem, + Formatters formatters + ) { + this.dbSystem = dbSystem; + + timeAmount = formatters.timeAmount(); + percentage = formatters.percentage(); + } + + public Map createJSONAsMap() { + return Collections.singletonMap("insights", createInsightsMap()); + } + + private Map createInsightsMap() { + Database db = dbSystem.getDatabase(); + long now = System.currentTimeMillis(); + long monthAgo = now - TimeUnit.DAYS.toMillis(30L); + + Map insights = new HashMap<>(); + + Long playtime = db.query(SessionQueries.playtime(monthAgo, now)); + Long afkTime = db.query(SessionQueries.afkTime(monthAgo, now)); + insights.put("total_playtime", timeAmount.apply(playtime)); + insights.put("afk_time", timeAmount.apply(afkTime)); + insights.put("afk_time_perc", playtime != 0 ? percentage.apply(1.0 * afkTime / playtime) : "-"); + + return insights; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/NetworkJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/NetworkJSONHandler.java index 801414ccf..3c96ec996 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/NetworkJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/NetworkJSONHandler.java @@ -20,6 +20,7 @@ import com.djrapitops.plan.api.exceptions.WebUserAuthException; import com.djrapitops.plan.system.json.JSONFactory; import com.djrapitops.plan.system.json.network.NetworkOverviewJSONParser; import com.djrapitops.plan.system.json.network.NetworkPlayerBaseOverviewJSONParser; +import com.djrapitops.plan.system.json.network.NetworkSessionsOverviewJSONParser; import com.djrapitops.plan.system.json.network.NetworkTabJSONParser; import com.djrapitops.plan.system.webserver.RequestTarget; import com.djrapitops.plan.system.webserver.auth.Authentication; @@ -42,12 +43,14 @@ public class NetworkJSONHandler extends TreePageHandler { ResponseFactory responseFactory, JSONFactory jsonFactory, NetworkOverviewJSONParser networkOverviewJSONParser, - NetworkPlayerBaseOverviewJSONParser playerBaseOverviewJSONParser + NetworkPlayerBaseOverviewJSONParser playerBaseOverviewJSONParser, + NetworkSessionsOverviewJSONParser sessionsOverviewJSONParser ) { super(responseFactory); registerPage("overview", networkOverviewJSONParser); registerPage("playerbaseOverview", playerBaseOverviewJSONParser); + registerPage("sessionsOverview", sessionsOverviewJSONParser); registerPage("servers", jsonFactory::serversAsJSONMaps); registerPage("pingTable", jsonFactory::pingPerGeolocation); } diff --git a/Plan/common/src/main/resources/assets/plan/web/network.html b/Plan/common/src/main/resources/assets/plan/web/network.html index 283ff5d87..3946cebfc 100644 --- a/Plan/common/src/main/resources/assets/plan/web/network.html +++ b/Plan/common/src/main/resources/assets/plan/web/network.html @@ -871,6 +871,7 @@ setLoadingText('Calculating values..'); jsonRequest("../v1/network/overview", loadNetworkOverviewValues); jsonRequest("../v1/network/servers", loadServerBoxes); + jsonRequest("../v1/network/sessionsOverview", loadSessionValues); jsonRequest("../v1/network/playerbaseOverview", loadPlayerbaseOverviewValues); jsonRequest("../v1/sessions", loadSessionAccordion); setLoadingText('Rendering graphs..');