Skip to content

Commit d315382

Browse files
authored
Merge pull request #118 from GabrielSalla/create-internal-monitor-consecutive-fails
Create internal monitor for monitor consecutive errors
2 parents 46065f5 + 9e8a515 commit d315382

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Monitor with high consecutive fails
2+
Objective: check for Monitors with high consecutive fails.
3+
"""
4+
5+
from typing import TypedDict, cast
6+
7+
from databases import query_application
8+
from monitor_utils import (
9+
AlertOptions,
10+
IssueOptions,
11+
MonitorOptions,
12+
PriorityLevels,
13+
ValueRule,
14+
read_file,
15+
)
16+
from notifications.internal_monitor_notification import internal_monitor_notification
17+
18+
19+
class IssueDataType(TypedDict):
20+
monitor_id: int
21+
monitor_name: str
22+
monitor_enabled: bool
23+
failed_count: int
24+
25+
26+
monitor_options = MonitorOptions(
27+
update_cron="*/2 * * * *",
28+
search_cron="*/5 * * * *",
29+
)
30+
31+
issue_options = IssueOptions(
32+
model_id_key="monitor_id",
33+
solvable=True,
34+
)
35+
36+
alert_options = AlertOptions(
37+
rule=ValueRule(
38+
value_key="failed_count",
39+
operation="greater_than",
40+
priority_levels=PriorityLevels(
41+
moderate=3,
42+
high=5,
43+
critical=10,
44+
),
45+
)
46+
)
47+
48+
49+
async def search() -> list[IssueDataType] | None:
50+
sql = read_file("search_query.sql")
51+
52+
return cast(list[IssueDataType], await query_application(sql))
53+
54+
55+
async def update(issues_data: list[IssueDataType]) -> list[IssueDataType] | None:
56+
sql = read_file("update_query.sql")
57+
monitors_ids = [issue_data["monitor_id"] for issue_data in issues_data]
58+
59+
return cast(list[IssueDataType], await query_application(sql, monitors_ids))
60+
61+
62+
def is_solved(issue_data: IssueDataType) -> bool:
63+
monitor_enabled = issue_data["monitor_enabled"]
64+
failed_count = issue_data["failed_count"]
65+
return not monitor_enabled or failed_count == 0
66+
67+
68+
notification_options = internal_monitor_notification(
69+
name="Monitor with high consecutive fails",
70+
issues_fields=["monitor_id", "monitor_name", "failed_count"],
71+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
select
2+
monitors.id as monitor_id,
3+
monitors.name as monitor_name,
4+
monitors.enabled as monitor_enabled,
5+
coalesce(failed_count, 0) as failed_count
6+
from "Monitors" as monitors
7+
left join lateral (
8+
select count(id) as failed_count
9+
from "MonitorExecutions" as monitor_executions
10+
where
11+
monitor_executions.monitor_id = monitors.id and
12+
monitor_executions.started_at > coalesce(monitors.last_successful_execution, to_timestamp(0))
13+
) as monitor_executions
14+
on true
15+
where
16+
monitors.enabled and
17+
failed_count >= 3;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
select
2+
monitors.id as monitor_id,
3+
monitors.name as monitor_name,
4+
monitors.enabled as monitor_enabled,
5+
coalesce(failed_count, 0) as failed_count
6+
from "Monitors" as monitors
7+
left join lateral (
8+
select count(id) as failed_count
9+
from "MonitorExecutions" as monitor_executions
10+
where
11+
monitor_executions.monitor_id = monitors.id and
12+
monitor_executions.started_at > coalesce(monitors.last_successful_execution, to_timestamp(0))
13+
) as monitor_executions
14+
on true
15+
where monitors.id = any($1 :: int[]);

0 commit comments

Comments
 (0)