Files
bvandeusenandClaude Opus 5.5 5569f9934e
CI / lint (push) Successful in 2s
CI / integration (push) Failing after 1m24s
CI / unit (push) Successful in 1m37s
CI / publish (push) Skipped
feat(anomalies): a metric out of its normal is an anomaly to review (#5740)
#5740 "A metric that departs from its normal is recorded as an anomaly to
review, and is findable after it ends", milestone 513.

- Every minute each learned key's last 15 minutes is judged against its
  hour's normal: above the usual hold x1.25 and by the metric's floor opens
  an occurrence, which ends when the level comes back.
- metric_anomalies holds one row per item to review; a repeat while it
  still waits joins it as an occurrence (metric_anomaly_spans).
- An ended row nobody reviews within anomalies.review_days (14, Settings ->
  Thresholds) lapses; an ongoing one never does.
- Learning skips every hour an occurrence covers unless its anomaly was
  marked normal: no normal is learned without review.
- Alerts -> Anomalies lists those to review first, then the reviewed and
  lapsed under day dividers; marking is operator/admin and audited.
- The Overview widget and get_overview warn of open ones and count those
  to review; the host page shows its own; the weekly report lists the
  week's and the lapsed; MCP list_anomalies and review_anomaly.
- Reviewed and lapsed rows are pruned at data.retention_days, and kept
  in backups like alert history.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 13:41:57 -04:00

56 lines
2.5 KiB
Python

"""Metrics that departed from their normal, to review (#5740).
Revision ID: 0046_metric_anomalies
Revises: 0045_metric_norms
Create Date: 2026-10-14
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = "0046_metric_anomalies"
down_revision: Union[str, None] = "0045_metric_norms"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
"metric_anomalies",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("source_module", sa.String(64), nullable=False),
sa.Column("resource_name", sa.String(255), nullable=False),
sa.Column("metric_name", sa.String(128), nullable=False),
sa.Column("started_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("last_started_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("ended_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("occurrences", sa.Integer, nullable=False),
sa.Column("peak_level", sa.Float, nullable=False),
sa.Column("peak_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("usual_hold", sa.Float, nullable=False),
sa.Column("usual_peak", sa.Float, nullable=False),
sa.Column("review", sa.String(12), nullable=False),
sa.Column("reviewed_by", sa.String(64), nullable=True),
sa.Column("reviewed_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("note", sa.Text, nullable=True),
)
op.create_index("ix_metric_anomalies_key", "metric_anomalies",
["source_module", "resource_name", "metric_name"])
op.create_index("ix_metric_anomalies_review_last", "metric_anomalies",
["review", "last_started_at"])
op.create_table(
"metric_anomaly_spans",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("anomaly_id", sa.String(36),
sa.ForeignKey("metric_anomalies.id", ondelete="CASCADE"), nullable=False),
sa.Column("started_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("ended_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("peak_level", sa.Float, nullable=False),
)
op.create_index("ix_metric_anomaly_spans_anomaly_id", "metric_anomaly_spans", ["anomaly_id"])
def downgrade() -> None:
op.drop_table("metric_anomaly_spans")
op.drop_table("metric_anomalies")