Last active
November 14, 2022 21:53
-
-
Save richcollier/1c2b8161286bdca6c553859f28d3d66d 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
#record farequote watch with link to Single Metric Viewer | |
POST _xpack/watcher/watch/_execute | |
{ | |
"watch": { | |
"trigger": { | |
"schedule": { | |
"interval": "5m" | |
} | |
}, | |
"metadata": { | |
"job_id": "farequote_responsetime", | |
"min_record_score": 90 | |
}, | |
"input": { | |
"search": { | |
"request": { | |
"indices": [ | |
".ml-anomalies-*" | |
], | |
"body": { | |
"query": { | |
"bool": { | |
"filter": [ | |
{ | |
"range": { | |
"timestamp": { | |
"gte": "now-10m" | |
} | |
} | |
}, | |
{ | |
"term": { | |
"result_type": "record" | |
} | |
}, | |
{ | |
"term": { | |
"job_id": "{{ctx.metadata.job_id}}" | |
} | |
}, | |
{ | |
"range": { | |
"record_score": { | |
"gte": "{{ctx.metadata.min_record_score}}" | |
} | |
} | |
} | |
] | |
} | |
}, | |
"script_fields": { | |
"start": { | |
"script": { | |
"lang": "painless", | |
"source": """ | |
LocalDateTime.ofEpochSecond((doc["timestamp"].value.getMillis()-((doc["bucket_span"].value * 1000) * params.padding)) / 1000, 0, ZoneOffset.UTC).toString()+":00.000Z" | |
""", | |
"params": { | |
"padding": 10 | |
} | |
} | |
}, | |
"end": { | |
"script": { | |
"lang": "painless", | |
"source": """ | |
LocalDateTime.ofEpochSecond((doc["timestamp"].value.getMillis()+((doc["bucket_span"].value * 1000) * params.padding)) / 1000, 0, ZoneOffset.UTC).toString()+":00.000Z" | |
""", | |
"params": { | |
"padding": 10 | |
} | |
} | |
}, | |
"timestamp_epoch": { | |
"script": { | |
"lang": "painless", | |
"source": """doc["timestamp"].value.getMillis()/1000""" | |
} | |
}, | |
"timestamp_iso8601": { | |
"script": { | |
"lang": "painless", | |
"source": """doc["timestamp"].value""" | |
} | |
}, | |
"split": { | |
"script": { | |
"lang": "painless", | |
"source": """doc["partition_field_value"]""" | |
} | |
}, | |
"actual": { | |
"script": { | |
"lang": "painless", | |
"source": """Math.round(doc["actual"].value)""" | |
} | |
}, | |
"typical": { | |
"script": { | |
"lang": "painless", | |
"source": """Math.round(doc["typical"].value)""" | |
} | |
}, | |
"score": { | |
"script": { | |
"lang": "painless", | |
"source": """Math.round(doc["record_score"].value)""" | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
"condition": { | |
"compare": { | |
"ctx.payload.hits.total": { | |
"gt": 0 | |
} | |
} | |
}, | |
"actions": { | |
"log": { | |
"transform": { | |
"script": """ | |
return ctx.payload.hits.hits.stream() | |
.map(p -> [ | |
'airline':p.fields.split.0, | |
'score':p.fields.score.0, | |
'actual':p.fields.actual.0, | |
'typical':p.fields.typical.0, | |
'timestamp':p.fields.timestamp_iso8601.0, | |
'start':p.fields.start.0, | |
'end':p.fields.end.0 | |
]) | |
.collect(Collectors.toList()); | |
""" | |
}, | |
"logging": { | |
"text": """ | |
Anomalies: | |
========== | |
{{#ctx.payload._value}} | |
time={{timestamp}} | |
airline={{airline}} | |
score={{score}} (out of 100) | |
responsetime={{actual}}ms (typical={{typical}}ms) | |
link= http://localhost:5601/app/ml#/timeseriesexplorer/?_g=(ml:(jobIds:!({{ctx.metadata.job_id}})),refreshInterval:(display:Off,pause:!f,value:0),time:(from:'{{start}}',mode:absolute,to:'{{end}}'))&_a=(filters:!(),mlSelectInterval:(interval:(display:Auto,val:auto)),mlSelectSeverity:(threshold:(display:warning,val:0)),mlTimeSeriesExplorer:(detectorIndex:0,entities:(airline:{{airline}})),query:(query_string:(analyze_wildcard:!t,query:'*'))) | |
{{/ctx.payload._value}} | |
""" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you've created a job and declared influencers, and if those influencers show in the record results, you can just reference them by name. Look at the document for results_type:record in .ml-anomalies-* and you'll see what I mean. For example: