Skip to content

Anomaly detection

AnomalyDetectionBuilder

Helper object to build AnomalyDetection check.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
class AnomalyDetectionBuilder:
    """Helper object to build AnomalyDetection check."""

    def __init__(self) -> None:
        self._strategy: AbstractStrategy | None = None
        self._analyzer: AbstractAnalyzer | None = None
        self._check_level: CheckLevel = CheckLevel.Warning
        self._description: str = ""
        self._with_tag_values: dict[str, str] = dict()
        self._after_date: int | None = None
        self._before_date: int | None = None

    def for_analyzer(self, analyzer: AbstractAnalyzer) -> Self:
        """Add an analyzer."""

        self._analyzer = analyzer
        return self

    def with_strategy(self, strategy: AbstractStrategy) -> Self:
        """Add a strategy."""

        self._strategy = strategy
        return self

    def with_check_level(self, level: CheckLevel) -> Self:
        """Set a severity level."""

        self._check_level = level
        return self

    def with_description(self, description: str) -> Self:
        """Add a description."""

        self._description = description
        return self

    def with_tags(self, tags: dict[str, str]) -> Self:
        """Add tags."""

        self._with_tag_values = tags
        return self

    def after_date(self, dt: int) -> Self:
        """Set a minimal dataset date value

        This value will be used to filter out part of
        the timeseries of metrics.
        """
        self._after_date = dt
        return self

    def before_date(self, dt: int) -> Self:
        """Set a maximal dataset date value

        This value will be used to filter out part of
        the timeseries of metrics.
        """

        self._before_date = dt
        return self

    def _validate(self) -> None:
        if self._analyzer is None:
            raise ValueError("Analyzer is not set")
        if self._strategy is None:
            raise ValueError("Strategy is not set")

    def build(self) -> suite.AnomalyDetection:
        self._validate()

        # for mypy
        assert self._strategy is not None
        assert self._analyzer is not None

        return suite.AnomalyDetection(
            anomaly_detection_strategy=self._strategy._to_proto(),
            analyzer=self._analyzer._to_proto(),
            config=suite.AnomalyDetection.AnomalyCheckConfig(
                level=self._check_level._to_proto(),
                description=self._description,
                with_tag_values=self._with_tag_values,
                after_date=self._after_date,
                before_date=self._before_date,
            ),
        )

after_date(dt)

Set a minimal dataset date value

This value will be used to filter out part of the timeseries of metrics.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
def after_date(self, dt: int) -> Self:
    """Set a minimal dataset date value

    This value will be used to filter out part of
    the timeseries of metrics.
    """
    self._after_date = dt
    return self

before_date(dt)

Set a maximal dataset date value

This value will be used to filter out part of the timeseries of metrics.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
def before_date(self, dt: int) -> Self:
    """Set a maximal dataset date value

    This value will be used to filter out part of
    the timeseries of metrics.
    """

    self._before_date = dt
    return self

for_analyzer(analyzer)

Add an analyzer.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
def for_analyzer(self, analyzer: AbstractAnalyzer) -> Self:
    """Add an analyzer."""

    self._analyzer = analyzer
    return self

with_check_level(level)

Set a severity level.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
def with_check_level(self, level: CheckLevel) -> Self:
    """Set a severity level."""

    self._check_level = level
    return self

with_description(description)

Add a description.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
def with_description(self, description: str) -> Self:
    """Add a description."""

    self._description = description
    return self

with_strategy(strategy)

Add a strategy.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
def with_strategy(self, strategy: AbstractStrategy) -> Self:
    """Add a strategy."""

    self._strategy = strategy
    return self

with_tags(tags)

Add tags.

Source code in tsumugi-python/tsumugi/anomaly_detection.py
def with_tags(self, tags: dict[str, str]) -> Self:
    """Add tags."""

    self._with_tag_values = tags
    return self