35 lines
1,000 B
Python
35 lines
1,000 B
Python
import re
|
|
from typing import Any, Dict
|
|
|
|
|
|
class AlertMatcher:
|
|
def __init__(self, label: str, value: str) -> None:
|
|
self.label = label
|
|
self.value = value
|
|
|
|
def match(self, labels: Dict[str, str]) -> bool:
|
|
return self.label in labels and self.value == labels[self.label]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.label}={self.value}"
|
|
|
|
def __repr__(self) -> str:
|
|
return f"AlertMatcher({self})"
|
|
|
|
def __eq__(self, matcher: Any) -> bool:
|
|
return str(self) == str(matcher)
|
|
|
|
|
|
class AlertRegexMatcher(AlertMatcher):
|
|
def __init__(self, label: str, regex: str) -> None:
|
|
super().__init__(label, regex)
|
|
self.regex = re.compile(regex)
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.label}=~{self.value}"
|
|
|
|
def __repr__(self) -> str:
|
|
return f"AlertRegexMatcher({self})"
|
|
|
|
def match(self, labels: Dict[str, str]) -> bool:
|
|
return self.label in labels and self.regex.match(labels[self.label]) is not None
|