forked from Github/frigate
Review summary (#10196)
* Create review summary api to get information about reviewed and unreviewed events on each day * remove unused * Fix tests * Format tests * Fix
This commit is contained in:
@@ -23,9 +23,7 @@ from frigate.const import (
|
||||
)
|
||||
from frigate.models import Event, Timeline
|
||||
from frigate.object_processing import TrackedObject
|
||||
from frigate.util.builtin import (
|
||||
get_tz_modifiers,
|
||||
)
|
||||
from frigate.util.builtin import get_tz_modifiers
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -10,9 +10,10 @@ from flask import (
|
||||
make_response,
|
||||
request,
|
||||
)
|
||||
from peewee import DoesNotExist, operator
|
||||
from peewee import Case, DoesNotExist, fn, operator
|
||||
|
||||
from frigate.models import ReviewSegment
|
||||
from frigate.util.builtin import get_tz_modifiers
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -70,6 +71,106 @@ def review():
|
||||
return jsonify([r for r in review])
|
||||
|
||||
|
||||
@ReviewBp.route("/review/summary")
|
||||
def review_summary():
|
||||
tz_name = request.args.get("timezone", default="utc", type=str)
|
||||
hour_modifier, minute_modifier, seconds_offset = get_tz_modifiers(tz_name)
|
||||
month_ago = (datetime.now() - timedelta(days=30)).timestamp()
|
||||
|
||||
groups = (
|
||||
ReviewSegment.select(
|
||||
fn.strftime(
|
||||
"%Y-%m-%d",
|
||||
fn.datetime(
|
||||
ReviewSegment.start_time,
|
||||
"unixepoch",
|
||||
hour_modifier,
|
||||
minute_modifier,
|
||||
),
|
||||
).alias("day"),
|
||||
fn.SUM(
|
||||
Case(
|
||||
None,
|
||||
[
|
||||
(
|
||||
(ReviewSegment.severity == "alert"),
|
||||
ReviewSegment.has_been_reviewed,
|
||||
)
|
||||
],
|
||||
0,
|
||||
)
|
||||
).alias("reviewed_alert"),
|
||||
fn.SUM(
|
||||
Case(
|
||||
None,
|
||||
[
|
||||
(
|
||||
(ReviewSegment.severity == "detection"),
|
||||
ReviewSegment.has_been_reviewed,
|
||||
)
|
||||
],
|
||||
0,
|
||||
)
|
||||
).alias("reviewed_detection"),
|
||||
fn.SUM(
|
||||
Case(
|
||||
None,
|
||||
[
|
||||
(
|
||||
(ReviewSegment.severity == "significant_motion"),
|
||||
ReviewSegment.has_been_reviewed,
|
||||
)
|
||||
],
|
||||
0,
|
||||
)
|
||||
).alias("reviewed_motion"),
|
||||
fn.SUM(
|
||||
Case(
|
||||
None,
|
||||
[
|
||||
(
|
||||
(ReviewSegment.severity == "alert"),
|
||||
1,
|
||||
)
|
||||
],
|
||||
0,
|
||||
)
|
||||
).alias("total_alert"),
|
||||
fn.SUM(
|
||||
Case(
|
||||
None,
|
||||
[
|
||||
(
|
||||
(ReviewSegment.severity == "detection"),
|
||||
1,
|
||||
)
|
||||
],
|
||||
0,
|
||||
)
|
||||
).alias("total_detection"),
|
||||
fn.SUM(
|
||||
Case(
|
||||
None,
|
||||
[
|
||||
(
|
||||
(ReviewSegment.severity == "significant_motion"),
|
||||
1,
|
||||
)
|
||||
],
|
||||
0,
|
||||
)
|
||||
).alias("total_motion"),
|
||||
)
|
||||
.where(ReviewSegment.start_time > month_ago)
|
||||
.group_by(
|
||||
(ReviewSegment.start_time + seconds_offset).cast("int") / (3600 * 24),
|
||||
)
|
||||
.order_by(ReviewSegment.start_time.desc())
|
||||
)
|
||||
|
||||
return jsonify([e for e in groups.dicts().iterator()])
|
||||
|
||||
|
||||
@ReviewBp.route("/review/<id>/viewed", methods=("POST",))
|
||||
def set_reviewed(id):
|
||||
try:
|
||||
|
||||
@@ -38,16 +38,16 @@ QUEUE_READ_TIMEOUT = 0.00001 # seconds
|
||||
|
||||
class SegmentInfo:
|
||||
def __init__(
|
||||
self, motion_box_count: int, active_object_count: int, average_dBFS: int
|
||||
self, motion_area: int, active_object_count: int, average_dBFS: int
|
||||
) -> None:
|
||||
self.motion_box_count = motion_box_count
|
||||
self.motion_area = motion_area
|
||||
self.active_object_count = active_object_count
|
||||
self.average_dBFS = average_dBFS
|
||||
|
||||
def should_discard_segment(self, retain_mode: RetainModeEnum) -> bool:
|
||||
return (
|
||||
retain_mode == RetainModeEnum.motion
|
||||
and self.motion_box_count == 0
|
||||
and self.motion_area == 0
|
||||
and self.average_dBFS == 0
|
||||
) or (
|
||||
retain_mode == RetainModeEnum.active_objects
|
||||
@@ -412,7 +412,7 @@ class RecordingMaintainer(threading.Thread):
|
||||
Recordings.start_time: start_time.timestamp(),
|
||||
Recordings.end_time: end_time.timestamp(),
|
||||
Recordings.duration: duration,
|
||||
Recordings.motion: segment_info.motion_box_count,
|
||||
Recordings.motion: segment_info.motion_area,
|
||||
# TODO: update this to store list of active objects at some point
|
||||
Recordings.objects: segment_info.active_object_count,
|
||||
Recordings.dBFS: segment_info.average_dBFS,
|
||||
|
||||
@@ -6,28 +6,20 @@ from frigate.record.maintainer import SegmentInfo
|
||||
|
||||
class TestRecordRetention(unittest.TestCase):
|
||||
def test_motion_should_keep_motion_not_object(self):
|
||||
segment_info = SegmentInfo(
|
||||
motion_box_count=1, active_object_count=0, average_dBFS=0
|
||||
)
|
||||
segment_info = SegmentInfo(motion_area=1, active_object_count=0, average_dBFS=0)
|
||||
assert not segment_info.should_discard_segment(RetainModeEnum.motion)
|
||||
assert segment_info.should_discard_segment(RetainModeEnum.active_objects)
|
||||
|
||||
def test_object_should_keep_object_not_motion(self):
|
||||
segment_info = SegmentInfo(
|
||||
motion_box_count=0, active_object_count=1, average_dBFS=0
|
||||
)
|
||||
segment_info = SegmentInfo(motion_area=0, active_object_count=1, average_dBFS=0)
|
||||
assert segment_info.should_discard_segment(RetainModeEnum.motion)
|
||||
assert not segment_info.should_discard_segment(RetainModeEnum.active_objects)
|
||||
|
||||
def test_all_should_keep_all(self):
|
||||
segment_info = SegmentInfo(
|
||||
motion_box_count=0, active_object_count=0, average_dBFS=0
|
||||
)
|
||||
segment_info = SegmentInfo(motion_area=0, active_object_count=0, average_dBFS=0)
|
||||
assert not segment_info.should_discard_segment(RetainModeEnum.all)
|
||||
|
||||
def test_should_keep_audio_in_motion_mode(self):
|
||||
segment_info = SegmentInfo(
|
||||
motion_box_count=0, active_object_count=0, average_dBFS=1
|
||||
)
|
||||
segment_info = SegmentInfo(motion_area=0, active_object_count=0, average_dBFS=1)
|
||||
assert not segment_info.should_discard_segment(RetainModeEnum.motion)
|
||||
assert segment_info.should_discard_segment(RetainModeEnum.active_objects)
|
||||
|
||||
Reference in New Issue
Block a user