compass.services.usage.TimeBoundedUsageTracker#

class TimeBoundedUsageTracker(max_seconds=70)[source]#

Bases: object

Track usage of a resource over time

This class wraps a double-ended queue, and any inputs older than a certain time are dropped. Those values are also subtracted from the running total.

References

https://stackoverflow.com/questions/51485656/efficient-time-bound-queue-in-python

Parameters:

max_seconds (int, optional) – Maximum age in seconds of an element before it is dropped from consideration. By default, 65.

Methods

add(value)

Add a value to track

Attributes

total

Total of all entries younger than max_seconds

property total#

Total of all entries younger than max_seconds

Type:

float

add(value)[source]#

Add a value to track

Parameters:

value (int or float) – A new value to add to the queue. It’s total will be added to the running total, and it will live for max_seconds before being discarded.