Skip to content

Commit b2e5dd4

Browse files
authored
Merge pull request #100 from GabrielSalla/monitor-template
Create monitor template
2 parents 989bc09 + 6dced31 commit b2e5dd4

File tree

8 files changed

+104
-43
lines changed

8 files changed

+104
-43
lines changed

docs/monitor.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Creating a new monitor
2-
This guide will walk through the steps to set up a new Monitor.
2+
This guide will walk through the steps to set up a new Monitor. The file [monitor_template.py](../resources/monitor_template.py) serves as a template for creating a new monitor. It includes all the necessary imports and settings to get started.
33

44
As a demonstration, the Monitor that will be designed is intended to **search for users with invalid registration data**, specifically when their name is empty.
55

@@ -75,6 +75,13 @@ from monitor_utils import (
7575
)
7676
```
7777

78+
# The issue data type
79+
The `IssueDataType` class defines the structure of the data that represents an issue. It serves as a type annotation in the monitor's default functions, making development more intuitive and error-resistant while also providing a clear contract for the issue data structure.
80+
81+
Define a class called `IssueDataType`, that inherits from `TypedDict`, and includes all the fields that will be present in the issue data for the monitor.
82+
83+
**Attention**: The `IssueDataType` class must contain the field specified in the `model_id_key` parameter of the `IssueOptions` setting. This ensures that the issue’s unique identifier is consistently used across your monitor’s configuration. This setting will be presented in the [**Issue options**](#issue-options) section.
84+
7885
# The settings
7986
To set up the monitor’s behavior, issues, and alerts, use the provided Options dataclasses. These settings define how the monitor will manage issues and alerts.
8087

@@ -166,13 +173,6 @@ Priority levels definition. For the defined rule, what value should trigger each
166173

167174
All priority levels defaults to `None`. If a level is set to `None`, it will not be triggered.
168175

169-
# The issue data type
170-
The `IssueDataType` class defines the structure of the data that represents an issue. It serves as a type annotation in the monitor's default functions, making development more intuitive and error-resistant.
171-
172-
Define a class called `IssueDataType`, that inherits from `TypedDict`, and includes all the fields that will be present in the issue data for the monitor.
173-
174-
**Attention**: The `IssueDataType` class must contain the field specified in the `model_id_key` parameter of the `IssueOptions` setting. This ensures that the issue’s unique identifier is consistently used across your monitor’s configuration.
175-
176176
### Example
177177
For the user registration monitor, the issue data type should include the `id` and `name` fields, as these are the essential fields for identifying and tracking the issue.
178178

internal_monitors/monitor_high_active_issues_count/monitor_high_active_issues_count.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
TRIGGER_THRESHOLD = 500
2020

21+
22+
class IssueDataType(TypedDict):
23+
monitor_id: int
24+
monitor_name: str
25+
active_issues_count: int
26+
27+
2128
monitor_options = MonitorOptions(
2229
update_cron="*/2 * * * *",
2330
search_cron="*/5 * * * *",
@@ -41,12 +48,6 @@
4148
)
4249

4350

44-
class IssueDataType(TypedDict):
45-
monitor_id: int
46-
monitor_name: str
47-
active_issues_count: int
48-
49-
5051
async def search() -> list[IssueDataType] | None:
5152
sql = read_file("search_query.sql")
5253

resources/monitor_template.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Monitor template
3+
Read the documentation to learn how to configure each field.
4+
"""
5+
6+
from typing import TypedDict
7+
8+
from monitor_utils import AlertOptions, CountRule, IssueOptions, MonitorOptions, PriorityLevels
9+
10+
11+
# Define the data structure of the issues
12+
class IssueDataType(TypedDict):
13+
pass
14+
15+
16+
monitor_options = MonitorOptions(
17+
search_cron="*/15 * * * *",
18+
update_cron="*/5 * * * *",
19+
)
20+
21+
# Define the behavior expected for the issues
22+
issue_options = IssueOptions(
23+
model_id_key="id",
24+
solvable=True,
25+
)
26+
27+
# Define the alert triggering options
28+
alert_options = AlertOptions(
29+
rule=CountRule(
30+
priority_levels=PriorityLevels(
31+
low=0,
32+
moderate=1,
33+
high=2,
34+
critical=3,
35+
)
36+
)
37+
)
38+
39+
40+
async def search() -> list[IssueDataType] | None:
41+
"""Logic to search for the issues"""
42+
pass
43+
44+
45+
async def update(issues_data: list[IssueDataType]) -> list[IssueDataType] | None:
46+
"""Logic to update the issues data"""
47+
pass
48+
49+
50+
def is_solved(issue_data: IssueDataType) -> bool:
51+
"""Logic to determine if the issue is solved, based on its data"""
52+
return True
53+
54+
55+
# Notification configurations
56+
notification_options = []

sample_monitors/test_monitor/test_monitor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
from monitor_utils import AlertOptions, CountRule, IssueOptions, MonitorOptions, PriorityLevels
55
from notifications.internal_monitor_notification import internal_monitor_notification
66

7+
8+
class IssueDataType(TypedDict):
9+
id: int
10+
value: int
11+
12+
713
monitor_options = MonitorOptions(
814
search_cron="* * * * *",
915
update_cron="* * * * *",
@@ -26,11 +32,6 @@
2632
)
2733

2834

29-
class IssueDataType(TypedDict):
30-
id: int
31-
value: int
32-
33-
3435
async def search() -> list[IssueDataType] | None:
3536
# Return 5 issues data with a random 'id' and a random 'value' between 1
3637
# and 10

tests/sample_monitor_code.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
from monitor_utils import IssueOptions, MonitorOptions
44

5+
6+
class IssueDataType(TypedDict):
7+
id: int
8+
a: str
9+
b: int
10+
11+
512
monitor_options = MonitorOptions(
613
search_cron="* * * * *",
714
update_cron="* * * * *",
@@ -12,12 +19,6 @@
1219
)
1320

1421

15-
class IssueDataType(TypedDict):
16-
id: int
17-
a: str
18-
b: int
19-
20-
2122
async def search() -> list[IssueDataType] | None: ...
2223
async def update(issues_data: list[IssueDataType]) -> list[IssueDataType] | None: ...
2324
def is_solved(issue_data: IssueDataType) -> bool: ...

tests/sample_monitors/internal/monitor_2/monitor_2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
configs.application_queue
99

1010

11+
class IssueDataType(TypedDict):
12+
id: str
13+
a: str
14+
b: int
15+
16+
1117
monitor_options = MonitorOptions(
1218
search_cron="* * * * *",
1319
update_cron="* * * * *",
@@ -18,12 +24,6 @@
1824
)
1925

2026

21-
class IssueDataType(TypedDict):
22-
id: str
23-
a: str
24-
b: int
25-
26-
2727
async def search() -> list[IssueDataType] | None: ...
2828

2929

tests/sample_monitors/internal/monitor_3/monitor_3.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
from monitor_utils import IssueOptions, MonitorOptions
55

6+
7+
class IssueDataType(TypedDict):
8+
id: str
9+
a: str
10+
b: int
11+
12+
613
monitor_options = MonitorOptions(
714
search_cron="* * * * *",
815
update_cron="* * * * *",
@@ -13,12 +20,6 @@
1320
)
1421

1522

16-
class IssueDataType(TypedDict):
17-
id: str
18-
a: str
19-
b: int
20-
21-
2223
async def search() -> list[IssueDataType] | None: ...
2324

2425

tests/sample_monitors/others/monitor_1/monitor_1.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
from monitor_utils import IssueOptions, MonitorOptions
55

6+
7+
class IssueDataType(TypedDict):
8+
id: str
9+
a: str
10+
b: int
11+
12+
613
monitor_options = MonitorOptions(
714
search_cron="* * * * *",
815
update_cron="* * * * *",
@@ -13,12 +20,6 @@
1320
)
1421

1522

16-
class IssueDataType(TypedDict):
17-
id: str
18-
a: str
19-
b: int
20-
21-
2223
async def search() -> list[IssueDataType] | None: ...
2324

2425

0 commit comments

Comments
 (0)