Skip to content

Commit 82c72dc

Browse files
committed
refactor: 优化 merge_dimensions_into_conditions 方法存放位置
# Reviewed, transaction id: 70955
1 parent 9258659 commit 82c72dc

File tree

4 files changed

+65
-48
lines changed

4 files changed

+65
-48
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Tencent is pleased to support the open source community by making 蓝鲸智云 - 监控平台 (BlueKing - Monitor) available.
3+
Copyright (C) 2017-2025 Tencent. All rights reserved.
4+
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at http://opensource.org/licenses/MIT
6+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
7+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
8+
specific language governing permissions and limitations under the License.
9+
"""
10+
11+
from typing import Any
12+
13+
14+
def merge_dimensions_into_conditions(
15+
agg_condition: list[dict[str, Any]] | None,
16+
dimensions: dict[str, Any],
17+
dimension_fields: list[str],
18+
) -> list[dict[str, Any]]:
19+
"""合并维度信息到初始过滤条件中。
20+
21+
将维度信息合并到初始过滤条件中,用于告警下钻时构建查询过滤条件。
22+
如果维度已存在于初始条件中,则用维度值覆盖;如果维度不存在于初始条件中,则添加为新的过滤条件。
23+
24+
:param agg_condition: 初始过滤条件列表
25+
:param dimensions: 维度信息字典
26+
:param dimension_fields: 有效维度字段列表
27+
:return: 合并后的过滤条件列表
28+
"""
29+
# 使用告警策略配置的汇聚条件作为初始过滤条件
30+
filter_conditions: list[dict[str, Any]] = agg_condition or []
31+
32+
# 构建 key → 索引位置的映射,方便后续更新替换
33+
condition_index: dict[str, int] = {condition["key"]: idx for idx, condition in enumerate(filter_conditions)}
34+
35+
# 遍历告警的原始维度信息,增加维度过滤条件
36+
for key, value in dimensions.items():
37+
if key not in dimension_fields or value is None:
38+
continue
39+
40+
condition: dict[str, Any] = {
41+
"key": key,
42+
"method": "eq",
43+
"value": value if isinstance(value, list) else [value or ""],
44+
"condition": "and",
45+
}
46+
# 如果该维度已存在于汇聚条件中,则更新替换;否则添加为新过滤条件
47+
if key in condition_index:
48+
filter_conditions[condition_index[key]] = condition
49+
else:
50+
filter_conditions.append(condition)
51+
52+
return filter_conditions

bkmonitor/packages/fta_web/alert/utils.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from datetime import datetime, timedelta
1515
from dateutil.relativedelta import relativedelta
16-
from typing import Any
1716

1817

1918
# 生成从 start 到 end 的每日时间段
@@ -236,43 +235,3 @@ def is_include_promql(query_string: str) -> bool:
236235
):
237236
return True
238237
return False
239-
240-
241-
def merge_dimensions_into_conditions(
242-
agg_condition: list[dict[str, Any]] | None,
243-
dimensions: dict[str, Any],
244-
dimension_fields: list[str],
245-
) -> list[dict[str, Any]]:
246-
"""合并维度信息到初始过滤条件中。
247-
248-
将维度信息合并到初始过滤条件中,如果维度已存在于初始条件中,则用维度值覆盖;如果维度不存在于初始条件中,则添加为新的过滤条件。
249-
250-
:param agg_condition: 初始过滤条件列表
251-
:param dimensions: 维度信息字典
252-
:param dimension_fields: 有效维度字段列表
253-
:return: 合并后的过滤条件列表
254-
"""
255-
# 使用告警策略配置的汇聚条件作为初始过滤条件
256-
filter_conditions: list[dict[str, Any]] = agg_condition or []
257-
258-
# 构建 key → 索引位置的映射,方便后续更新替换
259-
condition_index: dict[str, int] = {condition["key"]: idx for idx, condition in enumerate(filter_conditions)}
260-
261-
# 遍历告警的原始维度信息,增加维度过滤条件
262-
for key, value in dimensions.items():
263-
if key not in dimension_fields or value is None:
264-
continue
265-
266-
condition: dict[str, Any] = {
267-
"key": key,
268-
"method": "eq",
269-
"value": value if isinstance(value, list) else [value or ""],
270-
"condition": "and",
271-
}
272-
# 如果该维度已存在于汇聚条件中,则更新替换;否则添加为新过滤条件
273-
if key in condition_index:
274-
filter_conditions[condition_index[key]] = condition
275-
else:
276-
filter_conditions.append(condition)
277-
278-
return filter_conditions

bkmonitor/packages/fta_web/alert_v2/resources.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
from bkmonitor.data_source import q_to_conditions
2020
from bkmonitor.data_source.unify_query.builder import QueryConfigBuilder, UnifyQuerySet
2121
from bkmonitor.documents import AlertDocument
22+
from bkmonitor.utils.alert_drilling import merge_dimensions_into_conditions
2223
from bkmonitor.utils.thread_backend import ThreadPool
2324
from constants.alert import APMTargetType, K8STargetType
2425
from constants.data_source import DataTypeLabel, DataSourceLabel
2526
from core.drf_resource import Resource, resource, api
2627
from fta_web.alert.resources import AlertDetailResource as BaseAlertDetailResource
27-
from fta_web.alert.utils import merge_dimensions_into_conditions
2828
from fta_web.alert_v2.target import BaseTarget, get_target_instance
2929
from monitor_web.data_explorer.event.constants import EventSource
3030
from monitor_web.data_explorer.event.resources import (
@@ -188,18 +188,24 @@ def build_generic_query(
188188
:param alert: 告警文档对象
189189
:param target: 目标对象
190190
:param q: 查询构建器
191-
:return: 构建好的查询配置,如果不是自定义事件告警则返回 None
191+
:return: 构建好的查询配置,如果不是支持的告警类型则返回 None
192192
"""
193193
# 获取告警策略数据查询配置
194194
try:
195195
query_config: dict[str, Any] = alert.strategy["items"][0]["query_configs"][0]
196196
except (KeyError, IndexError, TypeError):
197197
return None
198198

199-
if not (
200-
query_config.get("data_source_label") == DataSourceLabel.CUSTOM
201-
and query_config.get("data_type_label") == DataTypeLabel.EVENT
202-
):
199+
# 事件类告警包括:自定义事件、日志关键字事件
200+
data_source: tuple[str, str] = (
201+
query_config.get("data_source_label", ""),
202+
query_config.get("data_type_label", ""),
203+
)
204+
supported_sources: list[tuple[str, str]] = [
205+
(DataSourceLabel.CUSTOM, DataTypeLabel.EVENT),
206+
(DataSourceLabel.BK_MONITOR_COLLECTOR, DataTypeLabel.LOG),
207+
]
208+
if data_source not in supported_sources:
203209
return None
204210

205211
result_table_id: str = query_config.get("result_table_id") or ""

bkmonitor/packages/monitor_adapter/home/alert_redirect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
from bkmonitor.documents import ActionInstanceDocument, AlertDocument
1919
from bkmonitor.models import ActionInstance
2020
from bkmonitor.utils import time_tools
21+
from bkmonitor.utils.alert_drilling import merge_dimensions_into_conditions
2122
from constants.apm import ApmAlertHelper, FIVE_MIN_SECONDS
2223
from constants.data_source import DataSourceLabel, DataTypeLabel
2324
from core.drf_resource import resource
24-
from fta_web.alert.utils import merge_dimensions_into_conditions
2525

2626
"""
2727
提供告警详情页面跳转

0 commit comments

Comments
 (0)