Functional Weave
Code in Python

monitor.status-page@1.0.0

impl/python.py

1,419 bytes · the Python implementation · view raw

from typing import List, Sequence, Set

from .monitor_status_page_types import Component, StatusSummary

# Worst last. Maintenance ranks below every real problem: an outage elsewhere
# during planned work is still the headline.
_RANK = ["operational", "maintenance", "degraded", "partial-outage", "major-outage"]
_HEADLINE = {
    "operational": "All systems operational",
    "maintenance": "Scheduled maintenance",
    "degraded": "Degraded performance",
    "partial-outage": "Partial outage",
    "major-outage": "Major outage",
}


def status_page(components: Sequence[Component]) -> StatusSummary:
    """The banner for a whole status page: the worst component state wins."""
    if len(components) == 0:
        raise ValueError("components must not be empty")
    seen: Set[str] = set()
    worst = 0
    affected: List[str] = []
    for c in components:
        if c.name == "":
            raise ValueError("component name must not be empty")
        if c.name in seen:
            raise ValueError("duplicate component name: %s" % (c.name,))
        seen.add(c.name)
        if c.state not in _RANK:
            raise ValueError("unknown component state: %s" % (c.state,))
        rank = _RANK.index(c.state)
        worst = max(worst, rank)
        if rank > 0:
            affected.append(c.name)
    state = _RANK[worst]
    return StatusSummary(state=state, headline=_HEADLINE[state], affected=affected)