Watcher that counts the documents that arrive to an index in kibana

what those?

Sorry it means once we know the Magic Incantation... sometime we say that when we need to just know the exact syntax ... its kinda a joke / frustration about knowing the exact syntax ... and close does not work.

And here you go...

When you create that time variable it is part of ctx.payload

NOTE / EDIT this does not work as it writes over a payload.

  "actions": {
    "notify-slack": {
      "slack": {
        "message": {
          "to": [
            "#stephenb-es-integration"
          ],
          "text": "{{ctx.payload.time}} {{ctx.metadata.name}} : Encountered {{ctx.payload.aggregations.heartbeat_count.value}} heartbeats in the last 1 day"
        }
      }
    }
  },

  "transform" : {
    "script" : "return [ 'time' : ctx.trigger.triggered_time.withZoneSameInstant(ZoneId.of('America/Bogota')).format(DateTimeFormatter.ofPattern('YYYY-MM-dd HH:mm:ss')) ]" 
  }
}

NOTE I just changed to triggered_time since that is probable more accurate

output

APP [5:36 PM]
2021-04-23 19:36:28 test-heartbeat-watcher : Encountered heartbeats in the last 1 day

Ohh I just noticed that broke the payload again that must be what is happening!

Let me fix!!!

Ahhh I see it right here

A payload transform that executes a script on the current payload in the watch execution context and replaces it with a newly generated one. The following snippet shows how a simple script payload transform can be defined on the watch level:

A simple painless script that creates a new payload with a single time field holding the scheduled time.

So it is writing over our payload!!!

I will need to research ... I am sure it is simple...

ok, I hope it can be done

i'll figure it out but may not be tonight

And full circle... this may have been easier with kibana alerts :slight_smile:

After I figure this out I will do that

@Juan_David_Jaramillo

And here you go :slight_smile:

The Trick or (Magic Incantation) is that since the transform creates a new payload that overwrites the existing one, you need to save into the new payload the fields you want to use from the old payload ... and then reference them directly.

Note since I renamed / copied in the heartbeat_count.value into the new payload it is now at the top level of the new ctx.payload object

  "actions": {
    "notify-slack": {
      "slack": {
        "message": {
          "to": [
            "#stephenb-es-integration"
          ],
          "text": "{{ctx.metadata.name}} executed at {{ctx.payload.local_execution_time}} : Encountered {{ctx.payload.heartbeat_count}} heartbeats in the last 1 day"
        }
      }
    }
  },
  "transform": {
    "script": {
      "source": """
        return [
        'local_execution_time' : ctx.trigger.triggered_time.withZoneSameInstant(ZoneId.of('America/Bogota')).format(DateTimeFormatter.ofPattern('YYYY-MM-dd HH:mm:ss')),
        'heartbeat_count' : ctx.payload.aggregations.heartbeat_count.value
          ]
      """,
      "lang": "painless"
    }
  }
}

And the results ...

test-heartbeat-watcher executed at 2021-04-23 20:44:46 : Encountered 60480 heartbeats in the last 1 day

test-heartbeat-watcher executed at 2021-04-23 20:45:01 : Encountered 60480 heartbeats in the last 1 day

1 Like

Very Thanks!!! Amazing! :slight_smile:

1 Like

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