Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: alert labels may be None #1705

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_submit_info(self, begin_time: str, end_time: str) -> List[dict]:
_id = quote(self.project.path_with_namespace, safe="")
_url = f"api/v3/projects/{_id}/tloc/daily/count"
# 指定你的当前时区,默认是 0 时区,范围 (-11,11)
time_zone_num = int(timezone.localtime().tzinfo._utcoffset.seconds / 3600)
time_zone_num = int(timezone.localtime().tzinfo.utcoffset(timezone.now()).seconds / 3600)

params: Dict[str, Union[str, int]] = dict(begin_date=begin_time, end_date=end_time, timezone=time_zone_num)
resp = self._session.get(urljoin(self._api_url, _url), params=params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ def get_count(self, obj):
return len(obj.get("alerts") or [])

def get_slow_query_count(self, obj):
return sum(1 for alert in (obj.get("alerts") or []) if "gcs_mysql_slow_query" in alert.get("labels", []))
return sum(1 for alert in (obj.get("alerts") or []) if "gcs_mysql_slow_query" in (alert.get("labels") or []))

def get_alerts(self, obj):
alerts = obj.get("alerts") or []
# 慢查询告警排在前面,即 labels 中包含 gcs_mysql_slow_query 的排在前面
sorted_alerts = sorted(alerts, key=lambda alert: "gcs_mysql_slow_query" not in alert.get("labels", []))
sorted_alerts = sorted(alerts, key=lambda alert: "gcs_mysql_slow_query" not in (alert.get("labels") or []))
return AlertSLZ(sorted_alerts, many=True).data


Expand Down