From c7851e917fcd1a09aa09dc2b9a0581ff98ab2276 Mon Sep 17 00:00:00 2001 From: Ning Yao Date: Sat, 29 Feb 2020 01:43:08 +0800 Subject: [PATCH] Fix response format 'total' for v2 dataframes API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on [1], the response for v2 dataframes is {"total": 3}. However, for Elasticsearch search response [2], the "hits.total" in the response body is {"value": 3, "relation": "eq"}, which does not match the API response schema. [1]: https://docs.openstack.org/cloudkitty/latest/api-reference/v2/index.html?expanded=get-dataframes-from-the-storage-backend-detail#response-example [2]: https://www.elastic.co/guide/en/elasticsearch/reference/8.3/search-your-data.html Story: 2010219 Task: 45967 Change-Id: Ie2c8fd1b146138efc085d7c844afd27b7e10f3f3 Co-Authored-By: Rafael Weingärtner Signed-off-by: Ning Yao (cherry picked from commit 3ba19ed1c7d821b373f1d65e2086c11c8e13a38a) --- cloudkitty/storage/v2/elasticsearch/client.py | 9 ++++++++- ...sponse-total-for-elastic-search-a3a9244380ed046f.yaml | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/fix-response-total-for-elastic-search-a3a9244380ed046f.yaml diff --git a/cloudkitty/storage/v2/elasticsearch/client.py b/cloudkitty/storage/v2/elasticsearch/client.py index 79651b8c..eca5f1ee 100644 --- a/cloudkitty/storage/v2/elasticsearch/client.py +++ b/cloudkitty/storage/v2/elasticsearch/client.py @@ -308,7 +308,14 @@ def retrieve(self, begin, end, filters, metric_types, scroll_id = resp['_scroll_id'] self._scroll_ids.add(scroll_id) - total = resp['hits']['total'] + total_hits = resp['hits']['total'] + + if isinstance(total_hits, dict): + LOG.debug("Total hits [%s] is a dict. Therefore, we only extract " + "the 'value' attribute as the total option.", total_hits) + total_hits = total_hits.get("value") + + total = total_hits chunk = resp['hits']['hits'] output = chunk[offset:offset+limit if paginate else len(chunk)] diff --git a/releasenotes/notes/fix-response-total-for-elastic-search-a3a9244380ed046f.yaml b/releasenotes/notes/fix-response-total-for-elastic-search-a3a9244380ed046f.yaml new file mode 100644 index 00000000..e67fc22f --- /dev/null +++ b/releasenotes/notes/fix-response-total-for-elastic-search-a3a9244380ed046f.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fix response format ``total`` for v2 dataframes API. The response for v2 + dataframes is ``{"total": 3}``. However, for Elasticsearch search response, + the ``"hits.total"`` in the response body is ``{"value": 3, "relation": + "eq"}``, which does not match the API response schema.