Search in sources :

Example 6 with Output

use of org.opensearch.ml.common.parameter.Output in project ml-commons by opensearch-project.

the class LocalSampleCalculator method execute.

@Override
public Output execute(Input input) {
    if (input == null || !(input instanceof LocalSampleCalculatorInput)) {
        throw new IllegalArgumentException("wrong input");
    }
    LocalSampleCalculatorInput sampleCalculatorInput = (LocalSampleCalculatorInput) input;
    String operation = sampleCalculatorInput.getOperation();
    List<Double> inputData = sampleCalculatorInput.getInputData();
    switch(operation) {
        case "sum":
            double sum = inputData.stream().mapToDouble(f -> f.doubleValue()).sum();
            return new SampleAlgoOutput(sum);
        case "max":
            double max = inputData.stream().max(Comparator.naturalOrder()).get();
            return new SampleAlgoOutput(max);
        case "min":
            double min = inputData.stream().min(Comparator.naturalOrder()).get();
            return new SampleAlgoOutput(min);
        default:
            throw new IllegalArgumentException("can't support this operation");
    }
}
Also used : Client(org.opensearch.client.Client) Executable(org.opensearch.ml.engine.Executable) Function(org.opensearch.ml.engine.annotation.Function) Settings(org.opensearch.common.settings.Settings) SampleAlgoOutput(org.opensearch.ml.common.parameter.SampleAlgoOutput) Input(org.opensearch.ml.common.parameter.Input) Output(org.opensearch.ml.common.parameter.Output) LocalSampleCalculatorInput(org.opensearch.ml.common.parameter.LocalSampleCalculatorInput) List(java.util.List) FunctionName(org.opensearch.ml.common.parameter.FunctionName) Data(lombok.Data) Comparator(java.util.Comparator) NoArgsConstructor(lombok.NoArgsConstructor) LocalSampleCalculatorInput(org.opensearch.ml.common.parameter.LocalSampleCalculatorInput) SampleAlgoOutput(org.opensearch.ml.common.parameter.SampleAlgoOutput)

Example 7 with Output

use of org.opensearch.ml.common.parameter.Output in project ml-commons by opensearch-project.

the class AnomalyLocalizerImpl method processNewEntry.

private void processNewEntry(AnomalyLocalizationInput input, AggregationBuilder agg, AnomalyLocalizationOutput.Result result, AnomalyLocalizationOutput.Bucket bucket, Optional<Map<String, Object>> afterKey, PriorityQueue<AnomalyLocalizationOutput.Entity> queue, AnomalyLocalizationOutput output, ActionListener<AnomalyLocalizationOutput> listener) {
    SearchRequest request = newSearchRequestForEntry(input, agg, bucket, afterKey);
    client.search(request, wrap(r -> onNewEntryResponse(r, input, agg, result, bucket, queue, output, listener), listener::onFailure));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SneakyThrows(lombok.SneakyThrows) PriorityQueue(java.util.PriorityQueue) NotifyOnceListener(org.opensearch.action.NotifyOnceListener) Executable(org.opensearch.ml.engine.Executable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Input(org.opensearch.ml.common.parameter.Input) ArrayList(java.util.ArrayList) CompositeAggregationBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder) Output(org.opensearch.ml.common.parameter.Output) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) LatchedActionListener(org.opensearch.action.LatchedActionListener) KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Map(java.util.Map) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryBuilders(org.opensearch.index.query.QueryBuilders) Client(org.opensearch.client.Client) MAX_BUCKET_SETTING(org.opensearch.search.aggregations.MultiBucketConsumerService.MAX_BUCKET_SETTING) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) Settings(org.opensearch.common.settings.Settings) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) Collectors(java.util.stream.Collectors) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) List(java.util.List) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) Optional(java.util.Optional) ActionListener.wrap(org.opensearch.action.ActionListener.wrap) Collections(java.util.Collections) TermsValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest)

Aggregations

Input (org.opensearch.ml.common.parameter.Input)7 Output (org.opensearch.ml.common.parameter.Output)7 List (java.util.List)5 Data (lombok.Data)5 ActionListener (org.opensearch.action.ActionListener)5 SearchRequest (org.opensearch.action.search.SearchRequest)5 SearchResponse (org.opensearch.action.search.SearchResponse)5 Client (org.opensearch.client.Client)5 Settings (org.opensearch.common.settings.Settings)5 Executable (org.opensearch.ml.engine.Executable)5 SimpleEntry (java.util.AbstractMap.SimpleEntry)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Optional (java.util.Optional)4 PriorityQueue (java.util.PriorityQueue)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4