"""canary_ramp.py: your turn, a PROGRESSIVE canary that limits the blast radius.

The solution's canary is all-or-nothing: score once, then 100% or 0%. Real systems ramp a new build
up gradually, 1% of traffic, then 10%, then 50%, then 100%, watching the error rate at each step and
rolling back the instant any step breaches the budget. That way a bad deploy only ever hurts a small
slice of users before it is pulled.

Steps:
  1. Finish `progressive_canary` below: walk the stages, and at each stage roll back if that stage's
     measured error rate exceeds `budget`. If every stage stays within budget, the build is promoted.
  2. Run:  python canary_ramp.py

The worked single-shot canary (canary, release, rollback) is in ../solution/release_ops.py.
"""

STAGES = [1, 10, 50, 100]      # percent of traffic the candidate gets at each step


def progressive_canary(stage_error_rates, budget=0.05, stages=STAGES):
    """stage_error_rates: measured error rate at each stage (same length as `stages`).

    Return (outcome, reached_stage):
      - if a stage's error rate > budget, stop and return ("rolled_back", that stage's percent)
      - if all stages stay within budget, return ("promoted", 100)
    """
    # TODO: for i, pct in enumerate(stages):
    # TODO:     if stage_error_rates[i] > budget: return ("rolled_back", pct)
    # TODO: return ("promoted", 100)
    raise NotImplementedError("implement the progressive canary ramp")


if __name__ == "__main__":
    print("healthy ramp:", progressive_canary([0.00, 0.01, 0.02, 0.01]))   # -> ('promoted', 100)
    print("bad at 10%:  ", progressive_canary([0.00, 0.20, 0.00, 0.00]))   # -> ('rolled_back', 10)
    # Goal: a breach at any stage rolls back at that stage; otherwise it reaches 100%.
