|
47 | 47 | BkMonitorDeleteAlarmException, |
48 | 48 | BkMonitorSaveAlarmException, |
49 | 49 | BuiltInNotAllowDeleteException, |
| 50 | + DutyRuleSaveException, |
50 | 51 | ) |
51 | | -from backend.db_monitor.tasks import update_app_policy |
| 52 | +from backend.db_monitor.tasks import delete_monitor_duty_rule, update_app_policy, update_db_notice_group |
52 | 53 | from backend.db_monitor.utils import ( |
53 | 54 | bkm_delete_alarm_strategy, |
54 | 55 | bkm_save_alarm_strategy, |
@@ -167,12 +168,8 @@ def save_monitor_group(self) -> int: |
167 | 168 | resp = BKMonitorV3Api.save_duty_rule(save_duty_rule_params, use_admin=True, raw=True) |
168 | 169 | if resp.get("result"): |
169 | 170 | self.monitor_duty_rule_id = resp["data"]["id"] |
170 | | - monitor_duty_rule_ids = ( |
171 | | - DutyRule.objects.filter(db_type=self.db_type) |
172 | | - .exclude(monitor_duty_rule_id=0) |
173 | | - .order_by("-priority") |
174 | | - .values_list("monitor_duty_rule_id", flat=True) |
175 | | - ) |
| 171 | + duty_rules = DutyRule.get_biz_db_duty_rules(self.bk_biz_id, self.db_type) |
| 172 | + monitor_duty_rule_ids = [rule.monitor_duty_rule_id for rule in duty_rules] |
176 | 173 | save_monitor_group_params["need_duty"] = True |
177 | 174 | save_monitor_group_params["duty_rules"] = list(monitor_duty_rule_ids) + [self.monitor_duty_rule_id] |
178 | 175 | else: |
@@ -275,11 +272,15 @@ class DutyRule(AuditedModel): |
275 | 272 | category = models.CharField(verbose_name=_("轮值类型"), choices=DutyRuleCategory.get_choices(), max_length=LEN_SHORT) |
276 | 273 | db_type = models.CharField(_("数据库类型"), choices=DBType.get_choices(), max_length=LEN_SHORT) |
277 | 274 | duty_arranges = models.JSONField(_("轮值人员设置")) |
| 275 | + biz_config = models.JSONField(_("业务设置(包含业务include/排除业务exclude)"), default=dict) |
278 | 276 |
|
279 | 277 | def save(self, *args, **kwargs): |
280 | 278 | """ |
281 | 279 | 保存轮值 |
282 | 280 | """ |
| 281 | + # 0. (前置校验)不允许同时存在包含业务和排除业务两个设置 |
| 282 | + if self.biz_config.get("include") and self.biz_config.get("exclude"): |
| 283 | + raise DutyRuleSaveException(_("不允许通知存在包含业务和排除业务配置")) |
283 | 284 | # 1. 新建监控轮值 |
284 | 285 | params = { |
285 | 286 | "name": f"{self.db_type}_{self.name}", |
@@ -343,26 +344,43 @@ def save(self, *args, **kwargs): |
343 | 344 | # 3. 判断是否需要变更用户组 |
344 | 345 | # 3.1 非老规则(即新建的规则) |
345 | 346 | need_update_user_group = not is_old_rule |
346 | | - # 3.2 调整了优先级的规则 |
| 347 | + # 3.2 调整了优先级的规则,或者调整了业务配置 |
347 | 348 | if self.pk: |
348 | 349 | old_rule = DutyRule.objects.get(pk=self.pk) |
349 | | - if old_rule.priority != self.priority: |
| 350 | + if old_rule.priority != self.priority or old_rule.biz_config != self.biz_config: |
350 | 351 | need_update_user_group = True |
351 | 352 | # 4. 保存本地轮值规则 |
352 | 353 | super().save(*args, **kwargs) |
353 | | - # 5. 变更告警组 |
| 354 | + # 5. 变更告警组-异步执行 |
354 | 355 | if need_update_user_group: |
355 | | - for notice_group in NoticeGroup.objects.filter(is_built_in=True, db_type=self.db_type): |
356 | | - notice_group.save() |
| 356 | + update_db_notice_group.delay(self.db_type) |
357 | 357 |
|
358 | 358 | def delete(self, using=None, keep_parents=False): |
359 | | - BKMonitorV3Api.delete_duty_rules({"ids": [self.monitor_duty_rule_id], "bk_biz_ids": [env.DBA_APP_BK_BIZ_ID]}) |
| 359 | + """删除轮值""" |
360 | 360 | super().delete() |
| 361 | + delete_monitor_duty_rule.delay(self.db_type, self.monitor_duty_rule_id) |
361 | 362 |
|
362 | 363 | @classmethod |
363 | 364 | def priority_distinct(cls) -> list: |
364 | 365 | return list(cls.objects.values_list("priority", flat=True).distinct().order_by("-priority")) |
365 | 366 |
|
| 367 | + @classmethod |
| 368 | + def get_biz_db_duty_rules(cls, bk_biz_id: int, db_type: str): |
| 369 | + """获取指定业务DB组件的轮值策略""" |
| 370 | + duty_rules = DutyRule.objects.filter(db_type=db_type).exclude(monitor_duty_rule_id=0).order_by("-priority") |
| 371 | + active_biz_duty_rules: list = [] |
| 372 | + |
| 373 | + for rule in duty_rules: |
| 374 | + # 如果业务不在包含名单,或者业务在排除名单,则本策略不属于该业务下 |
| 375 | + if rule.biz_config: |
| 376 | + include, exclude = rule.biz_config.get("include"), rule.biz_config.get("exclude") |
| 377 | + if (include and bk_biz_id not in include) or (exclude and bk_biz_id in exclude): |
| 378 | + continue |
| 379 | + # 添加该业务下的轮值策略 |
| 380 | + active_biz_duty_rules.append(rule) |
| 381 | + |
| 382 | + return active_biz_duty_rules |
| 383 | + |
366 | 384 | class Meta: |
367 | 385 | verbose_name_plural = verbose_name = _("轮值规则(DutyRule)") |
368 | 386 |
|
|
0 commit comments