Kibana Watcher actions html body if list exists condition

I have the following ES response:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "calling" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "9991",
          "doc_count" : 4,
          "called" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "7",
                "doc_count" : 4
              }
            ]
          }
        },
        {
          "key" : "9999",
          "doc_count" : 4,
          "called" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ ]
          }
        }
      ]
    }
  }
}

Here is my watcher action html body:

{{#ctx.payload.aggregations.calling.buckets}}

    {{#called.buckets.size}}
         <div>Number {{key}} called:</div>

         {{#called.buckets}}
              <div>{{doc_count}} times number {{key}}</div>
         {{/called.buckets}}

    {{/called.buckets.size}}

{{/ctx.payload.aggregations.calling.buckets}}

I want to use an if condition called.buckets.size > 0, basically, if inner bucket is empty, do nothing. I know that Mustache is logic-less, but is there another way to accomplish this task?

You can use a transform block in your actions section to put some conditionals/logic and build a new payload object/data structure that will be passed on to the email action. For an example of using a transform, see:

Alert only on 3 consecutive anomalies above a certain summed score · GitHub starting on line 98.

I build a new ArrayList called records and when passed on, it becomes the new ctx.payload

A different example: Watch with simple terms agg and threshold · GitHub

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.