import re from typing import Any, Dict class AbstractAlertMatcher: def __init__(self, label: str, value: str, op: str) -> None: self.label = label self.value = value self._op = op def match(self, labels: Dict[str, str]) -> bool: raise NotImplementedError def __str__(self) -> str: return f"{self.label}{self._op}{self.value}" def __eq__(self, matcher: Any) -> bool: return self.label == matcher.label and self.value == matcher.value class AlertMatcher(AbstractAlertMatcher): def __init__(self, label: str, value: str) -> None: super().__init__(label, value, "=") def match(self, labels: Dict[str, str]) -> bool: return self.label in labels and self.value == labels[self.label] class AlertRegexMatcher(AbstractAlertMatcher): def __init__(self, label: str, regex: str) -> None: super().__init__(label, regex, "=~") self.regex = re.compile(regex) def match(self, labels: Dict[str, str]) -> bool: return self.label in labels and self.regex.match(labels[self.label]) is not None