Created
August 1, 2019 15:51
-
-
Save richcollier/5bc581d10a6dacb88ac25ba528e0df93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yields: