Search in sources :

Example 1 with StatisticsValueIterator

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;
}
Also used : StatisticValue(org.locationtech.geowave.core.store.api.StatisticValue) Index(org.locationtech.geowave.core.store.api.Index) IndexStatisticType(org.locationtech.geowave.core.store.statistics.index.IndexStatisticType) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) Statistic(org.locationtech.geowave.core.store.api.Statistic) FieldStatistic(org.locationtech.geowave.core.store.api.FieldStatistic) DataTypeStatistic(org.locationtech.geowave.core.store.api.DataTypeStatistic) IndexStatistic(org.locationtech.geowave.core.store.api.IndexStatistic) ParameterException(com.beust.jcommander.ParameterException) ArrayList(java.util.ArrayList) List(java.util.List) StatisticsValueIterator(org.locationtech.geowave.core.store.statistics.StatisticsValueIterator) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) DataTypeBinningStrategy(org.locationtech.geowave.core.store.statistics.binning.DataTypeBinningStrategy) CompositeBinningStrategy(org.locationtech.geowave.core.store.statistics.binning.CompositeBinningStrategy) FieldStatisticType(org.locationtech.geowave.core.store.statistics.field.FieldStatisticType) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) DataTypeStatisticType(org.locationtech.geowave.core.store.statistics.adapter.DataTypeStatisticType) ConsoleTablePrinter(org.locationtech.geowave.core.cli.utils.ConsoleTablePrinter)

Aggregations

ParameterException (com.beust.jcommander.ParameterException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConsoleTablePrinter (org.locationtech.geowave.core.cli.utils.ConsoleTablePrinter)1 CloseableIterator (org.locationtech.geowave.core.store.CloseableIterator)1 DataTypeStatistic (org.locationtech.geowave.core.store.api.DataTypeStatistic)1 FieldStatistic (org.locationtech.geowave.core.store.api.FieldStatistic)1 Index (org.locationtech.geowave.core.store.api.Index)1 IndexStatistic (org.locationtech.geowave.core.store.api.IndexStatistic)1 Statistic (org.locationtech.geowave.core.store.api.Statistic)1 StatisticValue (org.locationtech.geowave.core.store.api.StatisticValue)1 IndexStore (org.locationtech.geowave.core.store.index.IndexStore)1 DataStatisticsStore (org.locationtech.geowave.core.store.statistics.DataStatisticsStore)1 StatisticsValueIterator (org.locationtech.geowave.core.store.statistics.StatisticsValueIterator)1 DataTypeStatisticType (org.locationtech.geowave.core.store.statistics.adapter.DataTypeStatisticType)1 CompositeBinningStrategy (org.locationtech.geowave.core.store.statistics.binning.CompositeBinningStrategy)1 DataTypeBinningStrategy (org.locationtech.geowave.core.store.statistics.binning.DataTypeBinningStrategy)1 FieldStatisticType (org.locationtech.geowave.core.store.statistics.field.FieldStatisticType)1 IndexStatisticType (org.locationtech.geowave.core.store.statistics.index.IndexStatisticType)1