mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-01 14:07:54 +01:00
Optimized query for activity group graphs
- Removed 5-way union - Removed 2 nested queries - Calculated groups with java code from the queried activity index values. Affects issues: - Improvement for #1329
This commit is contained in:
parent
68fba31303
commit
4cca2251a8
@ -163,7 +163,7 @@ public class ActivityIndex {
|
||||
return Math.abs(Math.log(other.value) - Math.log(value));
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
public static String getGroup(double value) {
|
||||
if (value >= VERY_ACTIVE) {
|
||||
return HtmlLang.INDEX_VERY_ACTIVE.getDefault();
|
||||
} else if (value >= ACTIVE) {
|
||||
@ -176,4 +176,8 @@ public class ActivityIndex {
|
||||
return HtmlLang.INDEX_INACTIVE.getDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return getGroup(value);
|
||||
}
|
||||
}
|
||||
|
@ -142,53 +142,27 @@ public class ActivityIndexQueries {
|
||||
public static Query<Map<String, Integer>> fetchActivityIndexGroupingsOn(long date, UUID serverUUID, long threshold) {
|
||||
String selectActivityIndex = selectActivityIndexSQL();
|
||||
|
||||
// ? inside SELECT needs to be implicitly cast to a type because of H2 compile time type checks.
|
||||
String selectIndexes = SELECT + "CAST(? as CHAR) as activity_group, activity_index" +
|
||||
String selectIndexes = SELECT + "activity_index" +
|
||||
FROM + UserInfoTable.TABLE_NAME + " u" +
|
||||
LEFT_JOIN + '(' + selectActivityIndex + ") s on s." + SessionsTable.USER_UUID + "=u." + UserInfoTable.USER_UUID +
|
||||
WHERE + "u." + UserInfoTable.SERVER_UUID + "=?" +
|
||||
AND + "u." + UserInfoTable.REGISTERED + "<=?";
|
||||
|
||||
String selectCount = SELECT + "indexes.activity_group, COUNT(1) as count" + FROM +
|
||||
'(' + selectIndexes + ") indexes" +
|
||||
WHERE + "COALESCE(indexes.activity_index,0)>=?" +
|
||||
AND + "COALESCE(indexes.activity_index,0)<?" +
|
||||
GROUP_BY + "indexes.activity_group";
|
||||
|
||||
String selectMultipleCounts = SELECT + '*' + FROM +
|
||||
'(' + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount + ") selectMultiple";
|
||||
|
||||
return new QueryStatement<Map<String, Integer>>(selectMultipleCounts) {
|
||||
return new QueryStatement<Map<String, Integer>>(selectIndexes) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
set16ValuesForQuery(statement, 1, "Very Active", ActivityIndex.VERY_ACTIVE, 5.1);
|
||||
set16ValuesForQuery(statement, 17, "Active", ActivityIndex.ACTIVE, ActivityIndex.VERY_ACTIVE);
|
||||
set16ValuesForQuery(statement, 33, "Regular", ActivityIndex.REGULAR, ActivityIndex.ACTIVE);
|
||||
set16ValuesForQuery(statement, 49, "Irregular", ActivityIndex.IRREGULAR, ActivityIndex.REGULAR);
|
||||
set16ValuesForQuery(statement, 65, "Inactive", -1, ActivityIndex.IRREGULAR);
|
||||
}
|
||||
|
||||
private void set16ValuesForQuery(
|
||||
PreparedStatement statement, int index,
|
||||
String group, double above, double below
|
||||
) throws SQLException {
|
||||
statement.setString(index, group);
|
||||
setSelectActivityIndexSQLParameters(statement, index + 1, threshold, serverUUID, date);
|
||||
statement.setString(index + 12, serverUUID.toString());
|
||||
statement.setLong(index + 13, date);
|
||||
statement.setDouble(index + 14, above);
|
||||
statement.setDouble(index + 15, below);
|
||||
setSelectActivityIndexSQLParameters(statement, 1, threshold, serverUUID, date);
|
||||
statement.setString(12, serverUUID.toString());
|
||||
statement.setLong(13, date);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Integer> groups = new HashMap<>();
|
||||
while (set.next()) {
|
||||
groups.put(set.getString("activity_group"), set.getInt("count"));
|
||||
double activityIndex = set.getDouble("activity_index");
|
||||
String group = ActivityIndex.getGroup(activityIndex);
|
||||
groups.put(group, groups.getOrDefault(group, 0) + 1);
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
@ -139,50 +139,25 @@ public class NetworkActivityIndexQueries {
|
||||
public static Query<Map<String, Integer>> fetchActivityIndexGroupingsOn(long date, long threshold) {
|
||||
String selectActivityIndex = selectActivityIndexSQL();
|
||||
|
||||
String selectIndexes = SELECT + "? as activity_group, activity_index" +
|
||||
String selectIndexes = SELECT + "activity_index" +
|
||||
FROM + UsersTable.TABLE_NAME + " u" +
|
||||
LEFT_JOIN + '(' + selectActivityIndex + ") s on s." + SessionsTable.USER_UUID + "=u." + UsersTable.USER_UUID +
|
||||
AND + "u." + UsersTable.REGISTERED + "<=?";
|
||||
WHERE + "u." + UsersTable.REGISTERED + "<=?";
|
||||
|
||||
String selectCount = SELECT + "activity_group, COUNT(1) as count" + FROM +
|
||||
'(' + selectIndexes + ") indexes" +
|
||||
WHERE + "COALESCE(indexes.activity_index,0)>=?" +
|
||||
AND + "COALESCE(indexes.activity_index,0)<?" +
|
||||
GROUP_BY + "activity_group";
|
||||
|
||||
String selectMultipleCounts = SELECT + '*' + FROM +
|
||||
'(' + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount +
|
||||
UNION + selectCount + ") selectMultiple";
|
||||
|
||||
return new QueryStatement<Map<String, Integer>>(selectMultipleCounts) {
|
||||
return new QueryStatement<Map<String, Integer>>(selectIndexes) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
set12ValuesForQuery(statement, 1, "Very Active", ActivityIndex.VERY_ACTIVE, 5.1);
|
||||
set12ValuesForQuery(statement, 13, "Active", ActivityIndex.ACTIVE, ActivityIndex.VERY_ACTIVE);
|
||||
set12ValuesForQuery(statement, 25, "Regular", ActivityIndex.REGULAR, ActivityIndex.ACTIVE);
|
||||
set12ValuesForQuery(statement, 37, "Irregular", ActivityIndex.IRREGULAR, ActivityIndex.REGULAR);
|
||||
set12ValuesForQuery(statement, 49, "Inactive", -1, ActivityIndex.IRREGULAR);
|
||||
}
|
||||
|
||||
private void set12ValuesForQuery(
|
||||
PreparedStatement statement, int index,
|
||||
String group, double above, double below
|
||||
) throws SQLException {
|
||||
statement.setString(index, group);
|
||||
setSelectActivityIndexSQLParameters(statement, index + 1, threshold, date);
|
||||
statement.setLong(index + 9, date);
|
||||
statement.setDouble(index + 10, above);
|
||||
statement.setDouble(index + 11, below);
|
||||
setSelectActivityIndexSQLParameters(statement, 1, threshold, date);
|
||||
statement.setLong(9, date);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> processResults(ResultSet set) throws SQLException {
|
||||
Map<String, Integer> groups = new HashMap<>();
|
||||
while (set.next()) {
|
||||
groups.put(set.getString("activity_group"), set.getInt("count"));
|
||||
double activityIndex = set.getDouble("activity_index");
|
||||
String group = ActivityIndex.getGroup(activityIndex);
|
||||
groups.put(group, groups.getOrDefault(group, 0) + 1);
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user