- Updated argument parsing in manage_api.py to include new threshold parameters. - Enhanced _config_payload to include thresholds and webhook configurations. - Modified _build_summary to track queue metrics and adjust alert reporting. - Refactored DwellEngine to utilize queue thresholds for alerting and reporting. - Added queue metrics calculations and status change tracking in dwell_engine.py. - Updated notifier.py to support posting JSON events to webhooks. - Adjusted example configuration to reflect new threshold parameters. - Enhanced Docker entrypoint script for better process management. - Updated tests to cover new queue metrics and thresholds. - Improved ManagedServiceDetail and ManagedServices Vue components to display queue metrics.
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from urllib import request
|
|
|
|
|
|
def build_json_request(
|
|
url: str, payload: dict, timeout_seconds: float = 5.0
|
|
) -> request.Request:
|
|
data = json.dumps(payload).encode("utf-8")
|
|
req = request.Request(url=url, data=data, method="POST")
|
|
req.add_header("Content-Type", "application/json")
|
|
req.timeout_seconds = timeout_seconds
|
|
return req
|
|
|
|
|
|
def append_json_event(path: str | Path, payload: dict) -> None:
|
|
output_path = Path(path)
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
with output_path.open("a", encoding="utf-8") as handle:
|
|
handle.write(json.dumps(payload, ensure_ascii=False) + "\n")
|
|
|
|
|
|
def post_json_event(url: str, payload: dict, timeout_seconds: float = 5.0) -> None:
|
|
if not url.strip():
|
|
return
|
|
req = build_json_request(url, payload, timeout_seconds=timeout_seconds)
|
|
with request.urlopen(req, timeout=timeout_seconds):
|
|
return
|
|
|
|
|
|
def dispatch_json_event(
|
|
path: str | Path,
|
|
payload: dict,
|
|
webhook_url: str = "",
|
|
timeout_seconds: float = 5.0,
|
|
) -> None:
|
|
append_json_event(path, payload)
|
|
if webhook_url.strip():
|
|
post_json_event(webhook_url, payload, timeout_seconds=timeout_seconds)
|