Fixed plugin group filters

This commit is contained in:
Risto Lahtela 2021-02-03 18:12:01 +02:00
parent 3f118a5ea8
commit 9aa9970d26
5 changed files with 125 additions and 43 deletions

View File

@ -0,0 +1,67 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation.providers;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
public class ProviderIdentifier {
private final UUID serverUUID;
private final String pluginName;
private final String providerName;
private String serverName;
public ProviderIdentifier(UUID serverUUID, String pluginName, String providerName) {
this.serverUUID = serverUUID;
this.pluginName = pluginName;
this.providerName = providerName;
}
public Optional<String> getServerName() {
return Optional.of(serverName);
}
public void setServerName(String serverName) {
this.serverName = serverName;
}
public UUID getServerUUID() {
return serverUUID;
}
public String getPluginName() {
return pluginName;
}
public String getProviderName() {
return providerName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProviderIdentifier that = (ProviderIdentifier) o;
return serverUUID.equals(that.serverUUID) && pluginName.equals(that.pluginName) && providerName.equals(that.providerName);
}
@Override
public int hashCode() {
return Objects.hash(serverUUID, pluginName, providerName);
}
}

View File

@ -32,19 +32,21 @@ public class ExtensionUUIDsInGroupQuery extends QueryStatement<Set<UUID>> {
private final String pluginName;
private final String groupProvider;
private final UUID serverUUID;
private final List<String> inGroups;
public ExtensionUUIDsInGroupQuery(String pluginName, String groupProvider, List<String> inGroups) {
public ExtensionUUIDsInGroupQuery(String pluginName, String groupProvider, UUID serverUUID, List<String> inGroups) {
super(buildSQL(inGroups), 100);
this.pluginName = pluginName;
this.groupProvider = groupProvider;
this.serverUUID = serverUUID;
this.inGroups = inGroups;
}
private static String buildSQL(Collection<String> inGroups) {
TextStringBuilder dynamicInClauseAllocation = new TextStringBuilder();
dynamicInClauseAllocation.appendWithSeparators(inGroups.stream().map(group -> "?").toArray(), ",");
return SELECT + ExtensionGroupsTable.USER_UUID +
return SELECT + DISTINCT + ExtensionGroupsTable.USER_UUID +
FROM + ExtensionGroupsTable.TABLE_NAME +
WHERE + ExtensionGroupsTable.PROVIDER_ID + "=" + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID +
AND + ExtensionGroupsTable.GROUP_NAME + " IN (" + dynamicInClauseAllocation.build() + ")";
@ -52,10 +54,11 @@ public class ExtensionUUIDsInGroupQuery extends QueryStatement<Set<UUID>> {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, groupProvider);
statement.setString(2, pluginName);
for (int i = 1; i <= inGroups.size(); i++) {
statement.setString(i + 2, inGroups.get(i));
ExtensionProviderTable.set3PluginValuesToStatement(statement, 1, groupProvider, pluginName, serverUUID);
int index = 4;
for (String group : inGroups) {
statement.setString(index, group);
index++;
}
}

View File

@ -38,6 +38,7 @@ public class QueryFilters {
private final Map<String, Filter> filters;
private final AllPlayersFilter allPlayersFilter;
private final DBSystem dbSystem;
private final PluginGroupsFilter.PluginGroupsFilterQuery filterQuery;
private final AtomicBoolean fetchedPluginFilters = new AtomicBoolean(false);
@ -45,10 +46,12 @@ public class QueryFilters {
public QueryFilters(
Set<Filter> filters,
AllPlayersFilter allPlayersFilter,
DBSystem dbSystem
DBSystem dbSystem,
PluginGroupsFilter.PluginGroupsFilterQuery filterQuery
) {
this.allPlayersFilter = allPlayersFilter;
this.dbSystem = dbSystem;
this.filterQuery = filterQuery;
this.filters = new HashMap<>();
put(filters);
}
@ -61,7 +64,7 @@ public class QueryFilters {
private void prepareFilters() {
if (!fetchedPluginFilters.get()) {
put(dbSystem.getDatabase().query(new PluginGroupsFilter.PluginGroupsFilterQuery(dbSystem)));
put(dbSystem.getDatabase().query(filterQuery));
fetchedPluginFilters.set(true);
}
}

View File

@ -16,15 +16,20 @@
*/
package com.djrapitops.plan.storage.database.queries.filter.filters;
import com.djrapitops.plan.extension.implementation.providers.ProviderIdentifier;
import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionUUIDsInGroupQuery;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.QueryAllStatement;
import com.djrapitops.plan.storage.database.queries.filter.SpecifiedFilterInformation;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionGroupsTable;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionPluginTable;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.sql.tables.ServerTable;
import com.djrapitops.plan.utilities.java.Maps;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -35,32 +40,27 @@ import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
public class PluginGroupsFilter extends MultiOptionFilter {
private final DBSystem dbSystem;
private final String pluginName;
private final String groupProvider;
private final List<String> groups;
private final ProviderIdentifier identifier;
private final String serverName;
public PluginGroupsFilter(
DBSystem dbSystem,
String pluginName,
String groupProvider,
List<String> groups
) {
public PluginGroupsFilter(DBSystem dbSystem, ProviderIdentifier identifier, List<String> groups) {
this.dbSystem = dbSystem;
this.pluginName = pluginName;
this.groupProvider = groupProvider;
this.identifier = identifier;
this.groups = groups;
this.serverName = identifier.getServerName().orElse("?");
}
@Override
public String getKind() {
return "pluginGroups-" + pluginName + "-" + groupProvider;
return "pluginGroups-" + serverName + " " + identifier.getPluginName() + " " + identifier.getProviderName();
}
@Override
public Map<String, Object> getOptions() {
return Maps.builder(String.class, Object.class)
.put("plugin", pluginName)
.put("group", groupProvider)
.put("plugin", identifier.getPluginName())
.put("group", identifier.getProviderName())
.put("options", groups)
.build();
}
@ -68,7 +68,7 @@ public class PluginGroupsFilter extends MultiOptionFilter {
@Override
public Set<UUID> getMatchingUUIDs(SpecifiedFilterInformation query) {
return dbSystem.getDatabase().query(
new ExtensionUUIDsInGroupQuery(pluginName, groupProvider, getSelected(query))
new ExtensionUUIDsInGroupQuery(identifier.getPluginName(), identifier.getProviderName(), identifier.getServerUUID(), getSelected(query))
);
}
@ -77,12 +77,18 @@ public class PluginGroupsFilter extends MultiOptionFilter {
private final DBSystem dbSystem;
public PluginGroupsFilterQuery(DBSystem dbSystem) {
super(SELECT + DISTINCT + "pl." + ExtensionPluginTable.PLUGIN_NAME + " as plugin_name," +
@Inject
public PluginGroupsFilterQuery(DBSystem dbSystem, ServerInfo serverInfo) {
super(SELECT + DISTINCT +
"pl." + ExtensionPluginTable.PLUGIN_NAME + " as plugin_name," +
"s." + ServerTable.NAME + " as server_name," +
"s." + ServerTable.SERVER_ID + " as server_id," +
"pl." + ExtensionPluginTable.SERVER_UUID + " as server_uuid," +
"pr." + ExtensionProviderTable.PROVIDER_NAME + " as provider_name," +
"gr." + ExtensionGroupsTable.GROUP_NAME + " as group_name" +
FROM + ExtensionPluginTable.TABLE_NAME + " pl" +
INNER_JOIN + ExtensionProviderTable.TABLE_NAME + " pr on pl." + ExtensionPluginTable.ID + "=pr." + ExtensionProviderTable.PLUGIN_ID +
INNER_JOIN + ServerTable.TABLE_NAME + " s on s." + ServerTable.SERVER_UUID + "=pl." + ExtensionPluginTable.SERVER_UUID +
INNER_JOIN + ExtensionProviderTable.TABLE_NAME + " pr on pl." + ExtensionPluginTable.ID + "=pr." + ExtensionProviderTable.PLUGIN_ID +
INNER_JOIN + ExtensionGroupsTable.TABLE_NAME + " gr on pr." + ExtensionProviderTable.ID + "=gr." + ExtensionGroupsTable.PROVIDER_ID);
this.dbSystem = dbSystem;
@ -90,29 +96,31 @@ public class PluginGroupsFilter extends MultiOptionFilter {
@Override
public Collection<PluginGroupsFilter> processResults(ResultSet set) throws SQLException {
Map<String, Map<String, List<String>>> byPlugin = new HashMap<>();
Map<ProviderIdentifier, List<String>> byProvider = new HashMap<>();
while (set.next()) {
String plugin = set.getString("plugin_name");
String provider = set.getString("provider_name");
UUID serverUUID = UUID.fromString(set.getString("server_uuid"));
ProviderIdentifier identifier = new ProviderIdentifier(serverUUID, plugin, provider);
identifier.setServerName(Server.getIdentifiableName(
set.getString("server_name"),
set.getInt("server_id")
));
String group = set.getString("group_name");
Map<String, List<String>> byProvider = byPlugin.getOrDefault(plugin, new HashMap<>());
List<String> groups = byProvider.getOrDefault(provider, new ArrayList<>());
List<String> groups = byProvider.getOrDefault(identifier, new ArrayList<>());
groups.add(group);
byProvider.put(provider, groups);
byPlugin.put(plugin, byProvider);
byProvider.put(identifier, groups);
}
List<PluginGroupsFilter> filters = new ArrayList<>();
for (Map.Entry<String, Map<String, List<String>>> providersOfPlugin : byPlugin.entrySet()) {
for (Map.Entry<String, List<String>> groupsOfProvider : providersOfPlugin.getValue().entrySet()) {
filters.add(new PluginGroupsFilter(
dbSystem,
providersOfPlugin.getKey(),
groupsOfProvider.getKey(),
groupsOfProvider.getValue()
));
}
for (Map.Entry<ProviderIdentifier, List<String>> groupsOfProvider : byProvider.entrySet()) {
filters.add(new PluginGroupsFilter(
dbSystem,
groupsOfProvider.getKey(),
groupsOfProvider.getValue()
));
}
return filters;
}

View File

@ -80,9 +80,9 @@ class OperatorsFilter extends MultipleChoiceFilter {
class PluginGroupsFilter extends MultipleChoiceFilter {
constructor(
id, plugin, group, options
id, kind, options
) {
super(id, `pluginGroups: ${plugin} ${group}`, `are in ${plugin}'s ${group} Groups`, options);
super(id, kind, `are in ${options.plugin}'s ${options.group} Groups`, options);
}
}
@ -177,6 +177,9 @@ class RegisteredBetweenFilter extends BetweenDateFilter {
}
function createFilter(filter, id) {
if (filter.kind.startsWith("pluginGroups-")) {
return new PluginGroupsFilter(id, filter.kind, filter.options);
}
switch (filter.kind) {
case "activityIndexNow":
return new ActivityIndexFilter(id, filter.options);
@ -184,8 +187,6 @@ function createFilter(filter, id) {
return new BannedFilter(id, filter.options);
case "operators":
return new OperatorsFilter(id, filter.options);
case "pluginGroups":
return new PluginGroupsFilter(id, filter.plugin, filter.group, filter.options);
case "playedBetween":
return new PlayedBetweenFilter(id, filter.options);
case "registeredBetween":