Skip to content

Commit 98c2abc

Browse files
author
Michal Warda
committed
Cache dashboard discovery telemetry
1 parent d259c4a commit 98c2abc

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

pipeline/src/sam_audio_pipeline/source_frontier.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
_NETWORK_WINDOW_SECONDS = 60.0
2727
_network_samples: deque[tuple[float, int]] = deque()
2828
_network_samples_lock = threading.Lock()
29+
_discovery_snapshot_cache: dict[
30+
tuple[str, str], tuple[float, dict[str, dict[str, Any]]]
31+
] = {}
32+
_discovery_snapshot_cache_lock = threading.Lock()
33+
_DISCOVERY_SNAPSHOT_CACHE_SECONDS = 30.0
2934
DISCOVERY_PROBE_ACTIVE_SOURCES = 8
3035
DISCOVERY_MIN_SCAN_SOURCES = 12
3136
DISCOVERY_MIN_FINAL_RECORDS = 20
@@ -192,6 +197,28 @@ def discovery_strategy_snapshot(
192197
return result
193198

194199

200+
def _cached_discovery_strategy_snapshot(
201+
connection: sqlite3.Connection,
202+
*,
203+
workspace: Path,
204+
catalog_path: Path | None,
205+
) -> dict[str, dict[str, Any]]:
206+
"""Keep frequent dashboard polls from repeatedly aggregating history."""
207+
key = (str(workspace), str(catalog_path or ""))
208+
now = time.monotonic()
209+
with _discovery_snapshot_cache_lock:
210+
cached = _discovery_snapshot_cache.get(key)
211+
if cached and cached[0] > now:
212+
return cached[1]
213+
result = discovery_strategy_snapshot(connection, catalog_path=catalog_path)
214+
with _discovery_snapshot_cache_lock:
215+
_discovery_snapshot_cache[key] = (
216+
now + _DISCOVERY_SNAPSHOT_CACHE_SECONDS,
217+
result,
218+
)
219+
return result
220+
221+
195222
def connect_frontier(workspace: Path) -> sqlite3.Connection:
196223
workspace.mkdir(parents=True, exist_ok=True)
197224
connection = sqlite3.connect(
@@ -927,8 +954,10 @@ def frontier_snapshot(
927954
platform_metrics: dict[str, dict[str, Any]] = {}
928955
platform_counts = frontier_platform_counts(connection)
929956
circuits = provider_circuit_snapshot(connection, now=timestamp)
930-
discovery_strategies = discovery_strategy_snapshot(
931-
connection, catalog_path=catalog_path
957+
discovery_strategies = _cached_discovery_strategy_snapshot(
958+
connection,
959+
workspace=workspace,
960+
catalog_path=catalog_path,
932961
)
933962
for platform in sorted(platform_counts):
934963
downloads = [

0 commit comments

Comments
 (0)