diff --git a/src/dashboard/apigateway/apigateway/apis/web/metrics/views.py b/src/dashboard/apigateway/apigateway/apis/web/metrics/views.py index 077b507da..ce265d419 100644 --- a/src/dashboard/apigateway/apigateway/apis/web/metrics/views.py +++ b/src/dashboard/apigateway/apigateway/apis/web/metrics/views.py @@ -31,6 +31,20 @@ class QueryRangeApi(generics.ListAPIView): + + @staticmethod + def get_series_resource_id_index_map(series): + ids_data = {} + + for index in range(len(series)): + try: + id_ = int(series[index]["target"].split('"')[1].split(".")[2]) + ids_data[id_] = index + except Exception: + pass + + return ids_data + @swagger_auto_schema( query_serializer=MetricsQueryRangeInputSLZ(), responses={status.HTTP_200_OK: ""}, @@ -62,7 +76,8 @@ def get(self, request, *args, **kwargs): time_start, time_end = smart_time_range.get_head_and_tail() step = smart_time_range.get_interval() - metrics = MetricsRangeFactory.create_metrics(MetricsRangeEnum(data["metrics"])) + metrics_name = data["metrics"] + metrics = MetricsRangeFactory.create_metrics(MetricsRangeEnum(metrics_name)) data = metrics.query_range( gateway_name=request.gateway.name, @@ -74,6 +89,16 @@ def get(self, request, *args, **kwargs): end=time_end, step=step, ) + + if metrics_name in ["ingress", "egress"]: + series = data.get("data", {}).get("series", []) + ids_data = self.get_series_resource_id_index_map(series) + + if ids_data: + resources = Resource.objects.filter(id__in=ids_data.keys()).values("id", "name") + for obj in resources: + series[ids_data[obj["id"]]]["target"] = obj["name"] + return OKJsonResponse(data=data) diff --git a/src/dashboard/apigateway/apigateway/tests/apis/web/metrics/test_views.py b/src/dashboard/apigateway/apigateway/tests/apis/web/metrics/test_views.py index da74705c0..cc051cc59 100644 --- a/src/dashboard/apigateway/apigateway/tests/apis/web/metrics/test_views.py +++ b/src/dashboard/apigateway/apigateway/tests/apis/web/metrics/test_views.py @@ -16,10 +16,73 @@ # We undertake not to change the open source license (MIT license) applicable # to the current version of the project delivered to anyone in the future. # +from ddf import G + +from apigateway.core.models import Resource class TestQueryRangeApi: def test_get(self, mocker, fake_stage, request_view): + + resource_obj = G( + Resource, + name="testname001", + gateway=fake_stage.gateway + ) + + data = { + "data": { + "metrics": [], + "series": [ + { + "alias": "_result_", + "metric_field": "_result_", + "unit": "", + "target": "route=\"bk-esb.prod.{}\"".format(resource_obj.id), + "dimensions": { + "route": "bk-esb.prod.2152" + }, + "datapoints": [ + ] + }, + { + "alias": "_result_", + "metric_field": "_result_", + "unit": "", + "target": "route=\"bk-esb.prod.1234\"", + "dimensions": { + "route": "bk-esb.prod.1234" + }, + "datapoints": [ + ] + } + ] + } + } + + mocker.patch( + "apigateway.apis.web.metrics.views.MetricsRangeFactory.create_metrics", + return_value=mocker.Mock(query_range=mocker.Mock(return_value=data)), + ) + + response = request_view( + "GET", + "metrics.query_range", + path_params={ + "gateway_id": fake_stage.gateway.id, + }, + data={ + "stage_id": fake_stage.id, + "metrics": "ingress", + "time_range": 300, + }, + ) + result = response.json() + + assert response.status_code == 200 + assert result["data"]["data"]["series"][0]["target"] == "testname001" + assert result["data"]["data"]["series"][1]["target"] == "route=\"bk-esb.prod.1234\"" + mocker.patch( "apigateway.apis.web.metrics.views.MetricsRangeFactory.create_metrics", return_value=mocker.Mock(query_range=mocker.Mock(return_value={"foo": "bar"})),