<commit_before>---
"Test the execution of a Groovy closure in script condition":

  - do:
      bulk:
        refresh: true
        body: |
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "green", "@timestamp": "2005-01-01T00:00:00" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "yellow", "@timestamp": "2005-01-01T00:00:05" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "green", "@timestamp": "2005-01-01T00:00:55" }

  - do:
      warnings:
          - Groovy scripts are deprecated.  Use Painless scripts instead.
      xpack.watcher.put_watch:
        id: "watch_with_groovy_closure"
        body:  >
          {
            "trigger" : {
              "schedule" : { "cron" : "0 0 0 1 * ? 2099" }
            },
            "input" : {
              "search" : {
                "request" : {
                  "indices" : [ ".monitoring" ],
                  "body" : {
                    "query" : {
                      "match_all" : {
                      }
                    },
                    "aggregations" : {
                      "minutes" : {
                        "date_histogram" : {
                          "field" : "@timestamp",
                          "interval" : "5s",
                          "order" : {
                            "_count" : "desc"
                          }
                        },
                        "aggregations": {
                          "status" : {
                            "terms" : {
                              "field" : "status.keyword",
                              "size" : 3
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            },
            "condition" : {
              "script" : {
                "inline" : "if (ctx.payload.hits.total < 1) return false; def rows = ctx.payload.hits.hits; if (ctx.payload.aggregations.minutes.buckets.size() < 12) return false; def last60Seconds = ctx.payload.aggregations.minutes.buckets[-12..-1]; return last60Seconds.every { it.status.buckets.every { s -> s.key == 'red' } }",
                "lang": "groovy"
              }
            },
            "actions" : {
              "log" : {
                "logging" : {
                  "text" : "executed at {{ctx.execution_time}}"
                }
              }
            }
          }

  - match: { _id: "watch_with_groovy_closure" }

  - do:
      xpack.watcher.execute_watch:
        id: "watch_with_groovy_closure"
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-05-05T20:58:02.443Z",
              "triggered_time" : "2015-05-05T20:58:02.443Z"
            },
            "ignore_condition" : false,
            "action_modes" : {
              "_all" : "force_simulate"
            },
            "record_execution" : false
          }

  - match: { "watch_record.state": "execution_not_needed" }
  - match: { "watch_record.result.condition.met": false }

  - do:
      bulk:
        refresh: true
        body: |
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:00" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:05" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:10" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:15" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:20" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:25" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:30" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:35" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:40" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:45" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:50" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:55" }

  - do:
      xpack.watcher.execute_watch:
        id: "watch_with_groovy_closure"
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-05-05T20:58:02.443Z",
              "triggered_time" : "2015-05-05T20:58:02.443Z"
            },<commit_msg>Made deprecation of Groovy, Javascript, and Python more explicit.<commit_after>---
"Test the execution of a Groovy closure in script condition":

  - do:
      bulk:
        refresh: true
        body: |
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "green", "@timestamp": "2005-01-01T00:00:00" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "yellow", "@timestamp": "2005-01-01T00:00:05" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "green", "@timestamp": "2005-01-01T00:00:55" }

  - do:
      xpack.watcher.put_watch:
        id: "watch_with_groovy_closure"
        body:  >
          {
            "trigger" : {
              "schedule" : { "cron" : "0 0 0 1 * ? 2099" }
            },
            "input" : {
              "search" : {
                "request" : {
                  "indices" : [ ".monitoring" ],
                  "body" : {
                    "query" : {
                      "match_all" : {
                      }
                    },
                    "aggregations" : {
                      "minutes" : {
                        "date_histogram" : {
                          "field" : "@timestamp",
                          "interval" : "5s",
                          "order" : {
                            "_count" : "desc"
                          }
                        },
                        "aggregations": {
                          "status" : {
                            "terms" : {
                              "field" : "status.keyword",
                              "size" : 3
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            },
            "condition" : {
              "script" : {
                "inline" : "if (ctx.payload.hits.total < 1) return false; def rows = ctx.payload.hits.hits; if (ctx.payload.aggregations.minutes.buckets.size() < 12) return false; def last60Seconds = ctx.payload.aggregations.minutes.buckets[-12..-1]; return last60Seconds.every { it.status.buckets.every { s -> s.key == 'red' } }",
                "lang": "groovy"
              }
            },
            "actions" : {
              "log" : {
                "logging" : {
                  "text" : "executed at {{ctx.execution_time}}"
                }
              }
            }
          }

  - match: { _id: "watch_with_groovy_closure" }

  - do:
      warnings:
        - '[groovy] scripts are deprecated, use [painless] scripts instead'
      xpack.watcher.execute_watch:
        id: "watch_with_groovy_closure"
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-05-05T20:58:02.443Z",
              "triggered_time" : "2015-05-05T20:58:02.443Z"
            },
            "ignore_condition" : false,
            "action_modes" : {
              "_all" : "force_simulate"
            },
            "record_execution" : false
          }

  - match: { "watch_record.state": "execution_not_needed" }
  - match: { "watch_record.result.condition.met": false }

  - do:
      bulk:
        refresh: true
        body: |
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:00" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:05" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:10" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:15" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:20" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:25" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:30" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:35" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:40" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:45" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:50" }
          {"index": {"_index": ".monitoring", "_type": "cluster_stats"}}
          { "status": "red", "@timestamp": "2005-01-01T00:01:55" }

  - do:
      warnings:
        - '[groovy] scripts are deprecated, use [painless] scripts instead'
      xpack.watcher.execute_watch:
        id: "watch_with_groovy_closure"
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-05-05T20:58:02.443Z",
              "triggered_time" : "2015-05-05T20:58:02.443Z"
            },
{'fraction_non_alphanumeric': 0.17796450151057402, 'fraction_numerical': 0.05626888217522659, 'mean_word_length': 1.4726890756302522, 'pattern_counts': {'":': 160, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5569428', 'n_tokens_mistral': 3652, 'n_tokens_neox': 3195, 'n_words': 563}