Search in sources :

Example 1 with FieldStatisticType

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;
}
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)

Example 2 with FieldStatisticType

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;
}
Also used : StatisticType(org.locationtech.geowave.core.store.statistics.StatisticType) DataTypeStatisticType(org.locationtech.geowave.core.store.statistics.adapter.DataTypeStatisticType) FieldStatistic(org.locationtech.geowave.core.store.api.FieldStatistic) FieldStatisticType(org.locationtech.geowave.core.store.statistics.field.FieldStatisticType) ParameterException(com.beust.jcommander.ParameterException) DataStore(org.locationtech.geowave.core.store.api.DataStore) Parameter(com.beust.jcommander.Parameter) DataTypeStatistic(org.locationtech.geowave.core.store.api.DataTypeStatistic) IndexStatisticType(org.locationtech.geowave.core.store.statistics.index.IndexStatisticType) IndexStatistic(org.locationtech.geowave.core.store.api.IndexStatistic) Lists(com.clearspring.analytics.util.Lists) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) List(java.util.List) StatisticValue(org.locationtech.geowave.core.store.api.StatisticValue) StatisticsRegistry(org.locationtech.geowave.core.store.statistics.StatisticsRegistry) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) DataTypeAdapter(org.locationtech.geowave.core.store.api.DataTypeAdapter) Statistic(org.locationtech.geowave.core.store.api.Statistic) Index(org.locationtech.geowave.core.store.api.Index) FieldDescriptor(org.locationtech.geowave.core.store.adapter.FieldDescriptor) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) IndexStatistic(org.locationtech.geowave.core.store.api.IndexStatistic) StatisticValue(org.locationtech.geowave.core.store.api.StatisticValue) DataTypeAdapter(org.locationtech.geowave.core.store.api.DataTypeAdapter) Index(org.locationtech.geowave.core.store.api.Index) IndexStatisticType(org.locationtech.geowave.core.store.statistics.index.IndexStatisticType) FieldDescriptor(org.locationtech.geowave.core.store.adapter.FieldDescriptor) FieldStatistic(org.locationtech.geowave.core.store.api.FieldStatistic) DataTypeStatistic(org.locationtech.geowave.core.store.api.DataTypeStatistic) IndexStatistic(org.locationtech.geowave.core.store.api.IndexStatistic) Statistic(org.locationtech.geowave.core.store.api.Statistic) StatisticType(org.locationtech.geowave.core.store.statistics.StatisticType) DataTypeStatisticType(org.locationtech.geowave.core.store.statistics.adapter.DataTypeStatisticType) FieldStatisticType(org.locationtech.geowave.core.store.statistics.field.FieldStatisticType) IndexStatisticType(org.locationtech.geowave.core.store.statistics.index.IndexStatisticType) DataTypeStatistic(org.locationtech.geowave.core.store.api.DataTypeStatistic) ParameterException(com.beust.jcommander.ParameterException) FieldStatisticType(org.locationtech.geowave.core.store.statistics.field.FieldStatisticType) DataTypeStatisticType(org.locationtech.geowave.core.store.statistics.adapter.DataTypeStatisticType) FieldStatistic(org.locationtech.geowave.core.store.api.FieldStatistic)

Aggregations

ParameterException (com.beust.jcommander.ParameterException)2 List (java.util.List)2 CloseableIterator (org.locationtech.geowave.core.store.CloseableIterator)2 DataTypeStatistic (org.locationtech.geowave.core.store.api.DataTypeStatistic)2 FieldStatistic (org.locationtech.geowave.core.store.api.FieldStatistic)2 Index (org.locationtech.geowave.core.store.api.Index)2 IndexStatistic (org.locationtech.geowave.core.store.api.IndexStatistic)2 Statistic (org.locationtech.geowave.core.store.api.Statistic)2 StatisticValue (org.locationtech.geowave.core.store.api.StatisticValue)2 IndexStore (org.locationtech.geowave.core.store.index.IndexStore)2 DataStatisticsStore (org.locationtech.geowave.core.store.statistics.DataStatisticsStore)2 DataTypeStatisticType (org.locationtech.geowave.core.store.statistics.adapter.DataTypeStatisticType)2 FieldStatisticType (org.locationtech.geowave.core.store.statistics.field.FieldStatisticType)2 IndexStatisticType (org.locationtech.geowave.core.store.statistics.index.IndexStatisticType)2 Parameter (com.beust.jcommander.Parameter)1 Lists (com.clearspring.analytics.util.Lists)1 ArrayList (java.util.ArrayList)1 ConsoleTablePrinter (org.locationtech.geowave.core.cli.utils.ConsoleTablePrinter)1 FieldDescriptor (org.locationtech.geowave.core.store.adapter.FieldDescriptor)1 DataStore (org.locationtech.geowave.core.store.api.DataStore)1