From 847500e6c4991a8cb044ad5e8f3178bebe440d99 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Tue, 1 Feb 2022 16:29:43 +0200 Subject: [PATCH] Added %plan_top_{category}_{time}_{n}_value% placeholders Affects issues: - Close #2262 --- .../plan/placeholder/ServerPlaceHolders.java | 35 ++++++++++------- .../queries/analysis/TopListQueries.java | 38 +++++++++++++++---- .../queries/analysis/TopListQueriesTest.java | 6 ++- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java index 03350b56e..25bfb036f 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java @@ -211,39 +211,46 @@ public class ServerPlaceHolders implements Placeholders { } private void registerDynamicCategoryPlaceholders(PlanPlaceholders placeholders, Database database) { - List queries = new ArrayList<>(); + List> queries = new ArrayList<>(); queries.addAll(createCategoryQueriesForAllTimespans("playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10PlaytimePlayerOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis()))); queries.addAll(createCategoryQueriesForAllTimespans("active_playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis()))); for (int i = 0; i < 10; i++) { - for (TopCategoryQuery query : queries) { + for (TopCategoryQuery query : queries) { final int nth = i; placeholders.registerStatic(String.format("top_%s_%s_%s", query.getCategory(), query.getTimeSpan(), nth), - parameters -> database.query(query.getQuery(nth, parameters)).orElse("-")); + parameters -> database.query(query.getQuery(nth, parameters)) + .map(TopListQueries.TopListEntry::getPlayerName) + .orElse("-")); + placeholders.registerStatic(String.format("top_%s_%s_%s_value", query.getCategory(), query.getTimeSpan(), nth), + parameters -> database.query(query.getQuery(nth, parameters)) + .map(TopListQueries.TopListEntry::getValue) + .map(formatters.timeAmount()) + .orElse("-")); } } } - private List createCategoryQueriesForAllTimespans(String category, QueryCreator queryCreator) { + private List> createCategoryQueriesForAllTimespans(String category, QueryCreator queryCreator) { return Arrays.asList( - new TopCategoryQuery(category, queryCreator, "month", TimeUnit.DAYS.toMillis(30)), - new TopCategoryQuery(category, queryCreator, "week", TimeUnit.DAYS.toMillis(7)), - new TopCategoryQuery(category, queryCreator, "day", TimeUnit.DAYS.toMillis(1)), - new TopCategoryQuery(category, queryCreator, "total", System.currentTimeMillis()) + new TopCategoryQuery<>(category, queryCreator, "month", TimeUnit.DAYS.toMillis(30)), + new TopCategoryQuery<>(category, queryCreator, "week", TimeUnit.DAYS.toMillis(7)), + new TopCategoryQuery<>(category, queryCreator, "day", TimeUnit.DAYS.toMillis(1)), + new TopCategoryQuery<>(category, queryCreator, "total", System.currentTimeMillis()) ); } - interface QueryCreator { - Query> apply(Integer number, Long timespan, Arguments parameters); + interface QueryCreator { + Query>> apply(Integer number, Long timespan, Arguments parameters); } - public static class TopCategoryQuery { + public static class TopCategoryQuery { private final String category; - private final QueryCreator queryCreator; + private final QueryCreator queryCreator; private final String timeSpan; private final long timeSpanMillis; - public TopCategoryQuery(String category, QueryCreator queryCreator, String timeSpan, long timespan) { + public TopCategoryQuery(String category, QueryCreator queryCreator, String timeSpan, long timespan) { this.category = category; this.queryCreator = queryCreator; this.timeSpan = timeSpan; @@ -258,7 +265,7 @@ public class ServerPlaceHolders implements Placeholders { return timeSpan; } - public Query> getQuery(int i, Arguments parameters) { + public Query>> getQuery(int i, Arguments parameters) { return queryCreator.apply(i, timeSpanMillis, parameters); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java index 85da37bc2..244aadb7d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java @@ -35,7 +35,7 @@ public class TopListQueries { // Static query generation class } - public static Query> fetchNthTop10PlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) { + public static Query>> fetchNthTop10PlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) { String sql = SELECT + UsersTable.USER_NAME + ", " + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" + FROM + SessionsTable.TABLE_NAME + " s" + @@ -48,7 +48,7 @@ public class TopListQueries { LIMIT + "10" + OFFSET + "?"; - return new QueryStatement>(sql, 10) { + return new QueryStatement>>(sql, 10) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, serverUUID.toString()); @@ -58,9 +58,11 @@ public class TopListQueries { } @Override - public Optional processResults(ResultSet set) throws SQLException { + public Optional> processResults(ResultSet set) throws SQLException { if (set.next()) { - return Optional.of(set.getString(UsersTable.USER_NAME)); + return Optional.of( + new TopListEntry<>(set.getString(UsersTable.USER_NAME), set.getLong("playtime")) + ); } return Optional.empty(); } @@ -68,7 +70,7 @@ public class TopListQueries { } - public static Query> fetchNthTop10ActivePlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) { + public static Query>> fetchNthTop10ActivePlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) { String sql = SELECT + UsersTable.USER_NAME + ", " + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME + ") as active_playtime" + FROM + SessionsTable.TABLE_NAME + " s" + @@ -81,7 +83,7 @@ public class TopListQueries { LIMIT + "10" + OFFSET + "?"; - return new QueryStatement>(sql, 10) { + return new QueryStatement>>(sql, 10) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, serverUUID.toString()); @@ -91,12 +93,32 @@ public class TopListQueries { } @Override - public Optional processResults(ResultSet set) throws SQLException { + public Optional> processResults(ResultSet set) throws SQLException { if (set.next()) { - return Optional.of(set.getString(UsersTable.USER_NAME)); + return Optional.of( + new TopListEntry<>(set.getString(UsersTable.USER_NAME), set.getLong("active_playtime")) + ); } return Optional.empty(); } }; } + + public static class TopListEntry { + private final String playerName; + private final T value; + + public TopListEntry(String playerName, T value) { + this.playerName = playerName; + this.value = value; + } + + public String getPlayerName() { + return playerName; + } + + public T getValue() { + return value; + } + } } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java index ee49818db..702651f1d 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java @@ -46,7 +46,8 @@ public interface TopListQueriesTest extends DatabaseTestPreparer { String expected = TestConstants.PLAYER_ONE_NAME; String result = db().query(TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(serverUUID(), 1, 0, System.currentTimeMillis())) - .orElseThrow(AssertionError::new); + .orElseThrow(AssertionError::new) + .getPlayerName(); assertEquals(expected, result); } @@ -56,7 +57,8 @@ public interface TopListQueriesTest extends DatabaseTestPreparer { String expected = TestConstants.PLAYER_ONE_NAME; String result = db().query(TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(serverUUID(), 1, 0, System.currentTimeMillis())) - .orElseThrow(AssertionError::new); + .orElseThrow(AssertionError::new) + .getPlayerName(); assertEquals(expected, result); }