@Override
 protected void doWriteTo(StreamOutput out) throws IOException {
   script.writeTo(out);
   ValueFormatterStreams.writeOptional(formatter, out);
   gapPolicy.writeTo(out);
   out.writeGenericValue(bucketsPathsMap);
 }
 @Override
 protected XContentBuilder internalXContent(XContentBuilder builder, Params params)
     throws IOException {
   builder.field(BUCKETS_PATH.getPreferredName(), bucketsPathsMap);
   builder.field(ScriptField.SCRIPT.getPreferredName(), script);
   builder.field(GAP_POLICY.getPreferredName(), gapPolicy.getName());
   return builder;
 }
 @SuppressWarnings("unchecked")
 @Override
 protected void doReadFrom(StreamInput in) throws IOException {
   script = Script.readScript(in);
   formatter = ValueFormatterStreams.readOptional(in);
   gapPolicy = GapPolicy.readFrom(in);
   bucketsPathsMap = (Map<String, String>) in.readGenericValue();
 }
 @Override
 protected void doWriteTo(StreamOutput out) throws IOException {
   out.writeVInt(bucketsPathsMap.size());
   for (Entry<String, String> e : bucketsPathsMap.entrySet()) {
     out.writeString(e.getKey());
     out.writeString(e.getValue());
   }
   script.writeTo(out);
   gapPolicy.writeTo(out);
 }
 /** Read from a stream. */
 public BucketSelectorPipelineAggregationBuilder(StreamInput in) throws IOException {
   super(in, NAME);
   int mapSize = in.readVInt();
   bucketsPathsMap = new HashMap<String, String>(mapSize);
   for (int i = 0; i < mapSize; i++) {
     bucketsPathsMap.put(in.readString(), in.readString());
   }
   script = new Script(in);
   gapPolicy = GapPolicy.readFrom(in);
 }
Exemplo n.º 6
0
 @Override
 protected MovAvgPipelineAggregationBuilder createTestAggregatorFactory() {
   String name = randomAsciiOfLengthBetween(3, 20);
   String bucketsPath = randomAsciiOfLengthBetween(3, 20);
   MovAvgPipelineAggregationBuilder factory =
       new MovAvgPipelineAggregationBuilder(name, bucketsPath);
   if (randomBoolean()) {
     factory.format(randomAsciiOfLengthBetween(1, 10));
   }
   if (randomBoolean()) {
     factory.gapPolicy(randomFrom(GapPolicy.values()));
   }
   if (randomBoolean()) {
     switch (randomInt(4)) {
       case 0:
         factory.modelBuilder(new SimpleModel.SimpleModelBuilder());
         factory.window(randomIntBetween(1, 100));
         break;
       case 1:
         factory.modelBuilder(new LinearModel.LinearModelBuilder());
         factory.window(randomIntBetween(1, 100));
         break;
       case 2:
         if (randomBoolean()) {
           factory.modelBuilder(new EwmaModel.EWMAModelBuilder());
           factory.window(randomIntBetween(1, 100));
         } else {
           factory.modelBuilder(new EwmaModel.EWMAModelBuilder().alpha(randomDouble()));
           factory.window(randomIntBetween(1, 100));
         }
         break;
       case 3:
         if (randomBoolean()) {
           factory.modelBuilder(new HoltLinearModel.HoltLinearModelBuilder());
           factory.window(randomIntBetween(1, 100));
         } else {
           factory.modelBuilder(
               new HoltLinearModel.HoltLinearModelBuilder()
                   .alpha(randomDouble())
                   .beta(randomDouble()));
           factory.window(randomIntBetween(1, 100));
         }
         break;
       case 4:
       default:
         if (randomBoolean()) {
           factory.modelBuilder(new HoltWintersModel.HoltWintersModelBuilder());
           factory.window(randomIntBetween(2, 100));
         } else {
           int period = randomIntBetween(1, 100);
           factory.modelBuilder(
               new HoltWintersModel.HoltWintersModelBuilder()
                   .alpha(randomDouble())
                   .beta(randomDouble())
                   .gamma(randomDouble())
                   .period(period)
                   .seasonalityType(randomFrom(SeasonalityType.values()))
                   .pad(randomBoolean()));
           factory.window(randomIntBetween(2 * period, 200 * period));
         }
         break;
     }
   }
   factory.predict(randomIntBetween(1, 50));
   if (factory.model().canBeMinimized() && randomBoolean()) {
     factory.minimize(randomBoolean());
   }
   return factory;
 }
  public static BucketSelectorPipelineAggregationBuilder parse(
      String reducerName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = null;
    GapPolicy gapPolicy = null;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.VALUE_STRING) {
        if (context.getParseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
          bucketsPathsMap = new HashMap<>();
          bucketsPathsMap.put("_value", parser.text());
        } else if (context.getParseFieldMatcher().match(currentFieldName, GAP_POLICY)) {
          gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
        } else if (context.getParseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
          script = Script.parse(parser, context.getParseFieldMatcher());
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a "
                  + token
                  + " in ["
                  + reducerName
                  + "]: ["
                  + currentFieldName
                  + "].");
        }
      } else if (token == XContentParser.Token.START_ARRAY) {
        if (context.getParseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
          List<String> paths = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            String path = parser.text();
            paths.add(path);
          }
          bucketsPathsMap = new HashMap<>();
          for (int i = 0; i < paths.size(); i++) {
            bucketsPathsMap.put("_value" + i, paths.get(i));
          }
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a "
                  + token
                  + " in ["
                  + reducerName
                  + "]: ["
                  + currentFieldName
                  + "].");
        }
      } else if (token == XContentParser.Token.START_OBJECT) {
        if (context.getParseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
          script = Script.parse(parser, context.getParseFieldMatcher());
        } else if (context.getParseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
          Map<String, Object> map = parser.map();
          bucketsPathsMap = new HashMap<>();
          for (Map.Entry<String, Object> entry : map.entrySet()) {
            bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
          }
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a "
                  + token
                  + " in ["
                  + reducerName
                  + "]: ["
                  + currentFieldName
                  + "].");
        }
      } else {
        throw new ParsingException(
            parser.getTokenLocation(), "Unexpected token " + token + " in [" + reducerName + "].");
      }
    }

    if (bucketsPathsMap == null) {
      throw new ParsingException(
          parser.getTokenLocation(),
          "Missing required field ["
              + BUCKETS_PATH.getPreferredName()
              + "] for bucket_selector aggregation ["
              + reducerName
              + "]");
    }

    if (script == null) {
      throw new ParsingException(
          parser.getTokenLocation(),
          "Missing required field ["
              + ScriptField.SCRIPT.getPreferredName()
              + "] for bucket_selector aggregation ["
              + reducerName
              + "]");
    }

    BucketSelectorPipelineAggregationBuilder factory =
        new BucketSelectorPipelineAggregationBuilder(reducerName, bucketsPathsMap, script);
    if (gapPolicy != null) {
      factory.gapPolicy(gapPolicy);
    }
    return factory;
  }