# Watcherのconditionに記述した計算結果が想定外

**URL:** https://discuss.elastic.co/t/watcher-condition/306174
**Category:** 日本語による質問・議論はこちら
**Tags:** painless
**Created:** [June 2, 2022, 12:51am UTC](https://discuss.elastic.co/t/watcher-condition/306174 "2022-06-02T00:51:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NikaidoKota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikaidokota/32/106257_2.png) [@NikaidoKota](https://discuss.elastic.co/u/NikaidoKota)
#### Post date: [June 2, 2022, 12:51am UTC](https://discuss.elastic.co/t/watcher-condition/306174/1 "2022-06-02T00:51:51Z")

</div>

お世話になります。  
別途「 [Watcherで各bucketのdoc\_countを集計したい](https://discuss.elastic.co/t/watcher-bucket-doc-count/305789/16)」というスレッドでご質問させていただいた内容について、派生して新たな不明点が挙がってしまったため、改めてスレッドを立てさせていただきました。

具体的には、作成したwatcherについて、以下のconditionを満たしていないにも関わらずメール通知が飛ぶようになってします。

```auto
"condition": {
  "script": {
    "source": "if ( ctx.payload.aggregations.RESULT.buckets.SUCCESS.doc_count / ctx.payload.aggregations.RESULT.buckets.ALL.doc_count < params.threshold ) { return true; } return false;",
    "lang": "painless",
    "params": {
      "threshold": 0.85
    }
  }
},

```

本来やりたかった内容としては、  
SUCCESSの件数 / ALLの件数 = SUCCESSの割合 を求め、  
SUCCESSの割合が閾値（threshold）未満となっている場合にアラートを飛ばす、というものです。

watcherの履歴としてはエラーは発生しておらず、ログに記載されたresultの内容は以下の通りでした。  
（aggregationsのうち、ERRとついているものはconditionには利用しておらず、別途通知の文面に利用するために算出したものです）

```auto
"result": {
  "execution_time": "2022-05-30T08:18:11.145Z",
  "execution_duration": 72,
  "input": {
    "type": "search",
    "status": "success",
    "payload": {
      "_shards": {
        "total": 15,
        "failed": 0,
        "successful": 15,
        "skipped": 0
      },
      "hits": {
        "hits": [],
        "total": 4801,
        "max_score": null
      },
      "took": 5,
      "timed_out": false,
      "aggregations": {
        "RESULT": {
          "buckets": {
            "ALL": {
              "doc_count": 4801
            },
            "SUCCESS": {
              "doc_count": 4379
            },
            "ERR_1": {
              "doc_count": 0
            },
            "ERR_2": {
              "doc_count": 177
            },
            "OTHER_ERR": {
              "doc_count": 245
            }
          }
        }
      }
    },

```

上記のresultによればSUCCESSの割合は  
4379 / 4801 = 0.912...  
となるので発火しない想定でしたが、通知が飛んでしまっています。  
閾値を変更しながら通知を確認してみたのですが、conditionの条件が閾値によらず発火する状態になってしまっているようです。

確認のため、以下のconditionを同バケット対して設定してみたところ、どちらもtrueを返してしまっています。

```auto
  "condition": {
    "script": {
      "source": "ctx.payload.aggregations.RESULT.buckets.SUCCESS.doc_count * ctx.payload.aggregations.RESULT.buckets.ALL.doc_count == 0",
      "lang": "painless"
    }
  },

```

```auto
"condition": {
  "script": {
    "source": "ctx.payload.aggregations.RESULT.buckets.SUCCESS.doc_count / ctx.payload.aggregations.RESULT.buckets.ALL.doc_count == 0",
    "lang": "painless"
  }
},

```

つまり少なくとも ctx.payload.aggregations.RESULT.buckets.SUCCESS.doc\_count が0を返している、または\*演算子および/演算子が正しく処理できていない、という結果だと思います。  
しかしながら、以下の結果を確認するとこちらはそれぞれfalseなので、 condition内のscriptにおいてctx.payload.aggregations.RESULT.buckets.SUCCESS.doc\_count が0を返しているというわけではなさそうです。

```auto
"condition": {
  "script": {
    "source": "ctx.payload.aggregations.RESULT.buckets.ALL.doc_count == 0",
    "lang": "painless"
  }
},

```

```auto
"condition": {
  "script": {
    "source": "ctx.payload.aggregations.RESULT.buckets.SUCCESS.doc_count == 0",
    "lang": "painless"
  }
},

```

何か本事象について回避策や、改善すべき点をご存知の方はいらっしゃいますでしょうか。  
よろしくお願いいたします。

---

<div class="post-metadata">

### Author: ![NikaidoKota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikaidokota/32/106257_2.png) [@NikaidoKota](https://discuss.elastic.co/u/NikaidoKota)
#### Post date: [June 6, 2022, 1:16am UTC](https://discuss.elastic.co/t/watcher-condition/306174/2 "2022-06-06T01:16:09Z")

</div>

本件について、以下の結果はfalseとなっておりました。

```auto
  "condition": {
    "script": {
      "source": "ctx.payload.aggregations.RESULT.buckets.SUCCESS.doc_count ＋ ctx.payload.aggregations.RESULT.buckets.ALL.doc_count == 0",
      "lang": "painless"
    }
  },

```

したがって乗算と除算の場合に結果がおかしくなっているようです。  
原因に皆目見当がつかないような状態なので、何かアイデア一つでも頂戴できれば幸いです。

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 4, 2022, 1:16am UTC](https://discuss.elastic.co/t/watcher-condition/306174/3 "2022-07-04T01:16:28Z")

</div>

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