Added %plan_top_{category}_{time}_{n}_value% placeholders

Affects issues:
- Close #2262
This commit is contained in:
Risto Lahtela 2022-02-01 16:29:43 +02:00
parent 735580c22e
commit 847500e6c4
3 changed files with 55 additions and 24 deletions

View File

@ -211,39 +211,46 @@ public class ServerPlaceHolders implements Placeholders {
}
private void registerDynamicCategoryPlaceholders(PlanPlaceholders placeholders, Database database) {
List<TopCategoryQuery> queries = new ArrayList<>();
List<TopCategoryQuery<Long>> 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<Long> 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<TopCategoryQuery> createCategoryQueriesForAllTimespans(String category, QueryCreator queryCreator) {
private <T> List<TopCategoryQuery<T>> createCategoryQueriesForAllTimespans(String category, QueryCreator<T> 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<Optional<String>> apply(Integer number, Long timespan, Arguments parameters);
interface QueryCreator<T> {
Query<Optional<TopListQueries.TopListEntry<T>>> apply(Integer number, Long timespan, Arguments parameters);
}
public static class TopCategoryQuery {
public static class TopCategoryQuery<T> {
private final String category;
private final QueryCreator queryCreator;
private final QueryCreator<T> queryCreator;
private final String timeSpan;
private final long timeSpanMillis;
public TopCategoryQuery(String category, QueryCreator queryCreator, String timeSpan, long timespan) {
public TopCategoryQuery(String category, QueryCreator<T> 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<Optional<String>> getQuery(int i, Arguments parameters) {
public Query<Optional<TopListQueries.TopListEntry<T>>> getQuery(int i, Arguments parameters) {
return queryCreator.apply(i, timeSpanMillis, parameters);
}
}

View File

@ -35,7 +35,7 @@ public class TopListQueries {
// Static query generation class
}
public static Query<Optional<String>> fetchNthTop10PlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) {
public static Query<Optional<TopListEntry<Long>>> 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<Optional<String>>(sql, 10) {
return new QueryStatement<Optional<TopListEntry<Long>>>(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<String> processResults(ResultSet set) throws SQLException {
public Optional<TopListEntry<Long>> 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<Optional<String>> fetchNthTop10ActivePlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) {
public static Query<Optional<TopListEntry<Long>>> 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<Optional<String>>(sql, 10) {
return new QueryStatement<Optional<TopListEntry<Long>>>(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<String> processResults(ResultSet set) throws SQLException {
public Optional<TopListEntry<Long>> 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<T> {
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;
}
}
}

View File

@ -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);
}