85 lines
2.3 KiB
Python
85 lines
2.3 KiB
Python
"""OpenAI processing stub and condition evaluation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import operator
|
|
from typing import Any, Callable, Optional
|
|
|
|
from monitor.models import PageSnapshot, ProcessingResult, WebsiteConfig
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Supported operators for deterministic condition evaluation (future use)
|
|
OPERATORS: dict[str, Callable[[Any, Any], bool]] = {
|
|
">": operator.gt,
|
|
">=": operator.ge,
|
|
"<": operator.lt,
|
|
"<=": operator.le,
|
|
"==": operator.eq,
|
|
"!=": operator.ne,
|
|
}
|
|
|
|
|
|
def process_with_openai(
|
|
snapshot: PageSnapshot,
|
|
website_config: WebsiteConfig,
|
|
) -> ProcessingResult:
|
|
"""
|
|
Process a page snapshot with OpenAI.
|
|
|
|
STUB: This function is intentionally not implemented yet.
|
|
It will eventually send the snapshot and website instructions to OpenAI
|
|
for page understanding and value extraction.
|
|
|
|
Condition evaluation will remain in Python (see evaluate_condition).
|
|
"""
|
|
logger.info("Processing %s (OpenAI stub — not implemented)", website_config.name)
|
|
|
|
return ProcessingResult(
|
|
success=False,
|
|
value=None,
|
|
condition_met=False,
|
|
message="OpenAI processing not yet implemented",
|
|
email_subject="",
|
|
email_body="",
|
|
reason="stub",
|
|
)
|
|
|
|
|
|
def evaluate_condition(
|
|
value: Any,
|
|
operator_name: str,
|
|
threshold: Any,
|
|
) -> bool:
|
|
"""
|
|
Deterministically evaluate a condition in Python.
|
|
|
|
Example: evaluate_condition(105, ">", 100) -> True
|
|
|
|
This is separated from OpenAI so the LLM only extracts values,
|
|
while Python performs reliable comparisons.
|
|
"""
|
|
op = OPERATORS.get(operator_name)
|
|
if op is None:
|
|
raise ValueError(f"Unsupported operator: {operator_name}")
|
|
return op(value, threshold)
|
|
|
|
|
|
def handle_processing_result(
|
|
result: ProcessingResult,
|
|
notification_service: Any,
|
|
notifications_config: Any,
|
|
) -> None:
|
|
"""Route notifications when a condition is met."""
|
|
if not result.condition_met:
|
|
return
|
|
|
|
message = result.message or "Monitoring condition met."
|
|
if notifications_config.sms.enabled:
|
|
notification_service.send_sms(message)
|
|
|
|
if notifications_config.email.enabled:
|
|
subject = result.email_subject or "Website Monitor Alert"
|
|
body = result.email_body or message
|
|
notification_service.send_email(subject, body)
|