2024-02-21 21:10:28 +01:00
|
|
|
"""Emit stats to listeners."""
|
|
|
|
|
2024-04-04 05:22:11 +02:00
|
|
|
import itertools
|
2024-02-21 21:10:28 +01:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
from multiprocessing.synchronize import Event as MpEvent
|
2024-04-04 05:22:11 +02:00
|
|
|
from typing import Optional
|
2024-02-21 21:10:28 +01:00
|
|
|
|
|
|
|
from frigate.comms.inter_process import InterProcessRequestor
|
|
|
|
from frigate.config import FrigateConfig
|
2024-07-02 00:08:14 +02:00
|
|
|
from frigate.const import FREQUENCY_STATS_POINTS
|
2024-02-21 21:10:28 +01:00
|
|
|
from frigate.stats.util import stats_snapshot
|
|
|
|
from frigate.types import StatsTrackingTypes
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-04-04 18:24:23 +02:00
|
|
|
MAX_STATS_POINTS = 80
|
2024-04-04 05:22:11 +02:00
|
|
|
|
|
|
|
|
2024-02-21 21:10:28 +01:00
|
|
|
class StatsEmitter(threading.Thread):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config: FrigateConfig,
|
|
|
|
stats_tracking: StatsTrackingTypes,
|
|
|
|
stop_event: MpEvent,
|
|
|
|
):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.name = "frigate_stats_emitter"
|
|
|
|
self.config = config
|
|
|
|
self.stats_tracking = stats_tracking
|
|
|
|
self.stop_event = stop_event
|
|
|
|
self.hwaccel_errors: list[str] = []
|
|
|
|
self.stats_history: list[dict[str, any]] = []
|
|
|
|
|
|
|
|
# create communication for stats
|
|
|
|
self.requestor = InterProcessRequestor()
|
|
|
|
|
|
|
|
def get_latest_stats(self) -> dict[str, any]:
|
|
|
|
"""Get latest stats."""
|
|
|
|
if len(self.stats_history) > 0:
|
|
|
|
return self.stats_history[-1]
|
|
|
|
else:
|
|
|
|
stats = stats_snapshot(
|
|
|
|
self.config, self.stats_tracking, self.hwaccel_errors
|
|
|
|
)
|
|
|
|
self.stats_history.append(stats)
|
|
|
|
return stats
|
|
|
|
|
2024-04-04 05:22:11 +02:00
|
|
|
def get_stats_history(
|
|
|
|
self, keys: Optional[list[str]] = None
|
|
|
|
) -> list[dict[str, any]]:
|
2024-02-21 21:10:28 +01:00
|
|
|
"""Get stats history."""
|
2024-04-04 05:22:11 +02:00
|
|
|
if not keys:
|
|
|
|
return self.stats_history
|
|
|
|
|
|
|
|
selected_stats: list[dict[str, any]] = []
|
|
|
|
|
|
|
|
for s in self.stats_history:
|
|
|
|
selected = {}
|
|
|
|
|
|
|
|
for k in keys:
|
|
|
|
selected[k] = s.get(k)
|
|
|
|
|
|
|
|
selected_stats.append(selected)
|
|
|
|
|
|
|
|
return selected_stats
|
2024-02-21 21:10:28 +01:00
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
time.sleep(10)
|
2024-04-04 05:22:11 +02:00
|
|
|
for counter in itertools.cycle(
|
2024-04-04 18:24:23 +02:00
|
|
|
range(int(self.config.mqtt.stats_interval / FREQUENCY_STATS_POINTS))
|
2024-04-04 05:22:11 +02:00
|
|
|
):
|
2024-04-04 18:24:23 +02:00
|
|
|
if self.stop_event.wait(FREQUENCY_STATS_POINTS):
|
2024-04-04 05:22:11 +02:00
|
|
|
break
|
|
|
|
|
2024-02-21 21:10:28 +01:00
|
|
|
logger.debug("Starting stats collection")
|
|
|
|
stats = stats_snapshot(
|
|
|
|
self.config, self.stats_tracking, self.hwaccel_errors
|
|
|
|
)
|
|
|
|
self.stats_history.append(stats)
|
2024-04-04 05:22:11 +02:00
|
|
|
self.stats_history = self.stats_history[-MAX_STATS_POINTS:]
|
|
|
|
|
|
|
|
if counter == 0:
|
|
|
|
self.requestor.send_data("stats", json.dumps(stats))
|
|
|
|
|
2024-02-21 21:10:28 +01:00
|
|
|
logger.debug("Finished stats collection")
|
2024-04-04 05:22:11 +02:00
|
|
|
|
2024-02-21 21:10:28 +01:00
|
|
|
logger.info("Exiting stats emitter...")
|