use of org.locationtech.geowave.core.store.statistics.field.FieldStatisticType in project geowave by locationtech.
the class ListStatsCommand method performStatsCommand.
@Override
protected boolean performStatsCommand(final DataStorePluginOptions storeOptions, final StatsCommandLineOptions statsOptions, final Console console) throws IOException {
final DataStatisticsStore statsStore = storeOptions.createDataStatisticsStore();
final IndexStore indexStore = storeOptions.createIndexStore();
final String[] authorizations = getAuthorizations(statsOptions.getAuthorizations());
DataTypeAdapter<?> adapter = null;
if (statsOptions.getTypeName() != null) {
adapter = storeOptions.createDataStore().getType(statsOptions.getTypeName());
if (adapter == null) {
throw new ParameterException("A type called " + statsOptions.getTypeName() + " was not found.");
}
}
StatisticType<StatisticValue<Object>> statisticType = null;
if (statsOptions.getStatType() != null) {
statisticType = StatisticsRegistry.instance().getStatisticType(statsOptions.getStatType());
if (statisticType == null) {
throw new ParameterException("Unrecognized statistic type: " + statsOptions.getStatType());
}
}
List<String> headers = null;
List<Statistic<?>> statsToList = Lists.newLinkedList();
ValueTransformer transformer = null;
Predicate<StatisticValue<?>> filter;
if (statsOptions.getIndexName() != null) {
if (statisticType != null && !(statisticType instanceof IndexStatisticType)) {
throw new ParameterException("Only index statistic types can be specified when listing statistics for a specific index.");
}
Index index = indexStore.getIndex(statsOptions.getIndexName());
if (index == null) {
throw new ParameterException("An index called " + statsOptions.getIndexName() + " was not found.");
}
headers = Lists.newArrayList("Statistic", "Tag", "Bin", "Value");
transformer = new ValueToRow();
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getIndexStatistics(index, statisticType, statsOptions.getTag())) {
if (adapter != null) {
stats.forEachRemaining(stat -> {
if (stat.getBinningStrategy() instanceof DataTypeBinningStrategy || (stat.getBinningStrategy() instanceof CompositeBinningStrategy && ((CompositeBinningStrategy) stat.getBinningStrategy()).usesStrategy(DataTypeBinningStrategy.class))) {
statsToList.add(stat);
}
});
filter = new IndexAdapterFilter(adapter.getTypeName());
} else {
stats.forEachRemaining(statsToList::add);
filter = null;
}
}
} else if (statsOptions.getTypeName() != null) {
filter = null;
if (statsOptions.getFieldName() != null) {
if (statisticType != null && !(statisticType instanceof FieldStatisticType)) {
throw new ParameterException("Only field statistic types can be specified when listing statistics for a specific field.");
}
headers = Lists.newArrayList("Statistic", "Tag", "Bin", "Value");
transformer = new ValueToRow();
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getFieldStatistics(adapter, statisticType, statsOptions.getFieldName(), statsOptions.getTag())) {
stats.forEachRemaining(statsToList::add);
}
} else {
if (statisticType != null && statisticType instanceof IndexStatisticType) {
throw new ParameterException("Only data type and field statistic types can be specified when listing statistics for a specific data type.");
}
headers = Lists.newArrayList("Statistic", "Tag", "Field", "Bin", "Value");
transformer = new ValueToFieldRow();
if (statisticType == null || statisticType instanceof DataTypeStatisticType) {
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getDataTypeStatistics(adapter, statisticType, statsOptions.getTag())) {
stats.forEachRemaining(statsToList::add);
}
}
if (statisticType == null || statisticType instanceof FieldStatisticType) {
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getFieldStatistics(adapter, statisticType, null, statsOptions.getTag())) {
stats.forEachRemaining(statsToList::add);
}
}
}
} else if (statsOptions.getFieldName() != null) {
throw new ParameterException("A type name must be supplied with a field name.");
} else {
filter = null;
headers = Lists.newArrayList("Index/Adapter", "Statistic", "Tag", "Field", "Bin", "Value");
transformer = new ValueToAllRow();
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getAllStatistics(statisticType)) {
stats.forEachRemaining(stat -> {
if (statsOptions.getTag() == null || stat.getTag().equals(statsOptions.getTag())) {
statsToList.add(stat);
}
});
}
}
Collections.sort(statsToList, new StatComparator());
try (StatisticsValueIterator values = new StatisticsValueIterator(statsStore, statsToList.iterator(), null, authorizations)) {
Iterator<List<Object>> rows = Iterators.transform(filter == null ? values : Iterators.filter(values, v -> filter.test(v)), transformer::transform);
if (limit != null) {
rows = Iterators.limit(rows, limit);
}
if (rows.hasNext()) {
if (csv) {
StringBuilder sb = new StringBuilder();
sb.append(Arrays.toString(headers.toArray()));
rows.forEachRemaining(row -> sb.append(Arrays.toString(row.toArray())));
retValue = sb.toString();
console.println(retValue);
} else {
console.println("Matching statistics:");
ConsoleTablePrinter printer = new ConsoleTablePrinter(0, limit != null ? limit : 30, console);
printer.print(headers, rows);
}
} else {
console.println("No matching statistics were found.");
}
}
return true;
}
use of org.locationtech.geowave.core.store.statistics.field.FieldStatisticType in project geowave by locationtech.
the class StatsCommandLineOptions method resolveMatchingStatistics.
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Statistic<? extends StatisticValue<?>>> resolveMatchingStatistics(final DataStore dataStore, final DataStatisticsStore statsStore, final IndexStore indexStore) {
final List<Statistic<? extends StatisticValue<?>>> matching = Lists.newArrayList();
if ((indexName != null) && ((typeName != null) || (fieldName != null))) {
throw new ParameterException("Unable to process index statistics for a single type. Specify either an index name or a type name.");
}
StatisticType statisticType = null;
if (statType != null) {
statisticType = StatisticsRegistry.instance().getStatisticType(statType);
if (statisticType == null) {
throw new ParameterException("Unrecognized statistic type: " + statType);
}
}
if (statisticType != null) {
if (statisticType instanceof IndexStatisticType) {
if (indexName == null) {
throw new ParameterException("An index name must be supplied when specifying an index statistic type.");
}
final Index index = indexStore.getIndex(indexName);
if (index == null) {
throw new ParameterException("Unable to find an index named: " + indexName);
}
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getIndexStatistics(index, statisticType, tag)) {
stats.forEachRemaining(stat -> matching.add(stat));
}
} else if (statisticType instanceof DataTypeStatisticType) {
if (typeName == null) {
throw new ParameterException("A type name must be supplied when specifying a data type statistic type.");
}
final DataTypeAdapter<?> adapter = dataStore.getType(typeName);
if (adapter == null) {
throw new ParameterException("Unable to find an type named: " + typeName);
}
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getDataTypeStatistics(adapter, statisticType, tag)) {
stats.forEachRemaining(stat -> matching.add(stat));
}
} else if (statisticType instanceof FieldStatisticType) {
if (typeName == null) {
throw new ParameterException("A type name must be supplied when specifying a field statistic type.");
}
final DataTypeAdapter<?> adapter = dataStore.getType(typeName);
if (adapter == null) {
throw new ParameterException("Unable to find an type named: " + typeName);
}
if (fieldName == null) {
throw new ParameterException("A field name must be supplied when specifying a field statistic type.");
}
boolean fieldFound = false;
final FieldDescriptor[] fields = adapter.getFieldDescriptors();
for (int i = 0; i < fields.length; i++) {
if (fields[i].fieldName().equals(fieldName)) {
fieldFound = true;
break;
}
}
if (!fieldFound) {
throw new ParameterException("Unable to find a field named '" + fieldName + "' on type '" + typeName + "'.");
}
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getFieldStatistics(adapter, statisticType, fieldName, tag)) {
stats.forEachRemaining(stat -> matching.add(stat));
}
}
} else {
try (CloseableIterator<? extends Statistic<? extends StatisticValue<?>>> stats = statsStore.getAllStatistics(null)) {
stats.forEachRemaining(stat -> {
// This could all be optimized to one giant check, but it's split for readability
if ((tag != null) && !tag.equals(stat.getTag())) {
return;
}
if ((indexName != null) && (!(stat instanceof IndexStatistic) || !indexName.equals(((IndexStatistic) stat).getIndexName()))) {
return;
}
if (typeName != null) {
if (stat instanceof IndexStatistic) {
return;
}
if ((stat instanceof DataTypeStatistic) && !typeName.equals(((DataTypeStatistic) stat).getTypeName())) {
return;
}
if ((stat instanceof FieldStatistic) && !typeName.equals(((FieldStatistic) stat).getTypeName())) {
return;
}
}
if ((fieldName != null) && (!(stat instanceof FieldStatistic) || !fieldName.equals(((FieldStatistic) stat).getFieldName()))) {
return;
}
matching.add(stat);
});
}
}
return matching;
}
Aggregations