<commit_before>package org.opencb.opencga.storage.core.variant.stats;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.commons.lang3.time.StopWatch;
import org.opencb.biodata.models.variant.Genotype;
import org.opencb.biodata.models.variant.avro.VariantType;
import org.opencb.biodata.models.variant.metadata.DepthCount;
import org.opencb.biodata.models.variant.metadata.IndelLength;
import org.opencb.biodata.models.variant.metadata.SampleVariantStats;
import org.opencb.commons.datastore.core.DataResult;
import org.opencb.commons.datastore.core.FacetField;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.opencga.storage.core.exceptions.StorageEngineException;
import org.opencb.opencga.storage.core.metadata.models.SampleMetadata;
import org.opencb.opencga.storage.core.metadata.models.TaskMetadata;
import org.opencb.opencga.storage.core.variant.VariantStorageEngine;
import org.opencb.opencga.storage.core.variant.adaptors.GenotypeClass;
import org.opencb.opencga.storage.core.variant.adaptors.VariantQueryException;
import org.opencb.opencga.storage.core.variant.query.VariantQueryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

import static org.opencb.opencga.storage.core.variant.adaptors.VariantQueryParam.SAMPLE;
import static org.opencb.opencga.storage.core.variant.adaptors.VariantQueryParam.STUDY;

public class SampleVariantStatsAggregationQuery {

    private static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool(new BasicThreadFactory.Builder()
            .namingPattern("sample-variant-stats-pool-%s")
            .build());
    private final VariantStorageEngine engine;

    private Logger logger = LoggerFactory.getLogger(SampleVariantStatsAggregationQuery.class);

    public SampleVariantStatsAggregationQuery(VariantStorageEngine engine) {
        this.engine = engine;
    }


    public DataResult<SampleVariantStats> sampleStatsQuery(String studyStr, String sample, Query inputQuery) throws StorageEngineException {

        if (StringUtils.isEmpty(sample)) {
            throw new VariantQueryException("Missing sample");
        }
        int studyId = engine.getMetadataManager().getStudyId(studyStr);
        int sampleId = engine.getMetadataManager().getSampleIdOrFail(studyId, sample);

        Query query;
        if (inputQuery == null) {
            query = new Query();
        } else {
            query = new Query(inputQuery);
        }

        query.put(STUDY.key(), studyStr);
        query.remove(SAMPLE.key());

        // Test if there is any valid VariantAggregationExecutor
        engine.getVariantAggregationExecutor(new Query(query), new QueryOptions(QueryOptions.FACET, "chromosome"));

        Future<DataResult<FacetField>> submit = THREAD_POOL.submit(() -> {
            DataResult<FacetField> result = engine.facet(<commit_msg>storage: Fix check available aggregationExecutor on SampleVariantStatsAggregationQuery #1761<commit_after>package org.opencb.opencga.storage.core.variant.stats;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.commons.lang3.time.StopWatch;
import org.opencb.biodata.models.variant.Genotype;
import org.opencb.biodata.models.variant.avro.VariantType;
import org.opencb.biodata.models.variant.metadata.DepthCount;
import org.opencb.biodata.models.variant.metadata.IndelLength;
import org.opencb.biodata.models.variant.metadata.SampleVariantStats;
import org.opencb.commons.datastore.core.DataResult;
import org.opencb.commons.datastore.core.FacetField;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.opencga.storage.core.exceptions.StorageEngineException;
import org.opencb.opencga.storage.core.metadata.models.SampleMetadata;
import org.opencb.opencga.storage.core.metadata.models.TaskMetadata;
import org.opencb.opencga.storage.core.variant.VariantStorageEngine;
import org.opencb.opencga.storage.core.variant.adaptors.GenotypeClass;
import org.opencb.opencga.storage.core.variant.adaptors.VariantQueryException;
import org.opencb.opencga.storage.core.variant.query.VariantQueryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

import static org.opencb.opencga.storage.core.variant.adaptors.VariantQueryParam.SAMPLE;
import static org.opencb.opencga.storage.core.variant.adaptors.VariantQueryParam.STUDY;

public class SampleVariantStatsAggregationQuery {

    private static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool(new BasicThreadFactory.Builder()
            .namingPattern("sample-variant-stats-pool-%s")
            .build());
    private final VariantStorageEngine engine;

    private Logger logger = LoggerFactory.getLogger(SampleVariantStatsAggregationQuery.class);

    public SampleVariantStatsAggregationQuery(VariantStorageEngine engine) {
        this.engine = engine;
    }


    public DataResult<SampleVariantStats> sampleStatsQuery(String studyStr, String sample, Query inputQuery) throws StorageEngineException {

        if (StringUtils.isEmpty(sample)) {
            throw new VariantQueryException("Missing sample");
        }
        int studyId = engine.getMetadataManager().getStudyId(studyStr);
        int sampleId = engine.getMetadataManager().getSampleIdOrFail(studyId, sample);

        Query query;
        if (inputQuery == null) {
            query = new Query();
        } else {
            query = new Query(inputQuery);
        }

        query.put(STUDY.key(), studyStr);
        query.remove(SAMPLE.key());

        // Test if there is any valid VariantAggregationExecutor
        // If no, fast fail
        engine.getVariantAggregationExecutor(new Query(query)
                .append(SAMPLE.key(), sample), new QueryOptions(QueryOptions.FACET, "chromosome"));

        Future<DataResult<FacetField>> submit = THREAD_POOL.submit(() -> {
            DataResult<FacetField> result = engine.facet(
{'fraction_non_alphanumeric': 0.1007619343803452, 'fraction_numerical': 0.0021769553724148654, 'mean_word_length': 4.521030042918455, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6654037', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1879, 'n_words': 294}