有几个采样超阈值
按模型,采样 [2,5,6,7] 里 count_over(samples, 4) 交回几?
贯穿本节的运维模型(判题机纯 Python、无网,这是它的确定模型,见 spine.py):服务上线后靠日志/指标/健康/告警盯住它。
告警阈值:over(值,阈值)、count_over 超阈值几个、sustained(采样,阈值,n)=最近 n 个都超(持续才报、避免抖动)、should_clear=最新回落可消警。
def over(value, threshold):
"""这个值有没有超过阈值。"""
return value > threshold
def count_over(samples, threshold):
"""一串采样里有几个超过阈值。"""
return sum(1 for s in samples if s > threshold)
def sustained(samples, threshold, n):
"""最近 n 个采样是不是全超阈值(持续超过才告警,避免抖动)。"""
return len(samples) >= n and all(s > threshold for s in samples[-n:])
def should_clear(samples, threshold):
"""最新采样已回落到阈值内,可以消警了吗。"""
return samples[-1] <= threshold
print(count_over([2, 5, 6, 7], 4))
全部评论