Skip to content

Commit

Permalink
feat: 策略支持后台自定义检测等待时间 --story=121839378 (#4923)
Browse files Browse the repository at this point in the history
  • Loading branch information
dengyh authored Feb 7, 2025
1 parent 6c90350 commit bc95743
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions bkmonitor/alarm_backends/core/control/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, item_config, strategy):

self.expression = item_config.get("expression")
self.functions = item_config.get("functions")
self.time_delay = item_config.get("time_delay")
self.metric_ids = set()
self.data_source_types = set()
self.data_source_labels = set()
Expand Down
4 changes: 3 additions & 1 deletion bkmonitor/alarm_backends/service/access/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ def get_query_time_range(self, now_timestamp: int):
until_timestamp = 0

time_delay = settings.ACCESS_DATA_TIME_DELAY
if (DataSourceLabel.BK_LOG_SEARCH, DataTypeLabel.LOG) in first_item.data_source_types:
if first_item.time_delay:
time_delay += first_item.time_delay
elif (DataSourceLabel.BK_LOG_SEARCH, DataTypeLabel.LOG) in first_item.data_source_types:
time_delay += 60
elif first_item.use_aiops_sdk:
# bkbase智能检测flow默认有1分钟的计算等待延迟, SDK智能检测保持相同的逻辑
Expand Down
17 changes: 17 additions & 0 deletions bkmonitor/bkmonitor/migrations/0175_itemmodel_add_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.25 on 2025-02-07 08:39

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('bkmonitor', '0174_auto_20250205_1736'),
]

operations = [
migrations.AddField(
model_name='itemmodel',
name='time_delay',
field=models.IntegerField(default=0, verbose_name='策略等待时间'),
),
]
1 change: 1 addition & 0 deletions bkmonitor/bkmonitor/models/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ItemModel(Model):
target = models.JSONField("监控目标", default=default_target)
meta = models.JSONField("查询配置元数据", default=list)
metric_type = models.CharField("指标类型", max_length=32, default="", blank=True)
time_delay = models.IntegerField("策略等待时间", default=0)

class Meta:
verbose_name = "监控项配置V2"
Expand Down
7 changes: 7 additions & 0 deletions bkmonitor/bkmonitor/strategy/new_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,8 @@ class FunctionParamsSerializer(serializers.Serializer):
query_configs = serializers.ListField(allow_empty=False)
algorithms = Algorithm.Serializer(many=True)
metric_type = serializers.CharField(allow_blank=True, default="")
# 目前只允许后台修改
# time_delay = serializers.IntegerField(default=0)

def __init__(
self,
Expand All @@ -1432,6 +1434,7 @@ def __init__(
algorithms: List[Dict] = None,
metric_type: str = "",
instance: ItemModel = None,
time_delay: int = None,
**kwargs,
):
self.functions = functions or []
Expand All @@ -1445,6 +1448,7 @@ def __init__(
self.strategy_id = strategy_id
self.id = id
self.instance = instance
self.time_delay = time_delay

if metric_type:
self.metric_type = metric_type
Expand Down Expand Up @@ -1497,6 +1501,7 @@ def to_dict(self):
"query_configs": [query_config.to_dict() for query_config in self.query_configs],
"algorithms": [algorithm.to_dict() for algorithm in self.algorithms],
"metric_type": metric_type,
"time_delay": self.time_delay,
}

def to_unify_query_config(self):
Expand Down Expand Up @@ -1591,6 +1596,7 @@ def save(self):
item.functions = self.functions
item.origin_sql = self.origin_sql
item.metric_type = self.metric_type
item.time_delay = self.time_delay if self.time_delay else item.time_delay
item.save()
else:
item = self._create()
Expand Down Expand Up @@ -1626,6 +1632,7 @@ def from_models(
target=item.target,
metric_type=item.metric_type,
instance=item,
time_delay=item.time_delay,
)
record.algorithms = Algorithm.from_models(algorithms[item.id])
record.query_configs = QueryConfig.from_models(query_configs[item.id])
Expand Down

0 comments on commit bc95743

Please sign in to comment.