Implemented regular player count query

This commit is contained in:
Rsl1122 2019-07-07 16:34:02 +03:00
parent b2a02a3576
commit bbe5c8ed67
4 changed files with 88 additions and 12 deletions

View File

@ -57,6 +57,11 @@ import java.util.Optional;
*/
public class ActivityIndex {
public static final double VERY_ACTIVE = 3.75;
public static final double ACTIVE = 3.0;
public static final double REGULAR = 2.0;
public static final double IRREGULAR = 1.0;
private final double value;
private final long date;
@ -126,13 +131,13 @@ public class ActivityIndex {
}
public String getGroup() {
if (value >= 3.75) {
if (value >= VERY_ACTIVE) {
return "Very Active";
} else if (value >= 3) {
} else if (value >= ACTIVE) {
return "Active";
} else if (value >= 2) {
} else if (value >= REGULAR) {
return "Regular";
} else if (value >= 1) {
} else if (value >= IRREGULAR) {
return "Irregular";
} else {
return "Inactive";
@ -140,13 +145,13 @@ public class ActivityIndex {
}
public String getColor() {
if (value >= 3.75) {
if (value >= VERY_ACTIVE) {
return "green";
} else if (value >= 3) {
} else if (value >= ACTIVE) {
return "green";
} else if (value >= 2) {
} else if (value >= REGULAR) {
return "lime";
} else if (value >= 1) {
} else if (value >= IRREGULAR) {
return "amber";
} else {
return "blue-gray";

View File

@ -16,6 +16,19 @@
*/
package com.djrapitops.plan.db.access.queries.analysis;
import com.djrapitops.plan.data.store.mutators.ActivityIndex;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.SessionsTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
/**
* Queries for Activity Index that attempts to gain insight into player activity levels.
* <p>
@ -49,4 +62,55 @@ package com.djrapitops.plan.db.access.queries.analysis;
* @author Rsl1122
*/
public class ActivityIndexQueries {
public static Query<Integer> fetchRegularPlayerCount(long date, UUID serverUUID, long playtimeThreshold) {
String selectActivePlaytimeSQL = SELECT +
SessionsTable.USER_UUID +
",SUM(" +
SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME +
") as active_playtime" +
FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.SERVER_UUID + "=?" +
AND + SessionsTable.SESSION_START + ">=?" +
AND + SessionsTable.SESSION_END + "<=?" +
GROUP_BY + SessionsTable.USER_UUID;
String selectThreeWeeks = selectActivePlaytimeSQL + UNION + selectActivePlaytimeSQL + UNION + selectActivePlaytimeSQL;
String selectActivityIndex = SELECT +
"5.0 - 5.0 * AVG(1 / (?/2 * (q1.active_playtime/?) +1)) as activity_index," +
"q1." + SessionsTable.USER_UUID +
FROM + '(' + selectThreeWeeks + ") q1" +
GROUP_BY + "q1." + SessionsTable.USER_UUID;
String selectActivePlayerCount = SELECT + "COUNT(1) as count" +
FROM + '(' + selectActivityIndex + ") q2" +
WHERE + "q2.activity_index>=?";
return new QueryStatement<Integer>(selectActivePlayerCount) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setDouble(1, Math.PI);
statement.setLong(2, playtimeThreshold);
statement.setString(3, serverUUID.toString());
statement.setLong(4, date - TimeUnit.DAYS.toMillis(7L));
statement.setLong(5, date);
statement.setString(6, serverUUID.toString());
statement.setLong(7, date - TimeUnit.DAYS.toMillis(14L));
statement.setLong(8, date - TimeUnit.DAYS.toMillis(7L));
statement.setString(9, serverUUID.toString());
statement.setLong(10, date - TimeUnit.DAYS.toMillis(21L));
statement.setLong(11, date - TimeUnit.DAYS.toMillis(14L));
statement.setDouble(12, ActivityIndex.REGULAR);
}
@Override
public Integer processResults(ResultSet set) throws SQLException {
return set.next() ? set.getInt("count") : 0;
}
};
}
}

View File

@ -34,6 +34,7 @@ public class Sql {
public static final String ORDER_BY = " ORDER BY ";
public static final String INNER_JOIN = " INNER JOIN ";
public static final String LEFT_JOIN = " LEFT JOIN ";
public static final String UNION = " UNION ";
public static final String AND = " AND ";
public static final String OR = " OR ";
public static final String IS_NULL = " IS NULL";

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.data.store.objects.DateHolder;
import com.djrapitops.plan.data.store.objects.DateObj;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.queries.analysis.ActivityIndexQueries;
import com.djrapitops.plan.db.access.queries.analysis.PlayerCountQueries;
import com.djrapitops.plan.db.access.queries.objects.KillQueries;
import com.djrapitops.plan.db.access.queries.objects.SessionQueries;
@ -30,6 +31,7 @@ import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.ServerInfo;
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;
@ -107,11 +109,12 @@ public class ServerOverviewJSONParser {
Database db = dbSystem.getDatabase();
long now = System.currentTimeMillis();
long twoDaysAgo = now - TimeUnit.DAYS.toMillis(2L);
Long playtimeThreshold = config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD);
Map<String, Object> numbers = new HashMap<>();
numbers.put("total_players", db.query(ServerAggregateQueries.serverUserCount(serverUUID)));
numbers.put("regular_players", 0); // TODO
numbers.put("regular_players", db.query(ActivityIndexQueries.fetchRegularPlayerCount(now, serverUUID, playtimeThreshold)));
numbers.put("online_players", getOnlinePlayers(serverUUID, db));
Optional<DateObj<Integer>> lastPeak = db.query(TPSQueries.fetchPeakPlayerCount(serverUUID, twoDaysAgo));
Optional<DateObj<Integer>> allTimePeak = db.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID));
@ -142,6 +145,7 @@ public class ServerOverviewJSONParser {
long now = System.currentTimeMillis();
long oneWeekAgo = now - TimeUnit.DAYS.toMillis(7L);
long twoWeeksAgo = now - TimeUnit.DAYS.toMillis(14L);
Long playtimeThreshold = config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD);
Map<String, Object> weeks = new HashMap<>();
@ -159,9 +163,11 @@ public class ServerOverviewJSONParser {
weeks.put("new_after", newAfter);
weeks.put("new_trend", newTrend);
weeks.put("regular_before", "-"); // TODO
weeks.put("regular_after", "-");
weeks.put("regular_trend", new Trend(0, 0, false));
int regularBefore = db.query(ActivityIndexQueries.fetchRegularPlayerCount(oneWeekAgo, serverUUID, playtimeThreshold));
int regularAfter = db.query(ActivityIndexQueries.fetchRegularPlayerCount(now, serverUUID, playtimeThreshold));
weeks.put("regular_before", regularBefore);
weeks.put("regular_after", regularAfter);
weeks.put("regular_trend", new Trend(regularBefore, regularAfter, false));
Long playtimeBefore = db.query(SessionQueries.playtime(twoWeeksAgo, oneWeekAgo, serverUUID));
Long playtimeAfter = db.query(SessionQueries.playtime(oneWeekAgo, now, serverUUID));