MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / MovingAverage

Class MovingAverage

pymongo/read_preferences.py:614–637  ·  view source on GitHub ↗

Tracks an exponentially-weighted moving average.

Source from the content-addressed store, hash-verified

612
613
614class MovingAverage:
615 """Tracks an exponentially-weighted moving average."""
616
617 average: Optional[float]
618
619 def __init__(self) -> None:
620 self.average = None
621
622 def add_sample(self, sample: float) -> None:
623 if sample < 0:
624 raise ValueError(f"duration cannot be negative {sample}")
625 if self.average is None:
626 self.average = sample
627 else:
628 # The Server Selection Spec requires an exponentially weighted
629 # average with alpha = 0.2.
630 self.average = 0.8 * self.average + 0.2 * sample
631
632 def get(self) -> Optional[float]:
633 """Get the calculated average, or None if no samples yet."""
634 return self.average
635
636 def reset(self) -> None:
637 self.average = None

Callers 6

run_scenarioFunction · 0.90
test_moving_averageMethod · 0.90
run_scenarioFunction · 0.90
test_moving_averageMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by 4

run_scenarioFunction · 0.72
test_moving_averageMethod · 0.72
run_scenarioFunction · 0.72
test_moving_averageMethod · 0.72