Skip to content

Commit

Permalink
Implement data gathering pipe for GraphHistoryPointsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraLS3 committed Jun 21, 2024
1 parent 831c466 commit 46f61e7
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.djrapitops.plan.extension.annotation.StringProvider;
import com.djrapitops.plan.extension.annotation.Tab;
import com.djrapitops.plan.extension.extractor.ExtensionMethod;
import com.djrapitops.plan.extension.graph.DataPoint;
import com.djrapitops.plan.extension.graph.HistoryStrategy;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Family;
import com.djrapitops.plan.extension.icon.Icon;
Expand Down Expand Up @@ -288,6 +290,11 @@ default DataValue<Long> buildNumber(Double value) {
*/
DataValue<String[]> buildGroup(Supplier<String[]> groups);

/**
* Lambda version for conditional return or throwing {@link com.djrapitops.plan.extension.NotReadyException}.
*/
DataValue<DataPoint[]> buildGraphHistoryPoints(Supplier<DataPoint[]> historyData, String methodName, HistoryStrategy appendStrategy);

/**
* Lambda version for conditional return or throwing {@link com.djrapitops.plan.extension.NotReadyException}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.djrapitops.plan.extension.FormatType;
import com.djrapitops.plan.extension.annotation.Conditional;
import com.djrapitops.plan.extension.graph.HistoryStrategy;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Icon;
import com.djrapitops.plan.extension.implementation.results.ExtensionDescription;
Expand All @@ -44,6 +45,7 @@ public class ProviderInformation extends ExtensionDescription {
private final Color tableColor; // can be null, TableProvider
private final boolean percentage; // affects where doubles are stored
private final boolean component; // affects where strings are stored
private final HistoryStrategy appendStrategy;

private ProviderInformation(ProviderInformation.Builder builder) {
super(
Expand All @@ -64,6 +66,7 @@ private ProviderInformation(ProviderInformation.Builder builder) {
tableColor = builder.tableColor;
percentage = builder.percentage;
component = builder.component;
appendStrategy = builder.appendStrategy;
}

public static ProviderInformation.Builder builder(String pluginName) {
Expand Down Expand Up @@ -110,6 +113,10 @@ public Optional<String> getCondition() {
}
}

public Optional<HistoryStrategy> getAppendStrategy() {
return Optional.ofNullable(appendStrategy);
}

private String getTruncatedConditionName() {
return StringUtils.truncate(condition.value(), 50);
}
Expand Down Expand Up @@ -159,6 +166,7 @@ public static class Builder {
private Color tableColor; // can be null, TableProvider
private boolean percentage; // affects where doubles are stored
private boolean component; // affects where strings are stored
private HistoryStrategy appendStrategy;

public Builder(String pluginName) {
this.pluginName = pluginName;
Expand Down Expand Up @@ -242,5 +250,10 @@ public Builder setAsComponent() {
public ProviderInformation build() {
return new ProviderInformation(this);
}

public Builder setAppendStrategy(HistoryStrategy appendStrategy) {
this.appendStrategy = appendStrategy;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.djrapitops.plan.extension.builder.DataValue;
import com.djrapitops.plan.extension.builder.ValueBuilder;
import com.djrapitops.plan.extension.extractor.ExtensionMethod;
import com.djrapitops.plan.extension.graph.DataPoint;
import com.djrapitops.plan.extension.graph.HistoryStrategy;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Icon;
import com.djrapitops.plan.extension.implementation.ProviderInformation;
Expand Down Expand Up @@ -174,6 +176,15 @@ private ProviderInformation getTableProviderInformation(Color tableColor) {
.build();
}

private ProviderInformation getGraphHistoryProviderInformation(String methodName, HistoryStrategy appendStrategy) {
return ProviderInformation.builder(pluginName)
.setName(methodName)
.setPriority(0)
.setTab(tabName)
.setAppendStrategy(appendStrategy)
.build();
}

@Override
public DataValue<Boolean> buildBoolean(boolean value) {
return new BooleanDataValue(value, getProviderInformation());
Expand Down Expand Up @@ -254,6 +265,11 @@ public DataValue<String[]> buildGroup(Supplier<String[]> groups) {
return new GroupsDataValue(groups, getProviderInformation());
}

@Override
public DataValue<DataPoint[]> buildGraphHistoryPoints(Supplier<DataPoint[]> historyData, String methodName, HistoryStrategy appendStrategy) {
return new GraphHistoryPoints(historyData, getGraphHistoryProviderInformation(methodName, appendStrategy));
}

@Override
public DataValue<Table> buildTable(Table table, Color tableColor) {
return new TableDataValue(table, getTableProviderInformation(tableColor));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.builder;

import com.djrapitops.plan.extension.graph.DataPoint;
import com.djrapitops.plan.extension.implementation.ProviderInformation;

import java.util.function.Supplier;

/**
* @author AuroraLS3
*/
public class GraphHistoryPoints extends BuiltDataValue<DataPoint[]> {
public GraphHistoryPoints(DataPoint[] value, ProviderInformation information) {
super(value, information);
}

public GraphHistoryPoints(Supplier<DataPoint[]> supplier, ProviderInformation information) {
super(supplier, information);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.djrapitops.plan.extension.builder.ExtensionDataBuilder;
import com.djrapitops.plan.extension.extractor.ExtensionMethod;
import com.djrapitops.plan.extension.extractor.ExtensionMethods;
import com.djrapitops.plan.extension.graph.DataPoint;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Icon;
import com.djrapitops.plan.extension.implementation.ExtensionWrapper;
Expand Down Expand Up @@ -163,6 +164,22 @@ private void addValuesToBuilder2(ExtensionDataBuilder dataBuilder, ExtensionMeth
if (brokenMethods.contains(provider)) continue;
addDataFromAnotherBuilder(dataBuilder, parameters, provider);
}
for (ExtensionMethod provider : methods.getGraphHistoryPointsProviders()) {
if (brokenMethods.contains(provider)) continue;
dataBuilder.addValue(DataPoint[].class, tryToBuildDataPoints(dataBuilder, parameters, provider));
}
}

private DataValue<DataPoint[]> tryToBuildDataPoints(ExtensionDataBuilder dataBuilder, Parameters parameters, ExtensionMethod provider) {
GraphHistoryPointsProvider annotation = provider.getExistingAnnotation(GraphHistoryPointsProvider.class);
try {
return dataBuilder.valueBuilder(provider.getMethodName())
.methodName(provider)
.buildGraphHistoryPoints(() -> callMethod(provider, parameters, DataPoint[].class), annotation.methodName(), annotation.strategy());
} catch (IllegalArgumentException e) {
logFailure(e, getPluginName(), provider.getMethodName());
return null;
}
}

private DataValue<Table> tryToBuildTable(ExtensionDataBuilder dataBuilder, Parameters parameters, ExtensionMethod provider) {
Expand Down Expand Up @@ -382,6 +399,8 @@ private void gatherPlayer(Parameters parameters, ExtDataBuilder dataBuilder) {
.ifPresent(data -> storePlayerGroups(parameters, conditions, data));
pair.getValue(Table.class).flatMap(data -> data.getMetadata(TableDataValue.class))
.ifPresent(data -> storePlayerTable(parameters, conditions, data));
pair.getValue(DataPoint[].class).flatMap(data -> data.getMetadata(GraphHistoryPoints.class))
.ifPresent(data -> {/*TODO #2544 - Store data after the metadata tables are already done.*/});
} catch (DataExtensionMethodCallException methodError) {
logFailure(methodError);
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError unexpectedError) {
Expand All @@ -406,6 +425,8 @@ private void gather(Parameters parameters, ExtDataBuilder dataBuilder) {
.ifPresent(data -> storeComponent(parameters, conditions, data));
pair.getValue(Table.class).flatMap(data -> data.getMetadata(TableDataValue.class))
.ifPresent(data -> storeTable(parameters, conditions, data));
pair.getValue(DataPoint[].class).flatMap(data -> data.getMetadata(GraphHistoryPoints.class))
.ifPresent(data -> {/*TODO #2544 - Store data after the metadata tables are already done.*/});
} catch (DataExtensionMethodCallException methodError) {
logFailure(methodError);
} catch (RejectedExecutionException ignore) {
Expand Down

0 comments on commit 46f61e7

Please sign in to comment.