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

@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