Skip to content

Instantly share code, notes, and snippets.

@richcollier
Created August 1, 2019 15:51
Show Gist options
  • Save richcollier/5bc581d10a6dacb88ac25ba528e0df93 to your computer and use it in GitHub Desktop.
Save richcollier/5bc581d10a6dacb88ac25ba528e0df93 to your computer and use it in GitHub Desktop.
POST _watcher/watch/_execute
{
"watch": {
"trigger": {
"schedule": {
"interval": "5m"
}
},
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
".ml-anomalies-*"
],
"body": {
"size": 1,
"query": {
"bool": {
"filter": [
{
"range": {
"timestamp": {
"gte": "now-{{ctx.metadata.watch_timespan}}"
}
}
},
{
"regexp": {
"job_id": "{{ctx.metadata.job_name_regex}}"
}
},
{
"range": {
"record_score": {
"gte": "{{ctx.metadata.job_min_record_score}}"
}
}
}
]
}
},
"aggs": {
"max_record_score": {
"max": {
"field": "record_score"
}
},
"jobs": {
"terms": {
"field": "job_id"
}
}
}
}
}
}
},
"condition": {
"script": {
"source": "ctx.payload.hits.total > 0",
"lang": "painless"
}
},
"actions": {
"log": {
"transform": {
"script": """
ArrayList job_list=new ArrayList();
for (def i=0; i< ctx.payload.aggregations.jobs.buckets.size(); i++) {
job_list.add(ctx.payload.aggregations.jobs.buckets[i].key);
}
return ['job_list': job_list, 'max_record_score': Math.round(ctx.payload.aggregations.max_record_score.value), '@timestamp': ctx.execution_time, 'total_critical_anomalies':ctx.payload.hits.total] ;
"""
},
"logging": {
"level": "info",
"text": """
total_critical_anomalies: {{ctx.payload.total_critical_anomalies}}
max_record_score: {{ctx.payload.max_record_score}}
@timestamp: {{ctx.payload.@timestamp}}
jobs: {{ctx.payload.job_list}}
"""
}
},
"index_payload": {
"transform": {
"script": """
ArrayList job_list=new ArrayList();
for (def i=0; i< ctx.payload.aggregations.jobs.buckets.size(); i++) {
job_list.add(ctx.payload.aggregations.jobs.buckets[i].key);
}
return ['job_list': job_list, 'max_record_score': Math.round(ctx.payload.aggregations.max_record_score.value), '@timestamp': ctx.execution_time, 'total_critical_anomalies':ctx.payload.hits.total] ;
"""
},
"index": {
"index": "ml_summary"
}
}
},
"metadata": {
"watch_timespan": "5y",
"job_name_regex": "farequote.*",
"job_min_record_score": 90
}
}
}
@richcollier
Copy link
Author

GET ml_summary/_search

yields:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ml_summary",
        "_type" : "_doc",
        "_id" : "EeLhTWwBEx7iVUUAXjCI",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2019-08-01T15:51:51.120Z",
          "job_list" : [
            "farequote_multi",
            "farequote_count_split",
            "farequote_median",
            "farequote_model",
            "farequote_responsetime",
            "farequote_responsetime_split",
            "farequote_scripted",
            "farequote_count_nosplit",
            "farequote_nosplit",
            "farequote_resp_by_airline"
          ],
          "total_critical_anomalies" : 26,
          "max_record_score" : 99
        }
      }
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment