Webhook output formatting

Good afternoon!

There is a 'transform' from watcher:

"transform": {
      "script": {
        "source": "DateFormat dateFormat = new SimpleDateFormat (\" yyyy-MM-dd HH: mm: ss z \ "); dateFormat.setTimeZone (TimeZone.getTimeZone (\" Europe / Moscow \ ")); Date date = new Date (); ctx.execution_time = dateFormat.format (date); return ['human_date': ctx.execution_time, 'aggregations': ctx.payload.aggregations] ",
        "lang": "painless"
     }
   }

Via WebHook, the following output is sent to telegrams:

**** Watcher ****
Date / Time: 2019-09-29 10:24:55 MSK
Watcher name: test_watcher
Host: www.domain.com
Request Time (ms): 60.000999450683594

In the Request Time field, the time in milliseconds is displayed with a large number of decimal places. How can I reduce the number of characters to zero? So that there is nothing after the decimal point

Hi, could you explain or show me how the return value of your script results in the output you're showing me? Based on the script alone, it looks like the only date-related return value is the human_date variable, which would be formatted as yyyy-MM-dd HH: mm: ss z. How does that end up in a numeric milliseconds value?

Thanks,
CJ

Hi, I can show you my Watcher

{
  "trigger": {
    "schedule": {
      "interval": "3m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [ "weblog-*" ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "must": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "now-1m",
                      "lte": "now"
                    }
                  }
                }
              ],
              "must_not": [
                {
                  "query_string": {
                    "analyze_wildcard": true,
                    "fields": [
                      "http_host.keyword"
                    ],
                    "query": "dev.domain.com"
                  }
                }
              ]
            }
          },
          "aggregations": {
            "host": {
              "terms": {
                "field": "http_host.keyword",
                "order": {
                  "max_req_time": "desc"
                }
              },
              "aggregations": {
                "max_req_time": {
                  "max": {
                    "field": "request_time"
                  }
                },
                "max_agg_pipe": {
                  "bucket_selector": {
                    "buckets_path": {
                      "max_req_time": "max_req_time"
                    },
                    "script": {
                      "lang": "expression",
                      "source": "max_req_time > 16"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gt": 50
      }
    }
  },
  "actions": {
    "telegram": {
      "webhook": {
        "scheme": "http",
        "host": "api.telegram.com",
        "port": 81,
        "method": "post",
        "path": "/bot123456789:XXXXXXXX/sendMessage",
        "params": {},
        "headers": {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        "body": "chat_id=-123456789&text=****Watcher**** %0A\nDate/Time: {{ctx.payload.human_date}}%0A\nWatcher name: {{ctx.metadata.name}}%0A\nHost: {{#ctx.payload.aggregations.host.buckets}}\n{{key}}\n%0ARequest Time (ms): {{max_req_time.value}}%0A\n{{/ctx.payload.aggregations.host.buckets}}\n"
      }
    }
  },
  "transform": {
    "script": {
      "source": "DateFormat dateFormat = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss z\");dateFormat.setTimeZone(TimeZone.getTimeZone(\"Europe/Moscow\")); Date date = new Date(); ctx.execution_time = dateFormat.format(date); return ['human_date': ctx.execution_time, 'aggregations': ctx.payload.aggregations]",
      "lang": "painless"
    }
  }
}

P.S. values are taken from the elastic (and the elastic takes from the logs)

Thanks Aleksandr, I see you also posted this question in Форматирование вывода через Webhook, where Igor was able to answer your question. In the future, if you post the same question in multiple forums could you please link back and forth between the posts? This will help us avoid wasting time by answering the same question twice. Thanks!

CJ

I apologize for this, from now on I will do so. Thanks you!

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