Using median CPU temperature.

This commit is contained in:
Roland 2023-08-30 14:25:33 -06:00
parent da6d6a8094
commit d3074eb3d8
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 14 additions and 8 deletions

View file

@ -114,7 +114,7 @@ def test_get_video_for_app(test_session, simple_channel, simple_video):
@pytest.mark.asyncio
def test_video_delete(test_client, test_session, test_directory, channel_factory, video_factory):
async def test_video_delete(test_client, test_session, test_directory, channel_factory, video_factory):
"""Video.delete() removes the video's files, but leave the DB record."""
channel1, channel2 = channel_factory(), channel_factory()
vid1 = video_factory(channel_id=channel1.id, with_video_file=True, with_caption_file=True)

View file

@ -3,6 +3,7 @@ import asyncio
import multiprocessing
import pathlib
import re
import statistics
import subprocess
from dataclasses import dataclass
from decimal import Decimal
@ -99,10 +100,11 @@ def get_cpu_info_psutil() -> CPUInfo:
# Prefer "coretemp", fallback to the first temperature.
temp = psutil.sensors_temperatures()
name = 'coretemp' if 'coretemp' in temp else list(temp.keys())[0]
# Temperatures may be None.
temperature = int(temp.get(name)[0].current or 0)
high_temperature = int(temp.get(name)[0].high or 0)
critical_temperature = int(temp.get(name)[0].critical or 0)
# Temperatures may be None. Get average temperatures from CPU because one core may be hot.
temperatures = temp.get(name)
temperature = statistics.median([i.current or 0 for i in temperatures])
high_temperature = statistics.median([i.high or 0 for i in temperatures])
critical_temperature = statistics.median([i.critical or 0 for i in temperatures])
# Temperatures my not exist.
if not high_temperature:
@ -120,9 +122,9 @@ def get_cpu_info_psutil() -> CPUInfo:
min_frequency=int(cpu_freq.min),
max_frequency=int(cpu_freq.max),
percent=int(percent),
temperature=temperature,
high_temperature=high_temperature,
critical_temperature=critical_temperature,
temperature=int(temperature),
high_temperature=int(high_temperature),
critical_temperature=int(critical_temperature),
)
return info

View file

@ -23,6 +23,9 @@ async def test_get_cpu_info():
info = await status.get_cpu_info()
assert isinstance(info, status.CPUInfo)
assert isinstance(info.percent, int)
assert isinstance(info.temperature, int)
assert isinstance(info.high_temperature, int)
assert isinstance(info.critical_temperature, int)
with mock.patch('wrolpi.status.get_cpu_info_psutil') as mock_get_cpu_info_psutil:
# Fallback to subprocess when psutil is not available.
@ -30,6 +33,7 @@ async def test_get_cpu_info():
info = await status.get_cpu_info()
assert isinstance(info, status.CPUInfo)
assert isinstance(info.percent, int)
assert info.temperature >= 0
@pytest.mark.asyncio