|
| 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 | +] |
0 commit comments