From 9aa9970d268ee764063378ea384676035dc68770 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Wed, 3 Feb 2021 18:12:01 +0200 Subject: [PATCH] Fixed plugin group filters --- .../providers/ProviderIdentifier.java | 67 ++++++++++++++++++ .../queries/ExtensionUUIDsInGroupQuery.java | 15 ++-- .../database/queries/filter/QueryFilters.java | 7 +- .../filter/filters/PluginGroupsFilter.java | 70 +++++++++++-------- .../resources/assets/plan/web/js/filters.js | 9 +-- 5 files changed, 125 insertions(+), 43 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/ProviderIdentifier.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/ProviderIdentifier.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/ProviderIdentifier.java new file mode 100644 index 000000000..8aed71227 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/ProviderIdentifier.java @@ -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 . + */ +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 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); + } +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionUUIDsInGroupQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionUUIDsInGroupQuery.java index 585d031d3..ff307ddd3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionUUIDsInGroupQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionUUIDsInGroupQuery.java @@ -32,19 +32,21 @@ public class ExtensionUUIDsInGroupQuery extends QueryStatement> { private final String pluginName; private final String groupProvider; + private final UUID serverUUID; private final List inGroups; - public ExtensionUUIDsInGroupQuery(String pluginName, String groupProvider, List inGroups) { + public ExtensionUUIDsInGroupQuery(String pluginName, String groupProvider, UUID serverUUID, List inGroups) { super(buildSQL(inGroups), 100); this.pluginName = pluginName; this.groupProvider = groupProvider; + this.serverUUID = serverUUID; this.inGroups = inGroups; } private static String buildSQL(Collection 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> { @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++; } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/QueryFilters.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/QueryFilters.java index c65ed4ba4..2820ce511 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/QueryFilters.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/QueryFilters.java @@ -38,6 +38,7 @@ public class QueryFilters { private final Map 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 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); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/filters/PluginGroupsFilter.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/filters/PluginGroupsFilter.java index b8478054e..8a10aa9ab 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/filters/PluginGroupsFilter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/filter/filters/PluginGroupsFilter.java @@ -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 groups; + private final ProviderIdentifier identifier; + private final String serverName; - public PluginGroupsFilter( - DBSystem dbSystem, - String pluginName, - String groupProvider, - List groups - ) { + public PluginGroupsFilter(DBSystem dbSystem, ProviderIdentifier identifier, List 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 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 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 processResults(ResultSet set) throws SQLException { - Map>> byPlugin = new HashMap<>(); + Map> 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> byProvider = byPlugin.getOrDefault(plugin, new HashMap<>()); - List groups = byProvider.getOrDefault(provider, new ArrayList<>()); + List groups = byProvider.getOrDefault(identifier, new ArrayList<>()); groups.add(group); - byProvider.put(provider, groups); - byPlugin.put(plugin, byProvider); + byProvider.put(identifier, groups); } List filters = new ArrayList<>(); - for (Map.Entry>> providersOfPlugin : byPlugin.entrySet()) { - for (Map.Entry> groupsOfProvider : providersOfPlugin.getValue().entrySet()) { - filters.add(new PluginGroupsFilter( - dbSystem, - providersOfPlugin.getKey(), - groupsOfProvider.getKey(), - groupsOfProvider.getValue() - )); - } + for (Map.Entry> groupsOfProvider : byProvider.entrySet()) { + filters.add(new PluginGroupsFilter( + dbSystem, + groupsOfProvider.getKey(), + groupsOfProvider.getValue() + )); } return filters; } diff --git a/Plan/common/src/main/resources/assets/plan/web/js/filters.js b/Plan/common/src/main/resources/assets/plan/web/js/filters.js index 2b54da541..5113c0c66 100644 --- a/Plan/common/src/main/resources/assets/plan/web/js/filters.js +++ b/Plan/common/src/main/resources/assets/plan/web/js/filters.js @@ -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":