Watcher Script Calculation

{
  "watch_id": "41f83e51-ea97-4383-8bae-f361fd14a81a",
  "node": "AUDLWhUORrCEs2E_lEhqng",
  "state": "execution_not_needed",
  "@timestamp": "2023-01-05T05:08:34.144Z",
  "user": "baolt",
  "status": {
    "state": {
      "active": true,
      "timestamp": "2023-01-05T05:06:06.760Z"
    },
    "last_checked": "2023-01-05T05:08:34.144Z",
    "last_met_condition": "2023-01-05T05:07:34.064Z",
    "actions": {
      "webhook_1": {
        "ack": {
          "timestamp": "2023-01-05T05:08:34.144Z",
          "state": "awaits_successful_execution"
        },
        "last_execution": {
          "timestamp": "2023-01-05T05:07:34.064Z",
          "successful": true
        },
        "last_successful_execution": {
          "timestamp": "2023-01-05T05:07:34.064Z",
          "successful": true
        }
      }
    },
    "execution_state": "execution_not_needed",
    "version": -1
  },
  "trigger_event": {
    "type": "schedule",
    "triggered_time": "2023-01-05T05:08:34.144Z",
    "schedule": {
      "scheduled_time": "2023-01-05T05:08:33.934Z"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "osbapp*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "must": [
                {
                  "query_string": {
                    "query": "type: osbpartner AND bankName: AGR AND _exists_: resCode"
                  }
                }
              ],
              "filter": {
                "range": {
                  "@timestamp": {
                    "gte": "{{ctx.trigger.scheduled_time}}||-65s",
                    "lte": "{{ctx.trigger.scheduled_time}}",
                    "format": "strict_date_optional_time||epoch_millis"
                  }
                }
              }
            }
          },
          "aggs": {
            "f": {
              "filters": {
                "filters": {
                  "success": {
                    "term": {
                      "resCode": "00"
                    }
                  },
                  "timeout": {
                    "term": {
                      "resCode": "68"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "if (ctx.payload.aggregations.f.buckets.timeout.doc_count > params.threshold) { return true; } return false;",
      "lang": "painless",
      "params": {
        "threshold": 1
      }
    }
  },
  "metadata": {
    "name": "AGR - FAIL ENQ PERCENTAGE",
    "xpack": {
      "type": "json"
    }
  },
  "result": {
    "execution_time": "2023-01-05T05:08:34.144Z",
    "execution_duration": 28,
    "input": {
      "type": "search",
      "status": "success",
      "payload": {
        "_shards": {
          "total": 324,
          "failed": 0,
          "successful": 324,
          "skipped": 306
        },
        "hits": {
          "hits": [],
          "total": 200,
          "max_score": null
        },
        "took": 26,
        "timed_out": false,
        "aggregations": {
          "f": {
            "buckets": {
              "success": {
                "doc_count": 192
              },
              "timeout": {
                "doc_count": 0
              }
            }
          }
        }
      },
      "search": {
        "request": {
          "search_type": "query_then_fetch",
          "indices": [
            "osbapp*"
          ],
          "rest_total_hits_as_int": true,
          "body": {
            "size": 0,
            "query": {
              "bool": {
                "must": [
                  {
                    "query_string": {
                      "query": "type: osbpartner AND bankName: AGR AND _exists_: resCode"
                    }
                  }
                ],
                "filter": {
                  "range": {
                    "@timestamp": {
                      "gte": "2023-01-05T05:08:33.934Z||-65s",
                      "lte": "2023-01-05T05:08:33.934Z",
                      "format": "strict_date_optional_time||epoch_millis"
                    }
                  }
                }
              }
            },
            "aggs": {
              "f": {
                "filters": {
                  "filters": {
                    "success": {
                      "term": {
                        "resCode": "00"
                      }
                    },
                    "timeout": {
                      "term": {
                        "resCode": "68"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "condition": {
      "type": "script",
      "status": "success",
      "met": false
    },
    "actions": []
  },
  "messages": []
}

I want to calculate the percentage of fail rescode in the total response code. The calculation is like this: (total - timeout - success) / total. So the script I use is (ctx.payload.hits.total - ctx.payload.aggregations.f.buckets.timeout.doc_count - ctx.payload.aggregations.f.buckets.success.doc_count) / ctx.payload.hits.total > params.threshold

But it seems not correct because even though I set the threshold to 0, it does not alert. Any can help me this? Thank you

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