Calculate time with "ctx.execution_time"

Hi everyone,I want to subtract 7 days from "ctx.execution_time" and transform it to a string, so my "transform" is as this:

 "transform": {
    "script": {
       "inline": "ctx.metadata.t1 = ctx.execution_time.minusDays(7).toString('yyyy-MM-dd HH:mm:ss.SSS')",
       "lang": "painless"
     }
  }

when i run the watch, it gives me the error:

  "transform": {
       "type": "script",
       "status": "failure",
       "reason": "ScriptException[runtime error]; nested: IllegalArgumentException[Unable to find dynamic method [minusDays] with [1] arguments for class [org.joda.time.DateTime].]; "
     }

but I check joda API with Java, the "minusDays" method exists in DateTime class. So, how should i write the transform? Thanks.

I find the reason, "ctx.execution_time" is a class of org.joda.time.DateTime for Java, but my watcher is wrotten with X-Pack 5.4, the default script language of X-Pack 5.x is "painless" but not Java, and, there is no DateTime class in painless!!!!

so, how can I calcuate time with org.joda.time.DateTime of Java in painless?? I feel painless script is not pain less, but pain more :disappointed:

1 Like

Hey,

try something like

return Instant.ofEpochMilli(ctx.execution_time.getMillis()).minus(7, ChronoUnit.DAYS)

in your transform.

--Alex

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.