mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-03-09 13:19:07 +01:00
ExtensionServerDataQuery + BooleanAggregateQuery:
- Queries plan_extension_server_values - Queries plan_extension_player_values for aggregates
This commit is contained in:
parent
c8687b7c48
commit
7ca0c04365
@ -143,4 +143,23 @@ public class ExtensionServiceImplementation implements ExtensionService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateServerValues() {
|
||||
for (Map.Entry<String, ProviderValueGatherer> gatherer : extensionGatherers.entrySet()) {
|
||||
try {
|
||||
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering values for server");
|
||||
|
||||
gatherer.getValue().updateValues();
|
||||
|
||||
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering completed for server");
|
||||
} catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) {
|
||||
logger.warn(gatherer.getKey() + " ran into (but failed safely) " + e.getClass().getSimpleName() +
|
||||
" when updating value for server" +
|
||||
", (You can disable integration with setting 'Plugins." + gatherer.getKey() + ".Enabled')" +
|
||||
" reason: '" + e.getMessage() +
|
||||
"', stack trace to follow:");
|
||||
errorHandler.log(L.WARN, gatherer.getValue().getClass(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -83,6 +83,14 @@ public class ExtensionTabData implements Comparable<ExtensionTabData> {
|
||||
return Integer.compare(this.tabInformation.getTabPriority(), other.tabInformation.getTabPriority()); // Lower is first
|
||||
}
|
||||
|
||||
public void combine(ExtensionTabData other) {
|
||||
this.booleanData.putAll(other.booleanData);
|
||||
this.doubleData.putAll(other.doubleData);
|
||||
this.percentageData.putAll(other.percentageData);
|
||||
this.numberData.putAll(other.numberData);
|
||||
this.stringData.putAll(other.stringData);
|
||||
}
|
||||
|
||||
public static class Factory {
|
||||
|
||||
private final ExtensionTabData data;
|
||||
|
@ -19,9 +19,7 @@ package com.djrapitops.plan.extension.implementation.results.server;
|
||||
import com.djrapitops.plan.extension.implementation.results.ExtensionInformation;
|
||||
import com.djrapitops.plan.extension.implementation.results.ExtensionTabData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Represents data of a single extension about a server.
|
||||
@ -34,12 +32,12 @@ public class ExtensionServerData implements Comparable<ExtensionServerData> {
|
||||
|
||||
private ExtensionInformation extensionInformation;
|
||||
|
||||
private List<ExtensionTabData> tabs;
|
||||
private Map<String, ExtensionTabData> tabs;
|
||||
|
||||
private ExtensionServerData(int pluginID) {
|
||||
this.pluginID = pluginID;
|
||||
|
||||
tabs = new ArrayList<>();
|
||||
tabs = new HashMap<>();
|
||||
}
|
||||
|
||||
public int getPluginID() {
|
||||
@ -51,11 +49,13 @@ public class ExtensionServerData implements Comparable<ExtensionServerData> {
|
||||
}
|
||||
|
||||
public boolean hasOnlyGenericTab() {
|
||||
return tabs.size() == 1 && tabs.get(0).getTabInformation().getTabName().isEmpty();
|
||||
return tabs.size() == 1 && tabs.containsKey("");
|
||||
}
|
||||
|
||||
public List<ExtensionTabData> getTabs() {
|
||||
return tabs;
|
||||
List<ExtensionTabData> tabList = new ArrayList<>(tabs.values());
|
||||
Collections.sort(tabList);
|
||||
return tabList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -71,6 +71,20 @@ public class ExtensionServerData implements Comparable<ExtensionServerData> {
|
||||
data = new ExtensionServerData(pluginId);
|
||||
}
|
||||
|
||||
public Factory combine(Factory with) {
|
||||
if (with != null) {
|
||||
for (ExtensionTabData tab : with.build().getTabs()) {
|
||||
Optional<ExtensionTabData> found = getTab(tab.getTabInformation().getTabName());
|
||||
if (found.isPresent()) {
|
||||
found.get().combine(tab);
|
||||
} else {
|
||||
addTab(tab);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Factory setInformation(ExtensionInformation information) {
|
||||
if (information.getId() != data.pluginID) {
|
||||
throw new IllegalArgumentException("ID mismatch, wanted id: " + data.pluginID + " but got " + information);
|
||||
@ -80,12 +94,15 @@ public class ExtensionServerData implements Comparable<ExtensionServerData> {
|
||||
}
|
||||
|
||||
public Factory addTab(ExtensionTabData tab) {
|
||||
data.tabs.add(tab);
|
||||
data.tabs.put(tab.getTabInformation().getTabName(), tab);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Optional<ExtensionTabData> getTab(String tabName) {
|
||||
return Optional.ofNullable(data.tabs.get(tabName));
|
||||
}
|
||||
|
||||
public ExtensionServerData build() {
|
||||
Collections.sort(data.tabs);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,200 @@
|
||||
/*
|
||||
* 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.storage.queries;
|
||||
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.tables.*;
|
||||
import com.djrapitops.plan.extension.ElementOrder;
|
||||
import com.djrapitops.plan.extension.icon.Color;
|
||||
import com.djrapitops.plan.extension.icon.Family;
|
||||
import com.djrapitops.plan.extension.icon.Icon;
|
||||
import com.djrapitops.plan.extension.implementation.TabInformation;
|
||||
import com.djrapitops.plan.extension.implementation.results.ExtensionDescriptive;
|
||||
import com.djrapitops.plan.extension.implementation.results.ExtensionDoubleData;
|
||||
import com.djrapitops.plan.extension.implementation.results.ExtensionTabData;
|
||||
import com.djrapitops.plan.extension.implementation.results.server.ExtensionServerData;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
|
||||
|
||||
/**
|
||||
* Query aggregated boolean values from player value table.
|
||||
* <p>
|
||||
* Returns Map: PluginID - ExtensionServerData.Factory.
|
||||
* <p>
|
||||
* How it is done:
|
||||
* - Combines three queries, one that selects true boolean count, one that selects boolean value count and one that selects provider information.
|
||||
* - Data query is sorted into a multi-map: PluginID - Tab Name - Tab Data
|
||||
* - (Tab Name can be empty.)
|
||||
* - Multi-map is sorted into ExtensionPlayerData objects by PluginID, one per ID
|
||||
* <p>
|
||||
* There are multiple data extraction methods to make extracting the value query easier.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ExtensionAggregateBooleansQuery implements Query<Map<Integer, ExtensionServerData.Factory>> {
|
||||
|
||||
private final UUID serverUUID;
|
||||
|
||||
public ExtensionAggregateBooleansQuery(UUID serverUUID) {
|
||||
this.serverUUID = serverUUID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, ExtensionServerData.Factory> executeQuery(SQLDB db) {
|
||||
String selectTrueBooleans = SELECT +
|
||||
ExtensionPlayerValueTable.PROVIDER_ID +
|
||||
",COUNT(1) as positive" +
|
||||
FROM + ExtensionPlayerValueTable.TABLE_NAME +
|
||||
WHERE + ExtensionPlayerValueTable.BOOLEAN_VALUE + "=?" +
|
||||
GROUP_BY + ExtensionPlayerValueTable.PROVIDER_ID;
|
||||
|
||||
String selectBooleanCount = SELECT +
|
||||
ExtensionPlayerValueTable.PROVIDER_ID +
|
||||
",COUNT(1) as total" +
|
||||
FROM + ExtensionPlayerValueTable.TABLE_NAME +
|
||||
WHERE + ExtensionPlayerValueTable.BOOLEAN_VALUE + IS_NOT_NULL +
|
||||
GROUP_BY + ExtensionPlayerValueTable.PROVIDER_ID;
|
||||
|
||||
String sql = SELECT +
|
||||
"b1.total as total," +
|
||||
"b2.positive as positive," +
|
||||
"p1." + ExtensionProviderTable.PLUGIN_ID + " as plugin_id," +
|
||||
"p1." + ExtensionProviderTable.PROVIDER_NAME + " as provider_name," +
|
||||
"p1." + ExtensionProviderTable.TEXT + " as text," +
|
||||
"p1." + ExtensionProviderTable.DESCRIPTION + " as description," +
|
||||
"p1." + ExtensionProviderTable.PRIORITY + " as provider_priority," +
|
||||
"p1." + ExtensionProviderTable.FORMAT_TYPE + " as format_type," +
|
||||
"p1." + ExtensionProviderTable.IS_PLAYER_NAME + " as is_player_name," +
|
||||
"t1." + ExtensionTabTable.TAB_NAME + " as tab_name," +
|
||||
"t1." + ExtensionTabTable.TAB_PRIORITY + " as tab_priority," +
|
||||
"t1." + ExtensionTabTable.ELEMENT_ORDER + " as element_order," +
|
||||
"i1." + ExtensionIconTable.ICON_NAME + " as provider_icon_name," +
|
||||
"i1." + ExtensionIconTable.FAMILY + " as provider_icon_family," +
|
||||
"i1." + ExtensionIconTable.COLOR + " as provider_icon_color," +
|
||||
"i2." + ExtensionIconTable.ICON_NAME + " as tab_icon_name," +
|
||||
"i2." + ExtensionIconTable.FAMILY + " as tab_icon_family," +
|
||||
"i2." + ExtensionIconTable.COLOR + " as tab_icon_color" +
|
||||
FROM + '(' + selectBooleanCount + ") b1" +
|
||||
INNER_JOIN + ExtensionProviderTable.TABLE_NAME + " p1 on p1." + ExtensionProviderTable.ID + "=b1." + ExtensionPlayerValueTable.PROVIDER_ID +
|
||||
INNER_JOIN + ExtensionPluginTable.TABLE_NAME + " e1 on p1." + ExtensionProviderTable.PLUGIN_ID + "=e1." + ExtensionPluginTable.ID +
|
||||
LEFT_JOIN + '(' + selectTrueBooleans + ") b2 on b2." + ExtensionPlayerValueTable.PROVIDER_ID + "=b1." + ExtensionPlayerValueTable.PROVIDER_ID +
|
||||
LEFT_JOIN + ExtensionTabTable.TABLE_NAME + " t1 on t1." + ExtensionTabTable.ID + "=p1." + ExtensionProviderTable.TAB_ID +
|
||||
LEFT_JOIN + ExtensionIconTable.TABLE_NAME + " i1 on i1." + ExtensionIconTable.ID + "=p1." + ExtensionProviderTable.ICON_ID +
|
||||
LEFT_JOIN + ExtensionIconTable.TABLE_NAME + " i2 on i2." + ExtensionIconTable.ID + "=p1." + ExtensionTabTable.ICON_ID +
|
||||
WHERE + ExtensionPluginTable.SERVER_UUID + "=?" +
|
||||
AND + "p1." + ExtensionProviderTable.HIDDEN + "=?";
|
||||
|
||||
return db.query(new QueryStatement<Map<Integer, ExtensionServerData.Factory>>(sql, 1000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setBoolean(1, true); // selectTrueBooleans parameter
|
||||
statement.setString(2, serverUUID.toString());
|
||||
statement.setBoolean(3, false); // Don't select hidden values
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, ExtensionServerData.Factory> processResults(ResultSet set) throws SQLException {
|
||||
Map<Integer, Map<String, ExtensionTabData.Factory>> tabDataByPluginID = extractTabDataByPluginID(set);
|
||||
return ExtensionServerDataQuery.flatMapToServerData(tabDataByPluginID);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Map<Integer, Map<String, ExtensionTabData.Factory>> extractTabDataByPluginID(ResultSet set) throws SQLException {
|
||||
Map<Integer, Map<String, ExtensionTabData.Factory>> tabDataByPluginID = new HashMap<>();
|
||||
|
||||
while (set.next()) {
|
||||
int pluginID = set.getInt("plugin_id");
|
||||
Map<String, ExtensionTabData.Factory> tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>());
|
||||
|
||||
String tabName = Optional.ofNullable(set.getString("tab_name")).orElse("");
|
||||
ExtensionTabData.Factory inMap = tabData.get(tabName);
|
||||
ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData);
|
||||
|
||||
ExtensionDescriptive extensionDescriptive = extractDescriptive(set);
|
||||
extractAndPutDataTo(extensionTab, extensionDescriptive, set);
|
||||
|
||||
tabData.put(tabName, extensionTab);
|
||||
tabDataByPluginID.put(pluginID, tabData);
|
||||
}
|
||||
return tabDataByPluginID;
|
||||
}
|
||||
|
||||
private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException {
|
||||
double percentageValue = percentage(set.getInt("positive"), set.getInt("total"));
|
||||
extensionTab.putPercentageData(new ExtensionDoubleData(descriptive, percentageValue));
|
||||
}
|
||||
|
||||
private double percentage(double first, double second) {
|
||||
if (first == 0.0 || second == 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
return first / second;
|
||||
}
|
||||
|
||||
private ExtensionDescriptive extractDescriptive(ResultSet set) throws SQLException {
|
||||
String name = set.getString("provider_name") + "_aggregate";
|
||||
String text = set.getString(ExtensionProviderTable.TEXT);
|
||||
String description = set.getString(ExtensionProviderTable.DESCRIPTION);
|
||||
int priority = set.getInt("provider_priority");
|
||||
|
||||
String iconName = set.getString("provider_icon_name");
|
||||
Family family = Family.getByName(set.getString("provider_icon_family")).orElse(Family.SOLID);
|
||||
Color color = Color.getByName(set.getString("provider_icon_color")).orElse(Color.NONE);
|
||||
Icon icon = new Icon(family, iconName, color);
|
||||
|
||||
return new ExtensionDescriptive(name, text, description, icon, priority);
|
||||
}
|
||||
|
||||
private ExtensionTabData.Factory extractTab(String tabName, ResultSet set, Map<String, ExtensionTabData.Factory> tabData) throws SQLException {
|
||||
Optional<Integer> tabPriority = Optional.of(set.getInt("tab_priority"));
|
||||
if (set.wasNull()) {
|
||||
tabPriority = Optional.empty();
|
||||
}
|
||||
Optional<ElementOrder[]> elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize);
|
||||
|
||||
Icon tabIcon = extractTabIcon(set);
|
||||
|
||||
return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation(
|
||||
tabName,
|
||||
tabIcon,
|
||||
elementOrder.orElse(ElementOrder.values()),
|
||||
tabPriority.orElse(100)
|
||||
)));
|
||||
}
|
||||
|
||||
private Icon extractTabIcon(ResultSet set) throws SQLException {
|
||||
Optional<String> iconName = Optional.ofNullable(set.getString("tab_icon_name"));
|
||||
if (iconName.isPresent()) {
|
||||
Family iconFamily = Family.getByName(set.getString("tab_icon_family")).orElse(Family.SOLID);
|
||||
Color iconColor = Color.getByName(set.getString("tab_icon_color")).orElse(Color.NONE);
|
||||
return new Icon(iconFamily, iconName.get(), iconColor);
|
||||
} else {
|
||||
return TabInformation.defaultIcon();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,261 @@
|
||||
/*
|
||||
* 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.storage.queries;
|
||||
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.tables.*;
|
||||
import com.djrapitops.plan.extension.ElementOrder;
|
||||
import com.djrapitops.plan.extension.FormatType;
|
||||
import com.djrapitops.plan.extension.icon.Color;
|
||||
import com.djrapitops.plan.extension.icon.Family;
|
||||
import com.djrapitops.plan.extension.icon.Icon;
|
||||
import com.djrapitops.plan.extension.implementation.TabInformation;
|
||||
import com.djrapitops.plan.extension.implementation.results.*;
|
||||
import com.djrapitops.plan.extension.implementation.results.server.ExtensionServerData;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
|
||||
|
||||
/**
|
||||
* Query ExtensionServerData of a server.
|
||||
* <p>
|
||||
* Returns List of ExtensionServerData.
|
||||
* <p>
|
||||
* How it is done:
|
||||
* - Two queries are run, one that fetches all extensions and one that fetches all data of the player.
|
||||
* - Data query is sorted into a multi-map: PluginID - Tab Name - Tab Data
|
||||
* - (Tab Name can be empty.)
|
||||
* - Multi-map is sorted into ExtensionServerData objects by PluginID, one per ID
|
||||
* - This map is combined with similar maps that contain aggregated player values
|
||||
* - This map is sorted into List of ExtensionPlayerData at the highest level.
|
||||
* <p>
|
||||
* There are multiple data extraction methods to make extracting the value query easier.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ExtensionServerDataQuery implements Query<List<ExtensionServerData>> {
|
||||
|
||||
private final UUID serverUUID;
|
||||
|
||||
public ExtensionServerDataQuery(UUID serverUUID) {
|
||||
this.serverUUID = serverUUID;
|
||||
}
|
||||
|
||||
static Map<Integer, ExtensionServerData.Factory> flatMapToServerData(Map<Integer, Map<String, ExtensionTabData.Factory>> tabDataByPluginID) {
|
||||
Map<Integer, ExtensionServerData.Factory> dataByPluginID = new HashMap<>();
|
||||
for (Map.Entry<Integer, Map<String, ExtensionTabData.Factory>> entry : tabDataByPluginID.entrySet()) {
|
||||
Integer pluginID = entry.getKey();
|
||||
ExtensionServerData.Factory data = dataByPluginID.getOrDefault(pluginID, new ExtensionServerData.Factory(pluginID));
|
||||
for (ExtensionTabData.Factory tabData : entry.getValue().values()) {
|
||||
data.addTab(tabData.build());
|
||||
}
|
||||
dataByPluginID.put(pluginID, data);
|
||||
}
|
||||
return dataByPluginID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ExtensionServerData> executeQuery(SQLDB db) {
|
||||
List<ExtensionInformation> extensionsOfServer = db.query(ExtensionInformationQueries.extensionsOfServer(serverUUID));
|
||||
Map<Integer, ExtensionServerData.Factory> extensionDataByPluginID = db.query(fetchIncompleteServerDataByPluginID());
|
||||
|
||||
combine(extensionDataByPluginID, db.query(new ExtensionAggregateBooleansQuery(serverUUID)));
|
||||
|
||||
return combineWithExtensionInfo(extensionsOfServer, extensionDataByPluginID);
|
||||
}
|
||||
|
||||
private void combine(
|
||||
Map<Integer, ExtensionServerData.Factory> extensionDataByPluginID,
|
||||
Map<Integer, ExtensionServerData.Factory> aggregates
|
||||
) {
|
||||
for (Map.Entry<Integer, ExtensionServerData.Factory> entry : aggregates.entrySet()) {
|
||||
Integer pluginID = entry.getKey();
|
||||
ExtensionServerData.Factory data = entry.getValue();
|
||||
|
||||
ExtensionServerData.Factory found = extensionDataByPluginID.get(pluginID);
|
||||
if (found == null) {
|
||||
extensionDataByPluginID.put(pluginID, data);
|
||||
} else {
|
||||
found.combine(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<ExtensionServerData> combineWithExtensionInfo(
|
||||
List<ExtensionInformation> extensionsOfServer,
|
||||
Map<Integer, ExtensionServerData.Factory> extensionDataByPluginID
|
||||
) {
|
||||
List<ExtensionServerData> extensionServerData = new ArrayList<>();
|
||||
|
||||
for (ExtensionInformation extensionInformation : extensionsOfServer) {
|
||||
ExtensionServerData.Factory data = extensionDataByPluginID.get(extensionInformation.getId());
|
||||
if (data == null) {
|
||||
continue;
|
||||
}
|
||||
extensionServerData.add(data.setInformation(extensionInformation).build());
|
||||
}
|
||||
return extensionServerData;
|
||||
}
|
||||
|
||||
private Query<Map<Integer, ExtensionServerData.Factory>> fetchIncompleteServerDataByPluginID() {
|
||||
String sql = SELECT +
|
||||
"v1." + ExtensionServerValueTable.BOOLEAN_VALUE + " as boolean_value," +
|
||||
"v1." + ExtensionServerValueTable.DOUBLE_VALUE + " as double_value," +
|
||||
"v1." + ExtensionServerValueTable.PERCENTAGE_VALUE + " as percentage_value," +
|
||||
"v1." + ExtensionServerValueTable.LONG_VALUE + " as long_value," +
|
||||
"v1." + ExtensionServerValueTable.STRING_VALUE + " as string_value," +
|
||||
"p1." + ExtensionProviderTable.PLUGIN_ID + " as plugin_id," +
|
||||
"p1." + ExtensionProviderTable.PROVIDER_NAME + " as provider_name," +
|
||||
"p1." + ExtensionProviderTable.TEXT + " as text," +
|
||||
"p1." + ExtensionProviderTable.DESCRIPTION + " as description," +
|
||||
"p1." + ExtensionProviderTable.PRIORITY + " as provider_priority," +
|
||||
"p1." + ExtensionProviderTable.FORMAT_TYPE + " as format_type," +
|
||||
"p1." + ExtensionProviderTable.IS_PLAYER_NAME + " as is_player_name," +
|
||||
"t1." + ExtensionTabTable.TAB_NAME + " as tab_name," +
|
||||
"t1." + ExtensionTabTable.TAB_PRIORITY + " as tab_priority," +
|
||||
"t1." + ExtensionTabTable.ELEMENT_ORDER + " as element_order," +
|
||||
"i1." + ExtensionIconTable.ICON_NAME + " as provider_icon_name," +
|
||||
"i1." + ExtensionIconTable.FAMILY + " as provider_icon_family," +
|
||||
"i1." + ExtensionIconTable.COLOR + " as provider_icon_color," +
|
||||
"i2." + ExtensionIconTable.ICON_NAME + " as tab_icon_name," +
|
||||
"i2." + ExtensionIconTable.FAMILY + " as tab_icon_family," +
|
||||
"i2." + ExtensionIconTable.COLOR + " as tab_icon_color" +
|
||||
FROM + ExtensionServerValueTable.TABLE_NAME + " v1" +
|
||||
INNER_JOIN + ExtensionProviderTable.TABLE_NAME + " p1 on p1." + ExtensionProviderTable.ID + "=v1." + ExtensionServerValueTable.PROVIDER_ID +
|
||||
INNER_JOIN + ExtensionPluginTable.TABLE_NAME + " e1 on p1." + ExtensionProviderTable.PLUGIN_ID + "=e1." + ExtensionPluginTable.ID +
|
||||
LEFT_JOIN + ExtensionTabTable.TABLE_NAME + " t1 on t1." + ExtensionTabTable.ID + "=p1." + ExtensionProviderTable.TAB_ID +
|
||||
LEFT_JOIN + ExtensionIconTable.TABLE_NAME + " i1 on i1." + ExtensionIconTable.ID + "=p1." + ExtensionProviderTable.ICON_ID +
|
||||
LEFT_JOIN + ExtensionIconTable.TABLE_NAME + " i2 on i2." + ExtensionIconTable.ID + "=p1." + ExtensionTabTable.ICON_ID +
|
||||
WHERE + ExtensionPluginTable.SERVER_UUID + "=?" +
|
||||
AND + "p1." + ExtensionProviderTable.HIDDEN + "=?";
|
||||
|
||||
return new QueryStatement<Map<Integer, ExtensionServerData.Factory>>(sql, 1000) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
statement.setBoolean(2, false); // Don't select hidden values
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, ExtensionServerData.Factory> processResults(ResultSet set) throws SQLException {
|
||||
Map<Integer, Map<String, ExtensionTabData.Factory>> tabDataByPluginID = extractTabDataByPluginID(set);
|
||||
return flatMapToServerData(tabDataByPluginID);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Map<Integer, Map<String, ExtensionTabData.Factory>> extractTabDataByPluginID(ResultSet set) throws SQLException {
|
||||
Map<Integer, Map<String, ExtensionTabData.Factory>> tabDataByPluginID = new HashMap<>();
|
||||
|
||||
while (set.next()) {
|
||||
int pluginID = set.getInt("plugin_id");
|
||||
Map<String, ExtensionTabData.Factory> tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>());
|
||||
|
||||
String tabName = Optional.ofNullable(set.getString("tab_name")).orElse("");
|
||||
ExtensionTabData.Factory inMap = tabData.get(tabName);
|
||||
ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData);
|
||||
|
||||
ExtensionDescriptive extensionDescriptive = extractDescriptive(set);
|
||||
extractAndPutDataTo(extensionTab, extensionDescriptive, set);
|
||||
|
||||
tabData.put(tabName, extensionTab);
|
||||
tabDataByPluginID.put(pluginID, tabData);
|
||||
}
|
||||
return tabDataByPluginID;
|
||||
}
|
||||
|
||||
private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException {
|
||||
boolean booleanValue = set.getBoolean(ExtensionServerValueTable.BOOLEAN_VALUE);
|
||||
if (!set.wasNull()) {
|
||||
extensionTab.putBooleanData(new ExtensionBooleanData(descriptive, booleanValue));
|
||||
return;
|
||||
}
|
||||
|
||||
double doubleValue = set.getDouble(ExtensionServerValueTable.DOUBLE_VALUE);
|
||||
if (!set.wasNull()) {
|
||||
extensionTab.putDoubleData(new ExtensionDoubleData(descriptive, doubleValue));
|
||||
return;
|
||||
}
|
||||
|
||||
double percentageValue = set.getDouble(ExtensionServerValueTable.PERCENTAGE_VALUE);
|
||||
if (!set.wasNull()) {
|
||||
extensionTab.putPercentageData(new ExtensionDoubleData(descriptive, percentageValue));
|
||||
return;
|
||||
}
|
||||
|
||||
long numberValue = set.getLong(ExtensionServerValueTable.LONG_VALUE);
|
||||
if (!set.wasNull()) {
|
||||
FormatType formatType = FormatType.getByName(set.getString(ExtensionProviderTable.FORMAT_TYPE)).orElse(FormatType.NONE);
|
||||
extensionTab.putNumberData(new ExtensionNumberData(descriptive, formatType, numberValue));
|
||||
return;
|
||||
}
|
||||
|
||||
String stringValue = set.getString(ExtensionServerValueTable.STRING_VALUE);
|
||||
if (stringValue != null) {
|
||||
boolean isPlayerName = set.getBoolean("is_player_name");
|
||||
extensionTab.putStringData(new ExtensionStringData(descriptive, isPlayerName, stringValue));
|
||||
}
|
||||
}
|
||||
|
||||
private ExtensionDescriptive extractDescriptive(ResultSet set) throws SQLException {
|
||||
String name = set.getString("provider_name");
|
||||
String text = set.getString(ExtensionProviderTable.TEXT);
|
||||
String description = set.getString(ExtensionProviderTable.DESCRIPTION);
|
||||
int priority = set.getInt("provider_priority");
|
||||
|
||||
String iconName = set.getString("provider_icon_name");
|
||||
Family family = Family.getByName(set.getString("provider_icon_family")).orElse(Family.SOLID);
|
||||
Color color = Color.getByName(set.getString("provider_icon_color")).orElse(Color.NONE);
|
||||
Icon icon = new Icon(family, iconName, color);
|
||||
|
||||
return new ExtensionDescriptive(name, text, description, icon, priority);
|
||||
}
|
||||
|
||||
private ExtensionTabData.Factory extractTab(String tabName, ResultSet set, Map<String, ExtensionTabData.Factory> tabData) throws SQLException {
|
||||
Optional<Integer> tabPriority = Optional.of(set.getInt("tab_priority"));
|
||||
if (set.wasNull()) {
|
||||
tabPriority = Optional.empty();
|
||||
}
|
||||
Optional<ElementOrder[]> elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize);
|
||||
|
||||
Icon tabIcon = extractTabIcon(set);
|
||||
|
||||
return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation(
|
||||
tabName,
|
||||
tabIcon,
|
||||
elementOrder.orElse(ElementOrder.values()),
|
||||
tabPriority.orElse(100)
|
||||
)));
|
||||
}
|
||||
|
||||
private Icon extractTabIcon(ResultSet set) throws SQLException {
|
||||
Optional<String> iconName = Optional.ofNullable(set.getString("tab_icon_name"));
|
||||
if (iconName.isPresent()) {
|
||||
Family iconFamily = Family.getByName(set.getString("tab_icon_family")).orElse(Family.SOLID);
|
||||
Color iconColor = Color.getByName(set.getString("tab_icon_color")).orElse(Color.NONE);
|
||||
return new Icon(iconFamily, iconName.get(), iconColor);
|
||||
} else {
|
||||
return TabInformation.defaultIcon();
|
||||
}
|
||||
}
|
||||
}
|
@ -51,7 +51,9 @@ import com.djrapitops.plan.extension.implementation.results.ExtensionBooleanData
|
||||
import com.djrapitops.plan.extension.implementation.results.ExtensionStringData;
|
||||
import com.djrapitops.plan.extension.implementation.results.ExtensionTabData;
|
||||
import com.djrapitops.plan.extension.implementation.results.player.ExtensionPlayerData;
|
||||
import com.djrapitops.plan.extension.implementation.results.server.ExtensionServerData;
|
||||
import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionPlayerDataQuery;
|
||||
import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerDataQuery;
|
||||
import com.djrapitops.plan.extension.implementation.storage.transactions.results.RemoveUnsatisfiedConditionalResultsTransaction;
|
||||
import com.djrapitops.plan.system.PlanSystem;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
@ -156,7 +158,7 @@ public abstract class CommonDBTest {
|
||||
db.executeTransaction(new StoreServerInformationTransaction(new Server(-1, serverUUID, "ServerName", "", 20)));
|
||||
assertEquals(serverUUID, db.getServerUUIDSupplier().get());
|
||||
|
||||
system.getExtensionService().unregister(new TestExtension());
|
||||
system.getExtensionService().unregister(new PlayerExtension());
|
||||
system.getExtensionService().unregister(new ServerExtension());
|
||||
system.getExtensionService().unregister(new ConditionalExtension());
|
||||
}
|
||||
@ -1037,7 +1039,7 @@ public abstract class CommonDBTest {
|
||||
public void extensionPlayerValuesAreStored() {
|
||||
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService();
|
||||
|
||||
extensionService.register(new TestExtension());
|
||||
extensionService.register(new PlayerExtension());
|
||||
extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME);
|
||||
|
||||
Map<UUID, List<ExtensionPlayerData>> playerDataByServerUUID = db.query(new ExtensionPlayerDataQuery(playerUUID));
|
||||
@ -1057,6 +1059,46 @@ public abstract class CommonDBTest {
|
||||
OptionalAssert.equals("Something", tabData.getString("stringVal").map(ExtensionStringData::getFormattedValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionServerValuesAreStored() {
|
||||
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService();
|
||||
|
||||
extensionService.register(new ServerExtension());
|
||||
extensionService.updateServerValues();
|
||||
|
||||
List<ExtensionServerData> ofServer = db.query(new ExtensionServerDataQuery(serverUUID));
|
||||
assertFalse(ofServer.isEmpty());
|
||||
|
||||
ExtensionServerData extensionServerData = ofServer.get(0);
|
||||
List<ExtensionTabData> tabs = extensionServerData.getTabs();
|
||||
assertEquals(1, tabs.size()); // No tab defined, should contain 1 tab
|
||||
ExtensionTabData tabData = tabs.get(0);
|
||||
|
||||
OptionalAssert.equals("5", tabData.getNumber("value").map(data -> data.getFormattedValue(Object::toString)));
|
||||
OptionalAssert.equals("No", tabData.getBoolean("boolVal").map(ExtensionBooleanData::getFormattedValue));
|
||||
OptionalAssert.equals("0.5", tabData.getDouble("doubleVal").map(data -> data.getFormattedValue(Object::toString)));
|
||||
OptionalAssert.equals("0.5", tabData.getPercentage("percentageVal").map(data -> data.getFormattedValue(Object::toString)));
|
||||
OptionalAssert.equals("Something", tabData.getString("stringVal").map(ExtensionStringData::getFormattedValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionServerAggregateQueriesWork() {
|
||||
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService();
|
||||
|
||||
extensionService.register(new PlayerExtension());
|
||||
extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME);
|
||||
|
||||
List<ExtensionServerData> ofServer = db.query(new ExtensionServerDataQuery(serverUUID));
|
||||
assertFalse(ofServer.isEmpty());
|
||||
|
||||
ExtensionServerData extensionServerData = ofServer.get(0);
|
||||
List<ExtensionTabData> tabs = extensionServerData.getTabs();
|
||||
assertEquals(1, tabs.size()); // No tab defined, should contain 1 tab
|
||||
ExtensionTabData tabData = tabs.get(0);
|
||||
|
||||
OptionalAssert.equals("0.0", tabData.getPercentage("boolVal_aggregate").map(data -> data.getFormattedValue(Objects::toString)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsatisfiedConditionalResultsAreCleaned() throws ExecutionException, InterruptedException {
|
||||
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService();
|
||||
@ -1109,31 +1151,31 @@ public abstract class CommonDBTest {
|
||||
}
|
||||
}
|
||||
|
||||
@PluginInfo(name = "TestExtension")
|
||||
public class TestExtension implements DataExtension {
|
||||
@NumberProvider(text = "a number")
|
||||
public long value(UUID playerUUD) {
|
||||
return 5L;
|
||||
@PluginInfo(name = "ConditionalExtension")
|
||||
public static class ConditionalExtension implements DataExtension {
|
||||
|
||||
static boolean condition = true;
|
||||
|
||||
@BooleanProvider(text = "a boolean", conditionName = "condition")
|
||||
public boolean isCondition(UUID playerUUID) {
|
||||
return condition;
|
||||
}
|
||||
|
||||
@BooleanProvider(text = "a boolean")
|
||||
public boolean boolVal(UUID playerUUID) {
|
||||
return false;
|
||||
@StringProvider(text = "Conditional Value")
|
||||
@Conditional("condition")
|
||||
public String conditionalValue(UUID playerUUID) {
|
||||
return "Conditional";
|
||||
}
|
||||
|
||||
@DoubleProvider(text = "a double")
|
||||
public double doubleVal(UUID playerUUID) {
|
||||
return 0.5;
|
||||
@StringProvider(text = "Reversed Conditional Value")
|
||||
@Conditional(value = "condition", negated = true)
|
||||
public String reversedConditionalValue(UUID playerUUID) {
|
||||
return "Reversed";
|
||||
}
|
||||
|
||||
@PercentageProvider(text = "a percentage")
|
||||
public double percentageVal(UUID playerUUID) {
|
||||
return 0.5;
|
||||
}
|
||||
|
||||
@StringProvider(text = "a string")
|
||||
public String stringVal(UUID playerUUID) {
|
||||
return "Something";
|
||||
@StringProvider(text = "Unconditional")
|
||||
public String unconditional(UUID playerUUID) {
|
||||
return "unconditional";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1165,31 +1207,31 @@ public abstract class CommonDBTest {
|
||||
}
|
||||
}
|
||||
|
||||
@PluginInfo(name = "Conditional TestExtension")
|
||||
public static class ConditionalExtension implements DataExtension {
|
||||
|
||||
static boolean condition = true;
|
||||
|
||||
@BooleanProvider(text = "a boolean", conditionName = "condition")
|
||||
public boolean isCondition(UUID playerUUID) {
|
||||
return condition;
|
||||
@PluginInfo(name = "PlayerExtension")
|
||||
public class PlayerExtension implements DataExtension {
|
||||
@NumberProvider(text = "a number")
|
||||
public long value(UUID playerUUD) {
|
||||
return 5L;
|
||||
}
|
||||
|
||||
@StringProvider(text = "Conditional Value")
|
||||
@Conditional("condition")
|
||||
public String conditionalValue(UUID playerUUID) {
|
||||
return "Conditional";
|
||||
@BooleanProvider(text = "a boolean")
|
||||
public boolean boolVal(UUID playerUUID) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@StringProvider(text = "Reversed Conditional Value")
|
||||
@Conditional(value = "condition", negated = true)
|
||||
public String reversedConditionalValue(UUID playerUUID) {
|
||||
return "Reversed";
|
||||
@DoubleProvider(text = "a double")
|
||||
public double doubleVal(UUID playerUUID) {
|
||||
return 0.5;
|
||||
}
|
||||
|
||||
@StringProvider(text = "Unconditional")
|
||||
public String unconditional(UUID playerUUID) {
|
||||
return "unconditional";
|
||||
@PercentageProvider(text = "a percentage")
|
||||
public double percentageVal(UUID playerUUID) {
|
||||
return 0.5;
|
||||
}
|
||||
|
||||
@StringProvider(text = "a string")
|
||||
public String stringVal(UUID playerUUID) {
|
||||
return "Something";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user