Skip to content

Commit

Permalink
fix(apis/web/metrics): 修复网关资源宽带占用标签显示问题 (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
F-cq authored Oct 31, 2024
1 parent 60ba561 commit a134fc4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/dashboard/apigateway/apigateway/apis/web/metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""},
Expand Down Expand Up @@ -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,
Expand All @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"})),
Expand Down

0 comments on commit a134fc4

Please sign in to comment.