【问题标题】:Elasticsearch query to calculate percentage of http statuses of API用于计算 API 的 http 状态百分比的 Elasticsearch 查询
【发布时间】:2021-10-14 13:57:54
【问题描述】:

我需要计算一个 API 的每个状态的百分比。

参考线程:Elasticsearch query to count number of hits for each 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%)

任何帮助将不胜感激。

【问题讨论】:

  • 你应该在这里发布完整的问题
  • @tomr 抱歉,如果我的问题含糊不清。我已经发布了我的完整问题。任何关于此的想法都会有很大帮助。
  • 太棒了。您还可以发布完整的输出,而不仅仅是 aggregations 位吗?
  • 一旦我有一个完整的例子可供参考,我将作为答案发布,但 TL;DR 版本是这样的:在客户端进行计算。因此,了解有关如何连接到 elasticsearch 的更多信息可能会有所帮助。

标签: elasticsearch kibana elasticsearch-aggregation


【解决方案1】:

这种计算应该由客户端来完成。

例如,在以下来自响应的 sn-p 中:

            {
                "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%)

【讨论】:

    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多