Fixes for timezone in calls

This commit is contained in:
Nick Mowen 2022-12-09 13:27:38 -07:00
parent 109d315728
commit 43d43524ab
2 changed files with 21 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import logging
import json import json
import os import os
import subprocess as sp import subprocess as sp
import pytz
import time import time
import traceback import traceback
from functools import reduce from functools import reduce
@ -797,11 +798,12 @@ def get_recordings_storage_usage():
# return hourly summary for recordings of camera # return hourly summary for recordings of camera
@bp.route("/<camera_name>/recordings/summary") @bp.route("/<camera_name>/recordings/summary")
def recordings_summary(camera_name): def recordings_summary(camera_name):
tz_name = request.args.get("timezone", default="localtime", type=str)
recording_groups = ( recording_groups = (
Recordings.select( Recordings.select(
fn.strftime( fn.strftime(
"%Y-%m-%d %H", "%Y-%m-%d %H",
fn.datetime(Recordings.start_time, "unixepoch", "localtime"), fn.datetime(Recordings.start_time, "unixepoch", tz_name),
).alias("hour"), ).alias("hour"),
fn.SUM(Recordings.duration).alias("duration"), fn.SUM(Recordings.duration).alias("duration"),
fn.SUM(Recordings.motion).alias("motion"), fn.SUM(Recordings.motion).alias("motion"),
@ -811,13 +813,13 @@ def recordings_summary(camera_name):
.group_by( .group_by(
fn.strftime( fn.strftime(
"%Y-%m-%d %H", "%Y-%m-%d %H",
fn.datetime(Recordings.start_time, "unixepoch", "localtime"), fn.datetime(Recordings.start_time, "unixepoch", tz_name),
) )
) )
.order_by( .order_by(
fn.strftime( fn.strftime(
"%Y-%m-%d H", "%Y-%m-%d H",
fn.datetime(Recordings.start_time, "unixepoch", "localtime"), fn.datetime(Recordings.start_time, "unixepoch", tz_name),
).desc() ).desc()
) )
) )
@ -825,14 +827,14 @@ def recordings_summary(camera_name):
event_groups = ( event_groups = (
Event.select( Event.select(
fn.strftime( fn.strftime(
"%Y-%m-%d %H", fn.datetime(Event.start_time, "unixepoch", "localtime") "%Y-%m-%d %H", fn.datetime(Event.start_time, "unixepoch", tz_name)
).alias("hour"), ).alias("hour"),
fn.COUNT(Event.id).alias("count"), fn.COUNT(Event.id).alias("count"),
) )
.where(Event.camera == camera_name, Event.has_clip) .where(Event.camera == camera_name, Event.has_clip)
.group_by( .group_by(
fn.strftime( fn.strftime(
"%Y-%m-%d %H", fn.datetime(Event.start_time, "unixepoch", "localtime") "%Y-%m-%d %H", fn.datetime(Event.start_time, "unixepoch", tz_name)
), ),
) )
.objects() .objects()
@ -1018,7 +1020,10 @@ def vod_ts(camera_name, start_ts, end_ts):
@bp.route("/vod/<year_month>/<day>/<hour>/<camera_name>") @bp.route("/vod/<year_month>/<day>/<hour>/<camera_name>")
def vod_hour(year_month, day, hour, camera_name): def vod_hour(year_month, day, hour, camera_name):
start_date = datetime.strptime(f"{year_month}-{day} {hour}", "%Y-%m-%d %H") tz_name = request.args.get("timezone", "utc")
start_date = datetime.strptime(f"{year_month}-{day} {hour}", "%Y-%m-%d %H").replace(
tzinfo=pytz.timezone(tz_name)
)
end_date = start_date + timedelta(hours=1) - timedelta(milliseconds=1) end_date = start_date + timedelta(hours=1) - timedelta(milliseconds=1)
start_ts = start_date.timestamp() start_ts = start_date.timestamp()
end_ts = end_date.timestamp() end_ts = end_date.timestamp()

View File

@ -277,12 +277,20 @@ class RecordingMaintainer(threading.Thread):
self.end_time_cache.pop(cache_path, None) self.end_time_cache.pop(cache_path, None)
return return
directory = os.path.join(RECORD_DIR, start_time.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%d/%H"), camera) directory = os.path.join(
RECORD_DIR,
start_time.replace(tzinfo=datetime.timezone.utc)
.astimezone(tz=None)
.strftime("%Y-%m-%d/%H"),
camera,
)
if not os.path.exists(directory): if not os.path.exists(directory):
os.makedirs(directory) os.makedirs(directory)
file_name = f"{start_time.replace(tzinfo=datetime.timezone.utc).strftime('%M.%S.mp4')}" file_name = (
f"{start_time.replace(tzinfo=datetime.timezone.utc).strftime('%M.%S.mp4')}"
)
file_path = os.path.join(directory, file_name) file_path = os.path.join(directory, file_name)
try: try: