mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-05 10:45:21 +03:00
Refactor some stats function to properly handle zero values
This commit is contained in:
parent
ff1b5c87aa
commit
6690183a23
@ -742,11 +742,12 @@ def clean_camera_user_pass(line: str) -> str:
|
|||||||
|
|
||||||
def escape_special_characters(path: str) -> str:
|
def escape_special_characters(path: str) -> str:
|
||||||
"""Cleans reserved characters to encodings for ffmpeg."""
|
"""Cleans reserved characters to encodings for ffmpeg."""
|
||||||
try:
|
match = re.search(REGEX_RTSP_CAMERA_USER_PASS, path)
|
||||||
found = re.search(REGEX_RTSP_CAMERA_USER_PASS, path).group(0)[3:-1]
|
if match:
|
||||||
|
found = match.group(0)[3:-1]
|
||||||
pw = found[(found.index(":") + 1) :]
|
pw = found[(found.index(":") + 1) :]
|
||||||
return path.replace(pw, urllib.parse.quote_plus(pw))
|
return path.replace(pw, urllib.parse.quote_plus(pw))
|
||||||
except AttributeError:
|
else:
|
||||||
# path does not have user:pass
|
# path does not have user:pass
|
||||||
return path
|
return path
|
||||||
|
|
||||||
@ -833,16 +834,25 @@ def get_cpu_stats() -> dict[str, dict]:
|
|||||||
|
|
||||||
process_elapsed_sec = system_uptime_sec - process_starttime_sec
|
process_elapsed_sec = system_uptime_sec - process_starttime_sec
|
||||||
process_usage_sec = process_utime_sec + process_stime_sec
|
process_usage_sec = process_utime_sec + process_stime_sec
|
||||||
cpu_average_usage = process_usage_sec * 100 // process_elapsed_sec
|
cpu_average_usage = (
|
||||||
|
process_usage_sec * 100 // process_elapsed_sec
|
||||||
|
if process_elapsed_sec
|
||||||
|
else 0
|
||||||
|
)
|
||||||
|
|
||||||
with open(f"/proc/{pid}/statm", "r") as f:
|
with open(f"/proc/{pid}/statm", "r") as f:
|
||||||
mem_stats = f.readline().split()
|
mem_stats = f.readline().split()
|
||||||
|
if os.sysconf("SC_PAGE_SIZE"):
|
||||||
mem_res = int(mem_stats[1]) * os.sysconf("SC_PAGE_SIZE") / 1024
|
mem_res = int(mem_stats[1]) * os.sysconf("SC_PAGE_SIZE") / 1024
|
||||||
|
|
||||||
if docker_memlimit > 0:
|
|
||||||
mem_pct = round((mem_res / docker_memlimit) * 100, 1)
|
|
||||||
else:
|
else:
|
||||||
|
mem_res = int(mem_stats[1])
|
||||||
|
|
||||||
|
if docker_memlimit:
|
||||||
|
mem_pct = round((mem_res / docker_memlimit) * 100, 1)
|
||||||
|
elif total_mem:
|
||||||
mem_pct = round((mem_res / total_mem) * 100, 1)
|
mem_pct = round((mem_res / total_mem) * 100, 1)
|
||||||
|
else:
|
||||||
|
mem_pct = 0
|
||||||
|
|
||||||
usages[pid] = {
|
usages[pid] = {
|
||||||
"cpu": str(cpu_percent),
|
"cpu": str(cpu_percent),
|
||||||
@ -850,8 +860,8 @@ def get_cpu_stats() -> dict[str, dict]:
|
|||||||
"mem": f"{mem_pct}",
|
"mem": f"{mem_pct}",
|
||||||
"cmdline": " ".join(cmdline),
|
"cmdline": " ".join(cmdline),
|
||||||
}
|
}
|
||||||
except Exception:
|
except Exception as e:
|
||||||
continue
|
logger.debug(f"Unable to calculate cpu_stats: {e}")
|
||||||
|
|
||||||
return usages
|
return usages
|
||||||
|
|
||||||
@ -1008,7 +1018,7 @@ def get_nvidia_gpu_stats() -> dict[int, dict]:
|
|||||||
else:
|
else:
|
||||||
gpu_util = 0
|
gpu_util = 0
|
||||||
|
|
||||||
if meminfo != "N/A":
|
if meminfo != "N/A" and meminfo.total:
|
||||||
gpu_mem_util = meminfo.used / meminfo.total * 100
|
gpu_mem_util = meminfo.used / meminfo.total * 100
|
||||||
else:
|
else:
|
||||||
gpu_mem_util = -1
|
gpu_mem_util = -1
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user