Skip to content

Commit 11b04a0

Browse files
committed
add internal monitor for high active issues count
1 parent 30a2c18 commit 11b04a0

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""Monitor with high active issues count
2+
Objective: check for Monitors with high active issues count to prevent the application from being
3+
affected from a high resource usage.
4+
"""
5+
6+
import os
7+
from typing import TypedDict, cast
8+
9+
from databases import query_application
10+
from monitor_utils import (
11+
AlertOptions,
12+
AlertPriority,
13+
IssueOptions,
14+
MonitorOptions,
15+
PriorityLevels,
16+
ValueRule,
17+
read_file,
18+
)
19+
from plugins.slack import SlackNotification
20+
21+
TRIGGER_THRESHOLD = 500
22+
23+
monitor_options = MonitorOptions(
24+
update_cron="*/2 * * * *",
25+
search_cron="*/5 * * * *",
26+
)
27+
28+
issue_options = IssueOptions(
29+
model_id_key="monitor_id",
30+
solvable=True,
31+
)
32+
33+
alert_options = AlertOptions(
34+
rule=ValueRule(
35+
value_key="active_issues_count",
36+
operation="greater_than",
37+
priority_levels=PriorityLevels(
38+
moderate=TRIGGER_THRESHOLD,
39+
high=2*TRIGGER_THRESHOLD,
40+
critical=3*TRIGGER_THRESHOLD,
41+
)
42+
)
43+
)
44+
45+
46+
class IssueDataType(TypedDict):
47+
monitor_id: int
48+
monitor_name: str
49+
active_issues_count: int
50+
51+
52+
async def search() -> list[IssueDataType] | None:
53+
sql = read_file("search_query.sql")
54+
55+
notifications_list = cast(
56+
list[IssueDataType],
57+
await query_application(sql, TRIGGER_THRESHOLD)
58+
)
59+
60+
return notifications_list
61+
62+
63+
async def update(issues_data: list[IssueDataType]) -> list[IssueDataType] | None:
64+
sql = read_file("search_query.sql")
65+
66+
notifications_list = cast(
67+
list[IssueDataType],
68+
await query_application(sql, TRIGGER_THRESHOLD)
69+
)
70+
71+
return notifications_list
72+
73+
74+
def is_solved(issue_data: IssueDataType) -> bool:
75+
active_issues_count = issue_data["active_issues_count"]
76+
return active_issues_count < TRIGGER_THRESHOLD / 2
77+
78+
79+
notification_options = [
80+
SlackNotification(
81+
channel=os.environ["SLACK_MAIN_CHANNEL"],
82+
title="Monitor with high active issues count",
83+
issues_fields=["monitor_id", "monitor_name", "active_issues_count"],
84+
mention=os.environ["SLACK_MAIN_MENTION"],
85+
min_priority_to_send=AlertPriority.low,
86+
min_priority_to_mention=AlertPriority.moderate,
87+
)
88+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
with active_issues as (
2+
select
3+
monitor_id,
4+
count(id) as issues_count
5+
from "Issues"
6+
where status = 'active'
7+
group by monitor_id
8+
having count(id) > $1 :: int
9+
)
10+
select
11+
monitors.id as monitor_id,
12+
monitors.name as monitor_name,
13+
active_issues.issues_count as active_issues_count
14+
from active_issues
15+
left join "Monitors" as monitors
16+
on monitors.id = active_issues.monitor_id;

0 commit comments

Comments
 (0)