create helper function and apply to routes

This commit is contained in:
chasa 2023-09-25 22:49:10 +01:00
commit 79ebe41385
2 changed files with 35 additions and 7 deletions

View file

@ -1793,3 +1793,24 @@ def get_ipv6_network_address(ip: str) -> str:
if cidr_pattern.match(plexpy.CONFIG.NOTIFY_CONCURRENT_IPV6_CIDR): if cidr_pattern.match(plexpy.CONFIG.NOTIFY_CONCURRENT_IPV6_CIDR):
cidr = plexpy.CONFIG.NOTIFY_CONCURRENT_IPV6_CIDR cidr = plexpy.CONFIG.NOTIFY_CONCURRENT_IPV6_CIDR
return str(ip_network(ip+cidr, strict=False).network_address) return str(ip_network(ip+cidr, strict=False).network_address)
def clean_plexapi_sessions(session: dict) -> dict:
"Cleans Plex session data, removing the 10Gbps bandwidth/0.064Mbps quality bug that appears when video transcoding sometimes."
if isinstance(session, dict):
# Reliable way to identify if a session is bugged.
if session['bandwidth'] == "10000000" and session['quality_profile'] == '0.064 Mbps':
# Bitrate is unaffected by the bug, use bitrate to estimate the bandwidth reserve.
session['bandwidth'] = str(int(int(session['bitrate']) * 1.5))
# Convert bitrate to a human readable quality profile.
if int(session['bitrate']) > 1000000:
session['quality_profile'] = f"{int(session['bitrate']) / 1000000:.1f} Gbps"
elif int(session['bitrate']) > 1000:
session['quality_profile'] = f"{int(session['bitrate']) / 1000:.1f} Mbps"
else:
session['quality_profile'] = f"{int(session['bitrate'])} kbps"
return session

View file

@ -352,7 +352,7 @@ class WebInterface(object):
if result: if result:
session = next((s for s in result['sessions'] if s['session_key'] == session_key), None) session = next((s for s in result['sessions'] if s['session_key'] == session_key), None)
return serve_template(template_name="current_activity_instance.html", session=session) return serve_template(template_name="current_activity_instance.html", session=helpers.clean_plexapi_sessions(session))
else: else:
return serve_template(template_name="current_activity_instance.html", session=None) return serve_template(template_name="current_activity_instance.html", session=None)
@ -6049,19 +6049,26 @@ class WebInterface(object):
'lan_bandwidth': 0, 'lan_bandwidth': 0,
'wan_bandwidth': 0} 'wan_bandwidth': 0}
cleaned_sessions = []
for s in result['sessions']: for s in result['sessions']:
if s['transcode_decision'] == 'transcode': s_cleaned = helpers.clean_plexapi_sessions(s)
cleaned_sessions.append(s_cleaned)
if s_cleaned['transcode_decision'] == 'transcode':
counts['stream_count_transcode'] += 1 counts['stream_count_transcode'] += 1
elif s['transcode_decision'] == 'copy': elif s_cleaned['transcode_decision'] == 'copy':
counts['stream_count_direct_stream'] += 1 counts['stream_count_direct_stream'] += 1
else: else:
counts['stream_count_direct_play'] += 1 counts['stream_count_direct_play'] += 1
counts['total_bandwidth'] += helpers.cast_to_int(s['bandwidth']) counts['total_bandwidth'] += helpers.cast_to_int(s_cleaned['bandwidth'])
if s['location'] == 'lan': if s_cleaned['location'] == 'lan':
counts['lan_bandwidth'] += helpers.cast_to_int(s['bandwidth']) counts['lan_bandwidth'] += helpers.cast_to_int(s_cleaned['bandwidth'])
else: else:
counts['wan_bandwidth'] += helpers.cast_to_int(s['bandwidth']) counts['wan_bandwidth'] += helpers.cast_to_int(s_cleaned['bandwidth'])
result['sessions'] = cleaned_sessions
result.update(counts) result.update(counts)