Skip to content

Commit d5a5b3e

Browse files
committed
Add PLAYTIME_PER_PLAYER_AVERAGE datapoint
1 parent 0427d2a commit d5a5b3e

7 files changed

Lines changed: 104 additions & 4 deletions

File tree

Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/auth/WebPermission.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public enum WebPermission implements Supplier<String>, Lang {
154154
DATA_PLAYER_SESSION_LENGTH_AVERAGE("See Average session length datapoint of players"),
155155
DATA_SERVER_SESSION_LENGTH_AVERAGE("See Average session length datapoint of servers"),
156156
DATA_NETWORK_SESSION_LENGTH_AVERAGE("See Average session length datapoint of network"),
157+
DATA_SERVER_PLAYTIME_PER_PLAYER_AVERAGE("See Average playtime per player datapoint of servers"),
158+
DATA_NETWORK_PLAYTIME_PER_PLAYER_AVERAGE("See Average playtime per player datapoint of network"),
157159
DATA_SERVER_PLAYERS_ONLINE_PEAK("See Player online peak -datapoint of servers"),
158160
DATA_NETWORK_PLAYERS_ONLINE_PEAK("See Player online peak -datapoint of network"),
159161

Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/datapoint/DatapointType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public enum DatapointType {
4242
REGULAR_PLAYERS(RegularPlayers.class, DatapointCacheKey.SESSION),
4343
SESSION_COUNT(SessionCount.class, DatapointCacheKey.SESSION),
4444
SESSION_LENGTH_AVERAGE(SessionLengthAverage.class, DatapointCacheKey.SESSION),
45+
PLAYTIME_PER_PLAYER_AVERAGE(PlaytimePerPlayerAverage.class, DatapointCacheKey.SESSION),
4546
PLAYERS_ONLINE_PEAK(PlayersOnlinePeak.class, DatapointCacheKey.TPS);
4647

4748
private final Class<? extends Datapoint<?>> datapointClass;

Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/datapoint/types/DatapointModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public interface DatapointModule {
8080
@IntoSet
8181
Datapoint<?> bindSessionLengthAverage(SessionLengthAverage sessionLengthAverage);
8282

83+
@Binds
84+
@IntoSet
85+
Datapoint<?> bindPlaytimePerPlayerAverage(PlaytimePerPlayerAverage playtimePerPlayerAverage);
86+
8387
@Binds
8488
@IntoSet
8589
Datapoint<?> bindPlayerOnlinePeak(PlayersOnlinePeak playersOnlinePeak);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is part of Player Analytics (Plan).
3+
*
4+
* Plan is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License v3 as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Plan is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
package com.djrapitops.plan.delivery.rendering.json.datapoint.types;
18+
19+
import com.djrapitops.plan.delivery.domain.auth.WebPermission;
20+
import com.djrapitops.plan.delivery.domain.datatransfer.GenericFilter;
21+
import com.djrapitops.plan.delivery.rendering.json.datapoint.Datapoint;
22+
import com.djrapitops.plan.delivery.rendering.json.datapoint.DatapointType;
23+
import com.djrapitops.plan.delivery.web.resolver.exception.BadRequestException;
24+
import com.djrapitops.plan.identification.ServerUUID;
25+
import com.djrapitops.plan.storage.database.DBSystem;
26+
import com.djrapitops.plan.storage.database.Database;
27+
import com.djrapitops.plan.storage.database.queries.objects.SessionQueries;
28+
29+
import javax.inject.Inject;
30+
import javax.inject.Singleton;
31+
import java.util.List;
32+
import java.util.Optional;
33+
34+
/**
35+
* Datapoint for average playtime per player.
36+
*
37+
* @author AuroraLS3
38+
*/
39+
@Singleton
40+
public class PlaytimePerPlayerAverage implements Datapoint<Long> {
41+
42+
private final DBSystem dbSystem;
43+
44+
@Inject
45+
public PlaytimePerPlayerAverage(DBSystem dbSystem) {
46+
this.dbSystem = dbSystem;
47+
}
48+
49+
@Override
50+
public DatapointType getType() {
51+
return DatapointType.PLAYTIME_PER_PLAYER_AVERAGE;
52+
}
53+
54+
@Override
55+
public FormatType getFormatType() {
56+
return FormatType.TIME_AMOUNT;
57+
}
58+
59+
@Override
60+
public WebPermission getPermission(GenericFilter filter) {
61+
if (filter.getPlayerUUID().isPresent()) {
62+
return WebPermission.DATA_PLAYER;
63+
} else if (!filter.getServerUUIDs().isEmpty()) {
64+
return WebPermission.DATA_SERVER_PLAYTIME_PER_PLAYER_AVERAGE;
65+
} else {
66+
return WebPermission.DATA_NETWORK_PLAYTIME_PER_PLAYER_AVERAGE;
67+
}
68+
}
69+
70+
@Override
71+
public Optional<Long> getValue(GenericFilter filter) {
72+
if (filter.getPlayerUUID().isPresent()) {
73+
throw new BadRequestException("PLAYTIME_PER_PLAYER_AVERAGE does not support player parameter");
74+
}
75+
Database db = dbSystem.getDatabase();
76+
77+
List<ServerUUID> serverUUIDs = filter.getServerUUIDs();
78+
79+
// This does not include active sessions
80+
// because merging active sessions to the average would produce more rows in SQL query.
81+
return Optional.of(db.query(SessionQueries.averagePlaytimePerPlayer(filter.getAfter(), filter.getBefore(), serverUUIDs)));
82+
}
83+
}

Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/SessionQueries.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,16 +760,18 @@ public Long processResults(ResultSet set) throws SQLException {
760760
};
761761
}
762762

763-
public static Query<Long> averagePlaytimePerPlayer(long after, long before, ServerUUID serverUUID) {
763+
public static Query<Long> averagePlaytimePerPlayer(long after, long before, List<ServerUUID> serverUUIDs) {
764+
if (serverUUIDs.isEmpty()) {
765+
return averagePlaytimePerPlayer(after, before);
766+
}
764767
return db -> {
765768
String selectPlaytimePerPlayer = SELECT +
766769
SessionsTable.USER_ID + "," +
767770
playtimeColumn(db.getSql(), after, before) +
768771
FROM + SessionsTable.TABLE_NAME + " t" +
769-
INNER_JOIN + ServerTable.TABLE_NAME + " s ON s." + ServerTable.ID + "=t." + SessionsTable.SERVER_ID +
770772
WHERE + SessionsTable.SESSION_START + "<=?" +
771773
AND + SessionsTable.SESSION_END + ">=?" +
772-
AND + "s." + ServerTable.SERVER_UUID + "=?" +
774+
AND + SessionsTable.SERVER_ID + " IN " + ServerTable.selectServerIds(serverUUIDs) +
773775
GROUP_BY + SessionsTable.USER_ID;
774776
String selectAverage = SELECT + "AVG(playtime) as average" + FROM + '(' + selectPlaytimePerPlayer + ") q1";
775777

@@ -778,7 +780,6 @@ public static Query<Long> averagePlaytimePerPlayer(long after, long before, Serv
778780
public void prepare(PreparedStatement statement) throws SQLException {
779781
statement.setLong(1, before);
780782
statement.setLong(2, after);
781-
statement.setString(3, serverUUID.toString());
782783
}
783784

784785
@Override
@@ -789,6 +790,10 @@ public Long processResults(ResultSet set) throws SQLException {
789790
};
790791
}
791792

793+
public static Query<Long> averagePlaytimePerPlayer(long after, long before, ServerUUID serverUUID) {
794+
return averagePlaytimePerPlayer(after, before, Collections.singletonList(serverUUID));
795+
}
796+
792797
/**
793798
* Fetch average playtime per ALL players.
794799
*

Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/AccessControlTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ static Stream<Arguments> testCases() {
213213
Arguments.of("/v1/datapoint?type=SESSION_LENGTH_AVERAGE", WebPermission.DATA_NETWORK_SESSION_LENGTH_AVERAGE, 200, 403),
214214
Arguments.of("/v1/datapoint?type=SESSION_LENGTH_AVERAGE&server=" + TestConstants.SERVER_UUID_STRING, WebPermission.DATA_SERVER_SESSION_LENGTH_AVERAGE, 200, 403),
215215
Arguments.of("/v1/datapoint?type=SESSION_LENGTH_AVERAGE&player=" + TestConstants.PLAYER_ONE_UUID_STRING, WebPermission.DATA_PLAYER_SESSION_LENGTH_AVERAGE, 200, 403),
216+
Arguments.of("/v1/datapoint?type=PLAYTIME_PER_PLAYER_AVERAGE", WebPermission.DATA_NETWORK_PLAYTIME_PER_PLAYER_AVERAGE, 200, 403),
217+
Arguments.of("/v1/datapoint?type=PLAYTIME_PER_PLAYER_AVERAGE&server=" + TestConstants.SERVER_UUID_STRING, WebPermission.DATA_SERVER_PLAYTIME_PER_PLAYER_AVERAGE, 200, 403),
218+
Arguments.of("/v1/datapoint?type=PLAYTIME_PER_PLAYER_AVERAGE&player=" + TestConstants.PLAYER_ONE_UUID_STRING, WebPermission.DATA_PLAYER, 400, 403),
216219
Arguments.of("/v1/datapoint?type=PLAYERS_ONLINE_PEAK", WebPermission.DATA_NETWORK_PLAYERS_ONLINE_PEAK, 200, 403),
217220
Arguments.of("/v1/datapoint?type=PLAYERS_ONLINE_PEAK&server=" + TestConstants.SERVER_UUID_STRING, WebPermission.DATA_SERVER_PLAYERS_ONLINE_PEAK, 200, 403),
218221
Arguments.of("/v1/datapoint?type=PLAYERS_ONLINE_PEAK&player=" + TestConstants.PLAYER_ONE_UUID_STRING, WebPermission.DATA_PLAYER, 400, 403),

Plan/react/dashboard/src/dataHooks/model/datapoint/Datapoint.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export enum DatapointType {
2323
PLAYERS_ONLINE_PEAK = "PLAYERS_ONLINE_PEAK",
2424
SESSION_COUNT = "SESSION_COUNT",
2525
SESSION_LENGTH_AVERAGE = "SESSION_LENGTH_AVERAGE",
26+
PLAYTIME_PER_PLAYER_AVERAGE = "PLAYTIME_PER_PLAYER_AVERAGE",
2627
}
2728

2829
export type DatapointTypeMap = {
@@ -35,6 +36,7 @@ export type DatapointTypeMap = {
3536
REGULAR_PLAYERS: number;
3637
SESSION_COUNT: number;
3738
SESSION_LENGTH_AVERAGE: number;
39+
PLAYTIME_PER_PLAYER_AVERAGE: number;
3840
SERVER_OCCUPIED: OutOf;
3941
MOST_ACTIVE_WORLD: OutOfCategory;
4042
MOST_ACTIVE_GAME_MODE: OutOfCategory;

0 commit comments

Comments
 (0)