Enniu_51
(Sayakiss)
October 20, 2015, 9:24am
1
curl -XGET 'http://localhost:9200/.watch_history*/_search?pretty' -d '{ "sort" : [ { "result.execution_time" : "desc" } ]}'
:
"condition": {
"type": "script",
"status": "success",
"met": true
},
"transform": {
"type": "script",
"status": "failure",
"reason": "GroovyScriptExecutionException[MissingPropertyException[No such property: org for class: Script8]]"
}
I use Watcher Java API
to build Transform:
watchSourceBuilder.transform(new ScriptTransform(new Script.Builder.DefaultType(scriptContent).build()));
Script code:
import org.elasticsearch.common.joda.time.DateTime
import org.elasticsearch.common.joda.time.Period
import org.elasticsearch.common.joda.time.format.DateTimeFormatter
String rangeString = '''PT1M''';
DateTimeFormatter fmt = org.joda.time.format.ISODateTimeFormat.dateTime();
DateTime scheduleTime = fmt.parseDateTime(ctx.trigger.triggered_time.toString());
Period period = org.joda.time.format.ISOPeriodFormat.standard().parsePeriod(rangeString);
DateTime beginTime = scheduleTime.minus(period);
return [beginTime:beginTime, endTime:scheduleTime];
Enniu_51
(Sayakiss)
October 20, 2015, 9:33am
2
@spinscale Do you have any idea?
Enniu_51
(Sayakiss)
October 20, 2015, 1:27pm
3
String rangeString = '''${range}''';
scheduleTime = org.joda.time.format.ISODateTimeFormat.dateTime().parseDateTime(ctx.trigger.triggered_time.toString());
period = org.joda.time.format.ISOPeriodFormat.standard().parsePeriod(rangeString);
beginTime = scheduleTime.minus(period);
return [beginTime:beginTime, endTime:scheduleTime];
I have tried that code, but it still gives:
"transform":{"type":"script","status":"failure","reason":"GroovyScriptExecutionException[MissingPropertyException[No such property: org for class: Script10]]"}
pickypg
(Chris Earle)
October 22, 2015, 4:24am
4
Hi Enniu,
Similar to the other Joda time classes, you need to use the Elasticsearch-shaded variants with the ISODateTimeFormat
and ISOPeriodFormat
. Change
org.joda.time.format.ISODateTimeFormat
org.joda.time.format.ISOPeriodFormat
to
org.elasticsearch.common.joda.time.format.ISODateTimeFormat
org.elasticsearch.common.joda.time.format.ISOPeriodFormat
then it should do what you expect.
Enniu_51
(Sayakiss)
October 22, 2015, 5:54am
5
Wow, that works!
But I feel strange, because @spinscale gave me such a transform:
"transform" : {
"script" : "time = org.joda.time.format.ISODateTimeFormat.dateTime().withZone(org.joda.time.DateTimeZone.forID(\"Europe/Berlin\")).parseDateTime(ctx.trigger.triggered_time.toString()).toString() ; return [ time_as_string : time ]"
}
Why he use org.joda.time.format.ISODateTimeFormat
there?
relate post:
Hey there,
you can use the the joda date time formatters to do this. This is just a quick test and it worked, I think you can do this more efficiently.
curl -XPUT 'http://localhost:9200/_watcher/watch/log_error_watch' -d '{
"trigger" : { "schedule" : { "interval" : "10s" } },
"input" : {
"search" : {
"request" : {
"indices" : [ "logs" ],
"body" : {
"query" : {
"match" : { "message": "error" }
}
}
}
}
},
"action…
Enniu_51
(Sayakiss)
October 22, 2015, 6:10am
6
I find I need original payload, so I change my return to:
return [origin_payload:ctx.payload, begin_time:beginTime, end_time:scheduleTime];
That works, but I wonder is there a way which will append new result to the payload rather than replace?
spinscale
(Alexander Reelsen)
October 26, 2015, 7:58am
7
@Enniu_51 I tested with a 2.0.0-rc1 build, which needed a different import (because shading is gone, see this blog post from last week)
transform returns a new payload. So to shorten things a bit in the script you could also just add something like ctx.payload.foo = whatver ; return ctx.payload
- but thats what you have to do.
--Alex
Enniu_51
(Sayakiss)
October 26, 2015, 8:02am
8
By return ctx.payload
, then we will have some thing like ctx.payload.payload.whatever
?