Average unique or new / day queries

This commit is contained in:
Rsl1122 2019-08-13 15:03:58 +03:00
parent a454091b7a
commit 3c421199ac
3 changed files with 81 additions and 21 deletions

View File

@ -111,6 +111,37 @@ public class PlayerCountQueries {
};
}
public static Query<Integer> averageUniquePlayerCount(long after, long before, long timeZoneOffset, UUID serverUUID) {
return database -> {
Sql sql = database.getType().getSql();
String selectUniquePlayersPerDay = SELECT +
sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + SessionsTable.SESSION_START + "+?)/1000"))) +
"*1000 as date," +
"COUNT(DISTINCT " + SessionsTable.USER_UUID + ") as player_count" +
FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.SESSION_END + "<=?" +
AND + SessionsTable.SESSION_START + ">=?" +
AND + SessionsTable.SERVER_UUID + "=?" +
GROUP_BY + "date";
String selectAverage = SELECT + "AVG(player_count) as average" + FROM + '(' + selectUniquePlayersPerDay + ") q1";
return database.query(new QueryStatement<Integer>(selectAverage, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, timeZoneOffset);
statement.setLong(2, before);
statement.setLong(3, after);
statement.setString(4, serverUUID.toString());
}
@Override
public Integer processResults(ResultSet set) throws SQLException {
return set.next() ? set.getInt("average") : 0;
}
});
};
}
public static Query<Integer> newPlayerCount(long after, long before, UUID serverUUID) {
String sql = SELECT + "COUNT(" + UserInfoTable.USER_UUID + ") as player_count" +
FROM + UserInfoTable.TABLE_NAME +
@ -163,4 +194,35 @@ public class PlayerCountQueries {
});
};
}
public static Query<Integer> averageNewPlayerCount(long after, long before, long timeZoneOffset, UUID serverUUID) {
return database -> {
Sql sql = database.getType().getSql();
String selectNewPlayersQuery = SELECT +
sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + UserInfoTable.REGISTERED + "+?)/1000"))) +
"*1000 as date," +
"COUNT(1) as player_count" +
FROM + UserInfoTable.TABLE_NAME +
WHERE + UserInfoTable.REGISTERED + "<=?" +
AND + UserInfoTable.REGISTERED + ">=?" +
AND + UserInfoTable.SERVER_UUID + "=?" +
GROUP_BY + "date";
String selectAverage = SELECT + "AVG(player_count) as average" + FROM + '(' + selectNewPlayersQuery + ") q1";
return database.query(new QueryStatement<Integer>(selectAverage, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, timeZoneOffset);
statement.setLong(2, before);
statement.setLong(3, after);
statement.setString(4, serverUUID.toString());
}
@Override
public Integer processResults(ResultSet set) throws SQLException {
return set.next() ? set.getInt("average") : 0;
}
});
};
}
}

View File

@ -29,15 +29,13 @@ import com.djrapitops.plan.db.access.queries.objects.UserInfoQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.DisplaySettings;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
@ -54,6 +52,7 @@ public class OnlineActivityOverviewJSONParser implements TabJSONParser<Map<Strin
private Formatter<Long> timeAmountFormatter;
private Formatter<Double> decimalFormatter;
private Formatter<Double> percentageFormatter;
private final TimeZone timeZone;
@Inject
public OnlineActivityOverviewJSONParser(
@ -67,6 +66,7 @@ public class OnlineActivityOverviewJSONParser implements TabJSONParser<Map<Strin
timeAmountFormatter = formatters.timeAmount();
decimalFormatter = formatters.decimals();
percentageFormatter = formatters.percentage();
this.timeZone = config.get(TimeSettings.USE_SERVER_TIME) ? TimeZone.getDefault() : TimeZone.getTimeZone("GMT");
}
public Map<String, Object> createJSONAsMap(UUID serverUUID) {
@ -83,6 +83,7 @@ public class OnlineActivityOverviewJSONParser implements TabJSONParser<Map<Strin
long weekAgo = now - TimeUnit.DAYS.toMillis(7L);
long halfMonthAgo = now - TimeUnit.DAYS.toMillis(15L);
long monthAgo = now - TimeUnit.DAYS.toMillis(30L);
int timeZoneOffset = timeZone.getOffset(now);
Map<String, Object> numbers = new HashMap<>();
@ -95,15 +96,14 @@ public class OnlineActivityOverviewJSONParser implements TabJSONParser<Map<Strin
numbers.put("unique_players_7d", db.query(PlayerCountQueries.uniquePlayerCount(weekAgo, now, serverUUID)));
numbers.put("unique_players_24h", db.query(PlayerCountQueries.uniquePlayerCount(dayAgo, now, serverUUID)));
// TODO
numbers.put("unique_players_30d_avg", -1);
numbers.put("unique_players_30d_avg", db.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo, now, timeZoneOffset, serverUUID)));
numbers.put("unique_players_30d_avg_trend", new Trend(
-1,
-1,
db.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo, halfMonthAgo, timeZoneOffset, serverUUID)),
db.query(PlayerCountQueries.averageUniquePlayerCount(halfMonthAgo, now, timeZoneOffset, serverUUID)),
false
));
numbers.put("unique_players_7d_avg", -1);
numbers.put("unique_players_24h_avg", -1);
numbers.put("unique_players_7d_avg", db.query(PlayerCountQueries.averageUniquePlayerCount(weekAgo, now, timeZoneOffset, serverUUID)));
numbers.put("unique_players_24h_avg", db.query(PlayerCountQueries.averageUniquePlayerCount(dayAgo, now, timeZoneOffset, serverUUID)));
numbers.put("new_players_30d", db.query(PlayerCountQueries.newPlayerCount(monthAgo, now, serverUUID)));
numbers.put("new_players_30d_trend", new Trend(
@ -114,15 +114,14 @@ public class OnlineActivityOverviewJSONParser implements TabJSONParser<Map<Strin
numbers.put("new_players_7d", db.query(PlayerCountQueries.newPlayerCount(weekAgo, now, serverUUID)));
numbers.put("new_players_24h", db.query(PlayerCountQueries.newPlayerCount(dayAgo, now, serverUUID)));
// TODO
numbers.put("new_players_30d_avg", -1);
numbers.put("new_players_30d_avg", db.query(PlayerCountQueries.averageNewPlayerCount(monthAgo, now, timeZoneOffset, serverUUID)));
numbers.put("new_players_30d_avg_trend", new Trend(
-1,
-1,
db.query(PlayerCountQueries.averageNewPlayerCount(monthAgo, halfMonthAgo, timeZoneOffset, serverUUID)),
db.query(PlayerCountQueries.averageNewPlayerCount(halfMonthAgo, now, timeZoneOffset, serverUUID)),
false
));
numbers.put("new_players_7d_avg", -1);
numbers.put("new_players_24h_avg", -1);
numbers.put("new_players_7d_avg", db.query(PlayerCountQueries.averageNewPlayerCount(weekAgo, now, timeZoneOffset, serverUUID)));
numbers.put("new_players_24h_avg", db.query(PlayerCountQueries.averageNewPlayerCount(dayAgo, now, timeZoneOffset, serverUUID)));
numbers.put("new_players_retention_30d", 0); // TODO
numbers.put("new_players_retention_30d_perc", percentageFormatter.apply(-1.0)); // TODO

View File

@ -37,10 +37,7 @@ import com.djrapitops.plan.utilities.formatting.Formatters;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
@ -60,6 +57,7 @@ public class ServerOverviewJSONParser implements TabJSONParser<Map<String, Objec
private Formatter<Double> decimals;
private Formatter<Double> percentage;
private Formatter<DateHolder> year;
private final TimeZone timeZone;
@Inject
public ServerOverviewJSONParser(
@ -77,6 +75,7 @@ public class ServerOverviewJSONParser implements TabJSONParser<Map<String, Objec
timeAmount = formatters.timeAmount();
decimals = formatters.decimals();
percentage = formatters.percentage();
this.timeZone = config.get(TimeSettings.USE_SERVER_TIME) ? TimeZone.getDefault() : TimeZone.getTimeZone("GMT");
}
public Map<String, Object> createJSONAsMap(UUID serverUUID) {
@ -95,7 +94,7 @@ public class ServerOverviewJSONParser implements TabJSONParser<Map<String, Objec
Map<String, Object> sevenDays = new HashMap<>();
sevenDays.put("unique_players", db.query(PlayerCountQueries.uniquePlayerCount(sevenDaysAgo, now, serverUUID)));
sevenDays.put("unique_players_day", "!"); // TODO
sevenDays.put("unique_players_day", db.query(PlayerCountQueries.averageUniquePlayerCount(sevenDaysAgo, now, timeZone.getOffset(now), serverUUID)));
sevenDays.put("new_players", db.query(PlayerCountQueries.newPlayerCount(sevenDaysAgo, now, serverUUID)));
sevenDays.put("new_players_retention", "!"); // TODO
sevenDays.put("new_players_retention_perc", "!"); // TODO