use of org.locationtech.geowave.core.store.statistics.StatisticsValueIterator 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;
}
Aggregations