mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-07 14:13:00 +03:00
* Add analytics opt-in setting, prompt notice kind, and notice watermarks * Add the analytics report schema * Add the analytics state file * Add the install and hardware analytics collectors * Add the detection analytics collector * Add the cameras analytics collector * Add the features analytics collector * Add the health analytics collector * Build analytics reports from the section collectors * Add the analytics report transport * Send the daily analytics report from the main process * Add the analytics preview API * Generate the analytics schema and check it in CI * Record the image variant in each Dockerfile * Show the analytics preview in telemetry settings * Document anonymous analytics * change wording * fixes * update network requirements docs * adapt analytics to free-form model scenes, split notice counts, and peewee 4 * fixes * change log levels * docstrings * docstrings * honor file-only analytics opt-outs and skip pre-consent notices An opt-out saved through the YAML editor's "Save only" or a hand edit never reached the live config, so reports kept going out under the old install ID until a restart. The reporter now reads telemetry.analytics from the config file when a report is due, validated with TelemetryConfig so yes, on, and 1 still count as consent, and an opt-out there deletes the state file and skips the send. The first report under a new install ID also carried every notice since the counters started, including ones from before the opt-in, so a fresh state file now resets the notice watermarks before the report's snapshot. * take the notice baseline at opt-in instead of the first send The watermark reset ran at the first send attempt, so notices raised between the opt-in and that send, up to 45 minutes after a startup, were absorbed into the reset and never reported. The reporter now writes the new install ID and resets the watermarks as soon as it sees consent without a state file. * report notice counts cumulatively from a database baseline Moving watermarks after each accepted send kept the install ID and the notice baseline out of step: pre-consent notices went out, notices raised before the first send were dropped, and a send that finished after an off/on toggle overwrote the new identity's baseline. health.notices now carries cumulative counts since the install ID was created. Creating an ID copies the lifetime counts into the existing reported_* columns before the state file names it, and nothing is written after a send, so the backend diffs consecutive reports per install ID. * write held notice repeats before the analytics baseline reset Repeats of batched kinds like failed_login wait in memory until the stats emitter flushes them, so a baseline reset taken while some were held left them out, and the next flush counted those pre-opt-in repeats as post-consent. The reset now writes held repeats to the notice history under the same lock before copying the counts. * seed detector inference speed from the first inference The average started at 0 so idle detectors read 0, but that made every detector's speed climb from 0 for its first couple dozen inferences after a start. Detectors now use the same InferenceSpeed helper as enrichments, so the first inference seeds the average and idle detectors still read 0.
138 lines
4.7 KiB
Python
138 lines
4.7 KiB
Python
"""Tests for the analytics report schema."""
|
|
|
|
import inspect
|
|
import re
|
|
import unittest
|
|
from enum import Enum
|
|
from typing import Any, get_args, get_origin
|
|
|
|
from pydantic import BaseModel
|
|
|
|
import frigate.detectors.hardware as detection_hardware
|
|
import frigate.util.hwaccel as hwaccel
|
|
from frigate.analytics.schema import (
|
|
AnalyticsReport,
|
|
DecodeFamily,
|
|
HardwareKey,
|
|
HwaccelKey,
|
|
InputPresetKey,
|
|
NoticeKindKey,
|
|
)
|
|
from frigate.ffmpeg_presets import PRESETS_HW_ACCEL_DECODE, PRESETS_INPUT
|
|
from frigate.notices.types import NOTICE_KINDS
|
|
from frigate.util.hwaccel import HwaccelFamily
|
|
|
|
# the only str fields: generated ids and strings the hardware reports
|
|
ALLOWED_STRINGS = {
|
|
("AnalyticsReport", "install_id"),
|
|
("AnalyticsReport", "report_id"),
|
|
("InstallSection", "version"),
|
|
("InstallSection", "kernel"),
|
|
("HardwareSection", "cpu_model"),
|
|
("GpuInfo", "name"),
|
|
("StorageInfo", "record_fs"),
|
|
("DetectionModel", "input"),
|
|
}
|
|
|
|
|
|
def nested_models(annotation: Any) -> list[type[BaseModel]]:
|
|
if isinstance(annotation, type) and issubclass(annotation, BaseModel):
|
|
return [annotation]
|
|
|
|
return [model for arg in get_args(annotation) for model in nested_models(arg)]
|
|
|
|
|
|
def all_models(
|
|
model: type[BaseModel], seen: set[type[BaseModel]] | None = None
|
|
) -> list[type[BaseModel]]:
|
|
seen = set() if seen is None else seen
|
|
|
|
if model in seen:
|
|
return []
|
|
|
|
seen.add(model)
|
|
found = [model]
|
|
|
|
for field in model.model_fields.values():
|
|
for nested in nested_models(field.annotation):
|
|
found += all_models(nested, seen)
|
|
|
|
return found
|
|
|
|
|
|
def dict_key_types(annotation: Any) -> list[Any]:
|
|
keys = [get_args(annotation)[0]] if get_origin(annotation) is dict else []
|
|
return keys + [key for arg in get_args(annotation) for key in dict_key_types(arg)]
|
|
|
|
|
|
class TestSchemaDiscipline(unittest.TestCase):
|
|
def test_every_field_has_a_description_and_public_flag(self):
|
|
for model in all_models(AnalyticsReport):
|
|
for name, field in model.model_fields.items():
|
|
with self.subTest(field=f"{model.__name__}.{name}"):
|
|
self.assertTrue(field.description)
|
|
self.assertIsInstance(field.json_schema_extra, dict)
|
|
self.assertIsInstance(field.json_schema_extra.get("x-public"), bool)
|
|
|
|
def test_strings_are_limited_to_ids_and_hardware_names(self):
|
|
for model in all_models(AnalyticsReport):
|
|
properties = model.model_json_schema()["properties"]
|
|
|
|
for name, field in model.model_fields.items():
|
|
if field.annotation is not str:
|
|
continue
|
|
|
|
with self.subTest(field=f"{model.__name__}.{name}"):
|
|
self.assertIn((model.__name__, name), ALLOWED_STRINGS)
|
|
self.assertTrue(
|
|
"maxLength" in properties[name] or "pattern" in properties[name]
|
|
)
|
|
|
|
def test_map_keys_are_enums(self):
|
|
for model in all_models(AnalyticsReport):
|
|
for name, field in model.model_fields.items():
|
|
for key_type in dict_key_types(field.annotation):
|
|
with self.subTest(field=f"{model.__name__}.{name}"):
|
|
self.assertTrue(issubclass(key_type, Enum))
|
|
|
|
|
|
class TestEnumsTrackTheirSources(unittest.TestCase):
|
|
def test_hardware_keys_cover_every_probe(self):
|
|
probed = set(
|
|
re.findall(
|
|
r'_hardware\(\s*"([^"]+)"', inspect.getsource(detection_hardware)
|
|
)
|
|
)
|
|
|
|
self.assertTrue(probed)
|
|
self.assertLessEqual(probed, {key.value for key in HardwareKey})
|
|
|
|
def test_decode_families_cover_every_hwaccel_family(self):
|
|
families = {
|
|
value.key
|
|
for value in vars(hwaccel).values()
|
|
if isinstance(value, HwaccelFamily)
|
|
}
|
|
|
|
self.assertTrue(families)
|
|
self.assertLessEqual(families, {family.value for family in DecodeFamily})
|
|
|
|
def test_preset_keys_mirror_the_presets(self):
|
|
for enum, presets in (
|
|
(HwaccelKey, PRESETS_HW_ACCEL_DECODE),
|
|
(InputPresetKey, PRESETS_INPUT),
|
|
):
|
|
with self.subTest(enum=enum.__name__):
|
|
self.assertEqual(
|
|
{key.value for key in enum},
|
|
{name.removeprefix("preset-") for name in presets}
|
|
| {"custom", "none"},
|
|
)
|
|
|
|
def test_notice_keys_are_the_reportable_kinds(self):
|
|
self.assertEqual(
|
|
{key.value for key in NoticeKindKey},
|
|
{key for key, kind in NOTICE_KINDS.items() if kind.reportable},
|
|
)
|
|
self.assertNotIn("analytics_prompt", {key.value for key in NoticeKindKey})
|