2024-03-02 23:10:37 +01:00
|
|
|
"""Review apis."""
|
|
|
|
|
2024-09-24 21:32:50 +02:00
|
|
|
import datetime
|
2024-03-02 23:10:37 +01:00
|
|
|
import logging
|
|
|
|
from functools import reduce
|
2024-03-27 14:22:28 +01:00
|
|
|
from pathlib import Path
|
2024-03-02 23:10:37 +01:00
|
|
|
|
2024-03-05 20:55:44 +01:00
|
|
|
import pandas as pd
|
2024-09-24 15:05:30 +02:00
|
|
|
from fastapi import APIRouter
|
|
|
|
from fastapi.params import Depends
|
|
|
|
from fastapi.responses import JSONResponse
|
2024-03-04 01:19:02 +01:00
|
|
|
from peewee import Case, DoesNotExist, fn, operator
|
2024-04-17 14:02:03 +02:00
|
|
|
from playhouse.shortcuts import model_to_dict
|
2024-03-02 23:10:37 +01:00
|
|
|
|
2024-12-06 15:04:02 +01:00
|
|
|
from frigate.api.defs.query.review_query_parameters import (
|
2024-09-24 15:05:30 +02:00
|
|
|
ReviewActivityMotionQueryParams,
|
|
|
|
ReviewQueryParams,
|
|
|
|
ReviewSummaryQueryParams,
|
|
|
|
)
|
2024-12-06 15:04:02 +01:00
|
|
|
from frigate.api.defs.request.review_body import ReviewModifyMultipleBody
|
|
|
|
from frigate.api.defs.response.generic_response import GenericResponse
|
|
|
|
from frigate.api.defs.response.review_response import (
|
2024-10-23 15:35:49 +02:00
|
|
|
ReviewActivityMotionResponse,
|
|
|
|
ReviewSegmentResponse,
|
|
|
|
ReviewSummaryResponse,
|
|
|
|
)
|
2024-09-24 15:05:30 +02:00
|
|
|
from frigate.api.defs.tags import Tags
|
2024-03-05 20:55:44 +01:00
|
|
|
from frigate.models import Recordings, ReviewSegment
|
2024-12-04 13:52:08 +01:00
|
|
|
from frigate.review.maintainer import SeverityEnum
|
2024-03-04 01:19:02 +01:00
|
|
|
from frigate.util.builtin import get_tz_modifiers
|
2024-03-02 23:10:37 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
router = APIRouter(tags=[Tags.review])
|
2024-03-02 23:10:37 +01:00
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.get("/review", response_model=list[ReviewSegmentResponse])
|
2024-09-24 15:05:30 +02:00
|
|
|
def review(params: ReviewQueryParams = Depends()):
|
|
|
|
cameras = params.cameras
|
|
|
|
labels = params.labels
|
|
|
|
zones = params.zones
|
|
|
|
reviewed = params.reviewed
|
|
|
|
limit = params.limit
|
|
|
|
severity = params.severity
|
2024-09-25 17:49:54 +02:00
|
|
|
before = params.before or datetime.datetime.now().timestamp()
|
|
|
|
after = (
|
|
|
|
params.after
|
|
|
|
or (datetime.datetime.now() - datetime.timedelta(hours=24)).timestamp()
|
|
|
|
)
|
2024-03-02 23:10:37 +01:00
|
|
|
|
2024-04-11 14:42:16 +02:00
|
|
|
clauses = [
|
|
|
|
(
|
|
|
|
(ReviewSegment.start_time > after)
|
|
|
|
& (
|
|
|
|
(ReviewSegment.end_time.is_null(True))
|
|
|
|
| (ReviewSegment.end_time < before)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
2024-03-02 23:10:37 +01:00
|
|
|
|
|
|
|
if cameras != "all":
|
|
|
|
camera_list = cameras.split(",")
|
|
|
|
clauses.append((ReviewSegment.camera << camera_list))
|
|
|
|
|
|
|
|
if labels != "all":
|
|
|
|
# use matching so segments with multiple labels
|
|
|
|
# still match on a search where any label matches
|
|
|
|
label_clauses = []
|
|
|
|
filtered_labels = labels.split(",")
|
|
|
|
|
|
|
|
for label in filtered_labels:
|
|
|
|
label_clauses.append(
|
|
|
|
(ReviewSegment.data["objects"].cast("text") % f'*"{label}"*')
|
2024-04-11 14:42:16 +02:00
|
|
|
| (ReviewSegment.data["audio"].cast("text") % f'*"{label}"*')
|
2024-03-02 23:10:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
label_clause = reduce(operator.or_, label_clauses)
|
|
|
|
clauses.append((label_clause))
|
|
|
|
|
2024-06-11 16:19:17 +02:00
|
|
|
if zones != "all":
|
|
|
|
# use matching so segments with multiple zones
|
|
|
|
# still match on a search where any zone matches
|
|
|
|
zone_clauses = []
|
|
|
|
filtered_zones = zones.split(",")
|
|
|
|
|
|
|
|
for zone in filtered_zones:
|
|
|
|
zone_clauses.append(
|
|
|
|
(ReviewSegment.data["zones"].cast("text") % f'*"{zone}"*')
|
|
|
|
)
|
|
|
|
|
|
|
|
zone_clause = reduce(operator.or_, zone_clauses)
|
|
|
|
clauses.append((zone_clause))
|
|
|
|
|
2024-03-02 23:10:37 +01:00
|
|
|
if reviewed == 0:
|
|
|
|
clauses.append((ReviewSegment.has_been_reviewed == False))
|
|
|
|
|
|
|
|
if severity:
|
|
|
|
clauses.append((ReviewSegment.severity == severity))
|
|
|
|
|
|
|
|
review = (
|
|
|
|
ReviewSegment.select()
|
|
|
|
.where(reduce(operator.and_, clauses))
|
|
|
|
.order_by(ReviewSegment.severity.asc())
|
|
|
|
.order_by(ReviewSegment.start_time.desc())
|
|
|
|
.limit(limit)
|
|
|
|
.dicts()
|
2024-03-27 14:22:28 +01:00
|
|
|
.iterator()
|
2024-03-02 23:10:37 +01:00
|
|
|
)
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(content=[r for r in review])
|
2024-03-02 23:10:37 +01:00
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.get("/review/summary", response_model=ReviewSummaryResponse)
|
2024-09-24 15:05:30 +02:00
|
|
|
def review_summary(params: ReviewSummaryQueryParams = Depends()):
|
|
|
|
hour_modifier, minute_modifier, seconds_offset = get_tz_modifiers(params.timezone)
|
2024-09-24 21:32:50 +02:00
|
|
|
day_ago = (datetime.datetime.now() - datetime.timedelta(hours=24)).timestamp()
|
|
|
|
month_ago = (datetime.datetime.now() - datetime.timedelta(days=30)).timestamp()
|
2024-03-04 01:19:02 +01:00
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
cameras = params.cameras
|
|
|
|
labels = params.labels
|
|
|
|
zones = params.zones
|
2024-03-05 13:02:34 +01:00
|
|
|
|
2024-03-06 01:39:37 +01:00
|
|
|
clauses = [(ReviewSegment.start_time > day_ago)]
|
|
|
|
|
|
|
|
if cameras != "all":
|
|
|
|
camera_list = cameras.split(",")
|
|
|
|
clauses.append((ReviewSegment.camera << camera_list))
|
|
|
|
|
|
|
|
if labels != "all":
|
|
|
|
# use matching so segments with multiple labels
|
|
|
|
# still match on a search where any label matches
|
|
|
|
label_clauses = []
|
|
|
|
filtered_labels = labels.split(",")
|
|
|
|
|
|
|
|
for label in filtered_labels:
|
|
|
|
label_clauses.append(
|
|
|
|
(ReviewSegment.data["objects"].cast("text") % f'*"{label}"*')
|
2024-04-11 14:42:16 +02:00
|
|
|
| (ReviewSegment.data["audio"].cast("text") % f'*"{label}"*')
|
2024-03-06 01:39:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
label_clause = reduce(operator.or_, label_clauses)
|
|
|
|
clauses.append((label_clause))
|
|
|
|
|
2024-06-11 16:19:17 +02:00
|
|
|
if zones != "all":
|
|
|
|
# use matching so segments with multiple zones
|
|
|
|
# still match on a search where any zone matches
|
|
|
|
zone_clauses = []
|
|
|
|
filtered_zones = zones.split(",")
|
|
|
|
|
|
|
|
for zone in filtered_zones:
|
|
|
|
zone_clauses.append(
|
|
|
|
(ReviewSegment.data["zones"].cast("text") % f'*"{zone}"*')
|
|
|
|
)
|
|
|
|
|
|
|
|
zone_clause = reduce(operator.or_, zone_clauses)
|
|
|
|
clauses.append((zone_clause))
|
|
|
|
|
2024-03-06 01:39:37 +01:00
|
|
|
last_24 = (
|
|
|
|
ReviewSegment.select(
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.alert),
|
2024-03-06 01:39:37 +01:00
|
|
|
ReviewSegment.has_been_reviewed,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("reviewed_alert"),
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.detection),
|
2024-03-06 01:39:37 +01:00
|
|
|
ReviewSegment.has_been_reviewed,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("reviewed_detection"),
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.alert),
|
2024-03-06 01:39:37 +01:00
|
|
|
1,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("total_alert"),
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.detection),
|
2024-03-06 01:39:37 +01:00
|
|
|
1,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("total_detection"),
|
|
|
|
)
|
|
|
|
.where(reduce(operator.and_, clauses))
|
|
|
|
.dicts()
|
|
|
|
.get()
|
|
|
|
)
|
|
|
|
|
2024-03-05 13:02:34 +01:00
|
|
|
clauses = [(ReviewSegment.start_time > month_ago)]
|
|
|
|
|
|
|
|
if cameras != "all":
|
|
|
|
camera_list = cameras.split(",")
|
|
|
|
clauses.append((ReviewSegment.camera << camera_list))
|
|
|
|
|
|
|
|
if labels != "all":
|
|
|
|
# use matching so segments with multiple labels
|
|
|
|
# still match on a search where any label matches
|
|
|
|
label_clauses = []
|
|
|
|
filtered_labels = labels.split(",")
|
|
|
|
|
|
|
|
for label in filtered_labels:
|
|
|
|
label_clauses.append(
|
|
|
|
(ReviewSegment.data["objects"].cast("text") % f'*"{label}"*')
|
|
|
|
)
|
|
|
|
|
|
|
|
label_clause = reduce(operator.or_, label_clauses)
|
|
|
|
clauses.append((label_clause))
|
|
|
|
|
2024-12-04 13:52:08 +01:00
|
|
|
day_in_seconds = 60 * 60 * 24
|
2024-03-06 01:39:37 +01:00
|
|
|
last_month = (
|
2024-03-04 01:19:02 +01:00
|
|
|
ReviewSegment.select(
|
|
|
|
fn.strftime(
|
|
|
|
"%Y-%m-%d",
|
|
|
|
fn.datetime(
|
|
|
|
ReviewSegment.start_time,
|
|
|
|
"unixepoch",
|
|
|
|
hour_modifier,
|
|
|
|
minute_modifier,
|
|
|
|
),
|
|
|
|
).alias("day"),
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.alert),
|
2024-03-04 01:19:02 +01:00
|
|
|
ReviewSegment.has_been_reviewed,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("reviewed_alert"),
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.detection),
|
2024-03-04 01:19:02 +01:00
|
|
|
ReviewSegment.has_been_reviewed,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("reviewed_detection"),
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.alert),
|
2024-03-04 01:19:02 +01:00
|
|
|
1,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("total_alert"),
|
|
|
|
fn.SUM(
|
|
|
|
Case(
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.severity == SeverityEnum.detection),
|
2024-03-04 01:19:02 +01:00
|
|
|
1,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
).alias("total_detection"),
|
|
|
|
)
|
2024-03-05 13:02:34 +01:00
|
|
|
.where(reduce(operator.and_, clauses))
|
2024-03-04 01:19:02 +01:00
|
|
|
.group_by(
|
2024-12-04 13:52:08 +01:00
|
|
|
(ReviewSegment.start_time + seconds_offset).cast("int") / day_in_seconds,
|
2024-03-04 01:19:02 +01:00
|
|
|
)
|
|
|
|
.order_by(ReviewSegment.start_time.desc())
|
|
|
|
)
|
|
|
|
|
2024-03-06 01:39:37 +01:00
|
|
|
data = {
|
|
|
|
"last24Hours": last_24,
|
|
|
|
}
|
2024-03-04 01:19:02 +01:00
|
|
|
|
2024-03-06 01:39:37 +01:00
|
|
|
for e in last_month.dicts().iterator():
|
|
|
|
data[e["day"]] = e
|
2024-03-04 01:19:02 +01:00
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(content=data)
|
2024-03-02 23:10:37 +01:00
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.post("/reviews/viewed", response_model=GenericResponse)
|
|
|
|
def set_multiple_reviewed(body: ReviewModifyMultipleBody):
|
2024-03-02 23:10:37 +01:00
|
|
|
ReviewSegment.update(has_been_reviewed=True).where(
|
2024-10-23 15:35:49 +02:00
|
|
|
ReviewSegment.id << body.ids
|
2024-03-02 23:10:37 +01:00
|
|
|
).execute()
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=({"success": True, "message": "Reviewed multiple items"}),
|
|
|
|
status_code=200,
|
2024-03-02 23:10:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.post("/reviews/delete", response_model=GenericResponse)
|
|
|
|
def delete_reviews(body: ReviewModifyMultipleBody):
|
|
|
|
list_of_ids = body.ids
|
2024-03-27 14:22:28 +01:00
|
|
|
reviews = (
|
|
|
|
ReviewSegment.select(
|
|
|
|
ReviewSegment.camera,
|
|
|
|
ReviewSegment.start_time,
|
|
|
|
ReviewSegment.end_time,
|
|
|
|
)
|
|
|
|
.where(ReviewSegment.id << list_of_ids)
|
|
|
|
.dicts()
|
|
|
|
.iterator()
|
|
|
|
)
|
|
|
|
recording_ids = []
|
|
|
|
|
|
|
|
for review in reviews:
|
|
|
|
start_time = review["start_time"]
|
|
|
|
end_time = review["end_time"]
|
|
|
|
camera_name = review["camera"]
|
|
|
|
recordings = (
|
|
|
|
Recordings.select(Recordings.id, Recordings.path)
|
|
|
|
.where(
|
|
|
|
Recordings.start_time.between(start_time, end_time)
|
|
|
|
| Recordings.end_time.between(start_time, end_time)
|
|
|
|
| (
|
|
|
|
(start_time > Recordings.start_time)
|
|
|
|
& (end_time < Recordings.end_time)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.where(Recordings.camera == camera_name)
|
|
|
|
.dicts()
|
|
|
|
.iterator()
|
|
|
|
)
|
|
|
|
|
|
|
|
for recording in recordings:
|
|
|
|
Path(recording["path"]).unlink(missing_ok=True)
|
|
|
|
recording_ids.append(recording["id"])
|
|
|
|
|
|
|
|
# delete recordings and review segments
|
|
|
|
Recordings.delete().where(Recordings.id << recording_ids).execute()
|
2024-03-02 23:10:37 +01:00
|
|
|
ReviewSegment.delete().where(ReviewSegment.id << list_of_ids).execute()
|
|
|
|
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(
|
2024-12-06 15:04:02 +01:00
|
|
|
content=({"success": True, "message": "Deleted review items."}), status_code=200
|
2024-09-24 15:05:30 +02:00
|
|
|
)
|
2024-03-05 20:55:44 +01:00
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.get(
|
|
|
|
"/review/activity/motion", response_model=list[ReviewActivityMotionResponse]
|
|
|
|
)
|
2024-09-24 15:05:30 +02:00
|
|
|
def motion_activity(params: ReviewActivityMotionQueryParams = Depends()):
|
2024-03-05 20:55:44 +01:00
|
|
|
"""Get motion and audio activity."""
|
2024-09-24 15:05:30 +02:00
|
|
|
cameras = params.cameras
|
2024-09-25 17:49:54 +02:00
|
|
|
before = params.before or datetime.datetime.now().timestamp()
|
|
|
|
after = (
|
|
|
|
params.after
|
|
|
|
or (datetime.datetime.now() - datetime.timedelta(hours=1)).timestamp()
|
|
|
|
)
|
2024-09-24 15:05:30 +02:00
|
|
|
# get scale in seconds
|
|
|
|
scale = params.scale
|
2024-03-05 20:55:44 +01:00
|
|
|
|
2024-03-06 01:39:37 +01:00
|
|
|
clauses = [(Recordings.start_time > after) & (Recordings.end_time < before)]
|
2024-03-25 17:00:28 +01:00
|
|
|
clauses.append((Recordings.motion > 0))
|
2024-03-06 01:39:37 +01:00
|
|
|
|
|
|
|
if cameras != "all":
|
|
|
|
camera_list = cameras.split(",")
|
|
|
|
clauses.append((Recordings.camera << camera_list))
|
2024-03-05 20:55:44 +01:00
|
|
|
|
2024-03-14 20:57:14 +01:00
|
|
|
data: list[Recordings] = (
|
2024-03-05 20:55:44 +01:00
|
|
|
Recordings.select(
|
2024-03-25 17:00:28 +01:00
|
|
|
Recordings.camera,
|
2024-03-05 20:55:44 +01:00
|
|
|
Recordings.start_time,
|
2024-03-15 20:13:40 +01:00
|
|
|
Recordings.motion,
|
2024-03-05 20:55:44 +01:00
|
|
|
)
|
2024-03-06 01:39:37 +01:00
|
|
|
.where(reduce(operator.and_, clauses))
|
2024-03-05 20:55:44 +01:00
|
|
|
.order_by(Recordings.start_time.asc())
|
2024-03-14 20:57:14 +01:00
|
|
|
.dicts()
|
2024-03-05 20:55:44 +01:00
|
|
|
.iterator()
|
|
|
|
)
|
|
|
|
|
|
|
|
# resample data using pandas to get activity on scaled basis
|
2024-03-25 17:00:28 +01:00
|
|
|
df = pd.DataFrame(data, columns=["start_time", "motion", "camera"])
|
2024-05-28 19:33:28 +02:00
|
|
|
|
|
|
|
if df.empty:
|
|
|
|
logger.warning("No motion data found for the requested time range")
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(content=[])
|
2024-05-28 19:33:28 +02:00
|
|
|
|
2024-07-24 16:58:42 +02:00
|
|
|
df = df.astype(dtype={"motion": "float32"})
|
2024-03-05 20:55:44 +01:00
|
|
|
|
|
|
|
# set date as datetime index
|
|
|
|
df["start_time"] = pd.to_datetime(df["start_time"], unit="s")
|
|
|
|
df.set_index(["start_time"], inplace=True)
|
|
|
|
|
|
|
|
# normalize data
|
2024-03-25 17:00:28 +01:00
|
|
|
motion = (
|
|
|
|
df["motion"]
|
2024-04-11 22:54:09 +02:00
|
|
|
.resample(f"{scale}s")
|
2024-03-14 20:57:14 +01:00
|
|
|
.apply(lambda x: max(x, key=abs, default=0.0))
|
|
|
|
.fillna(0.0)
|
2024-03-25 17:00:28 +01:00
|
|
|
.to_frame()
|
2024-03-05 20:55:44 +01:00
|
|
|
)
|
2024-04-10 00:51:38 +02:00
|
|
|
cameras = df["camera"].resample(f"{scale}s").agg(lambda x: ",".join(set(x)))
|
2024-03-25 17:00:28 +01:00
|
|
|
df = motion.join(cameras)
|
2024-03-17 13:30:39 +01:00
|
|
|
|
|
|
|
length = df.shape[0]
|
|
|
|
chunk = int(60 * (60 / scale))
|
|
|
|
|
|
|
|
for i in range(0, length, chunk):
|
|
|
|
part = df.iloc[i : i + chunk]
|
2024-07-24 16:58:42 +02:00
|
|
|
min_val, max_val = part["motion"].min(), part["motion"].max()
|
|
|
|
if min_val != max_val:
|
|
|
|
df.iloc[i : i + chunk, 0] = (
|
|
|
|
part["motion"].sub(min_val).div(max_val - min_val).mul(100).fillna(0)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
df.iloc[i : i + chunk, 0] = 0.0
|
2024-03-09 15:08:06 +01:00
|
|
|
|
|
|
|
# change types for output
|
|
|
|
df.index = df.index.astype(int) // (10**9)
|
|
|
|
normalized = df.reset_index().to_dict("records")
|
2024-09-24 15:05:30 +02:00
|
|
|
return JSONResponse(content=normalized)
|
2024-03-09 15:08:06 +01:00
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.get("/review/event/{event_id}", response_model=ReviewSegmentResponse)
|
2024-09-24 21:32:50 +02:00
|
|
|
def get_review_from_event(event_id: str):
|
|
|
|
try:
|
2024-10-23 15:35:49 +02:00
|
|
|
return JSONResponse(
|
|
|
|
model_to_dict(
|
|
|
|
ReviewSegment.get(
|
|
|
|
ReviewSegment.data["detections"].cast("text") % f'*"{event_id}"*'
|
|
|
|
)
|
2024-09-24 21:32:50 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
except DoesNotExist:
|
2024-10-23 15:35:49 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content={"success": False, "message": "Review item not found"},
|
|
|
|
status_code=404,
|
|
|
|
)
|
2024-09-24 21:32:50 +02:00
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.get("/review/{review_id}", response_model=ReviewSegmentResponse)
|
|
|
|
def get_review(review_id: str):
|
2024-09-24 21:32:50 +02:00
|
|
|
try:
|
2024-10-23 15:35:49 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content=model_to_dict(ReviewSegment.get(ReviewSegment.id == review_id))
|
|
|
|
)
|
2024-09-24 21:32:50 +02:00
|
|
|
except DoesNotExist:
|
2024-10-23 15:35:49 +02:00
|
|
|
return JSONResponse(
|
|
|
|
content={"success": False, "message": "Review item not found"},
|
|
|
|
status_code=404,
|
|
|
|
)
|
2024-09-24 21:32:50 +02:00
|
|
|
|
|
|
|
|
2024-10-23 15:35:49 +02:00
|
|
|
@router.delete("/review/{review_id}/viewed", response_model=GenericResponse)
|
|
|
|
def set_not_reviewed(review_id: str):
|
2024-09-24 21:32:50 +02:00
|
|
|
try:
|
2024-10-23 15:35:49 +02:00
|
|
|
review: ReviewSegment = ReviewSegment.get(ReviewSegment.id == review_id)
|
2024-09-24 21:32:50 +02:00
|
|
|
except DoesNotExist:
|
|
|
|
return JSONResponse(
|
|
|
|
content=(
|
2024-10-23 15:35:49 +02:00
|
|
|
{"success": False, "message": "Review " + review_id + " not found"}
|
2024-09-24 21:32:50 +02:00
|
|
|
),
|
|
|
|
status_code=404,
|
|
|
|
)
|
|
|
|
|
|
|
|
review.has_been_reviewed = False
|
|
|
|
review.save()
|
|
|
|
|
|
|
|
return JSONResponse(
|
2024-10-23 15:35:49 +02:00
|
|
|
content=(
|
|
|
|
{"success": True, "message": "Set Review " + review_id + " as not viewed"}
|
|
|
|
),
|
2024-09-24 21:32:50 +02:00
|
|
|
status_code=200,
|
|
|
|
)
|