elasticsearch - 用于计算 API 的 http 状态百分比的 Elasticsearch 查询
问题描述
我需要计算 API 的每个状态的百分比。
参考线程:Elasticsearch 查询以计算每个 API 的命中数
在上述线程的帮助下,我能够获得前 5 个 API 的状态计数。除此之外,我还想计算百分比。
目前我有这样的查询
{
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"from": "now-15m",
"to": "now",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"aggregations": {
"Url": {
"terms": {
"field": "data.url.keyword",
"size": 5,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"Status": {
"terms": {
"field": "data.response.status",
"size": 5,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
}
}
我得到如下输出
"aggregations": {
"Url": {
"doc_count_error_upper_bound": 940,
"sum_other_doc_count": 52374,
"buckets": [
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3261,
"key": 200
},
{
"doc_count": 254,
"key": 400
}
]
},
"doc_count": 3515,
"key": "/account/me"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3376,
"key": 200
}
]
},
"doc_count": 3385,
"key": "/PlanDetails"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3282,
"key": 200
}
]
},
"doc_count": 3282,
"key": "/evaluation"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3205,
"key": 200
}
]
},
"doc_count": 3205,
"key": "/user/me"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3055,
"key": 200
}
]
},
"doc_count": 3055,
"key": "/user"
}
]
}
}
}
所以我可以获得我的前 5 个热门 API 以及它们的状态和计数。但我也想获得每个 API 的状态百分比
像这样的东西
API
/search/results 200 : 30(89%) 201: 10(10%) 500:1(1%)
/eligibility 200 : 20(90%) 500 : 3(10%)
任何帮助,将不胜感激。
解决方案
这种计算应该由客户端完成。
例如,在响应的以下片段中:
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3261, <--- divide this
"key": 200
},
{
"doc_count": 254, <---- or this
"key": 400
}
]
},
"doc_count": 3515, <--- by this
"key": "/account/me"
},
使用每个 URL doc_count
,您可以计算如下数字:
API
/account/me 200 : 3261(93%) 400: 254(7%)
推荐阅读
- php - 如何使用 MAX 函数中的值来确定下一个可用的 id?
- r - 将另一个矩阵的内容(列表内容)放入矩阵的 NULL 位置
- google-bigquery - 在 bigquery 中创建一个非常大的表的 id 的成对组合
- javascript - handlerInput.responseBuilder 在我可以从 observable 获取数据之前返回
- firebase - 使用 Cloud Firestore 和 React 的 Todo App 数据结构
- python - 在Python中查找一列的最大值和最小值以及其他两列的差异
- java - Micronaut MySQL 删除记录 org.hibernate.HibernateException: No Session found for current thread
- angular - 使用 Router.navigate() 后未调用 Observable.subscribe()
- javascript - 几个带有 EventListeners 的按钮,一个不工作
- php - 使用正则表达式从文本中获取 1-10 中的 10