Skip to content

Commit

Permalink
[feat] Add optional configuration for thousands separator for library…
Browse files Browse the repository at this point in the history
… numbers (#132)
  • Loading branch information
nwithan8 authored Sep 3, 2023
1 parent 3c6786f commit 39d51fa
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ You will need to set the following environment variables:
| TC_HIDE_PROGRESS | No | Whether to hide stream progress in the streams view | "False" |
| TC_HIDE_ETA | No | Whether to hide stream ETAs in the streams view | "False" | | |
| TC_USE_FRIENDLY_NAMES | No | Use friendly names rather than usernames if available | "False" |
| TC_THOUSANDS_SEPARATOR | No | Symbol to separate thousands in numbers | "" |
| TC_VC_STATS_CATEGORY_NAME | No | Name of the stats voice channel category | "Tautulli Stats" |
| TC_VC_STREAM_COUNT | No | Whether to display current stream count in a voice channel | "False" |
| TC_VC_STREAM_COUNT_CHANNEL_ID | No | Optional ID of the Discord voice channel to display stream count (see [Voice Channels](#voice-channels)) | 0 |
Expand Down
1 change: 1 addition & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Tautulli:
HideProgress: false
HideETA: false
UseFriendlyNames: false
ThousandsSeparator: ""

Discord:
Connection:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
TC_HIDE_PROGRESS: "False"
TC_HIDE_ETA: "False"
TC_USE_FRIENDLY_NAMES: "False"
TC_THOUSANDS_SEPARATOR: ""
TC_VC_STATS_CATEGORY_NAME: "Plex Stream Stats"
TC_VC_STREAM_COUNT: "False"
TC_VC_STREAM_COUNT_CHANNEL_ID: 0
Expand Down
12 changes: 9 additions & 3 deletions modules/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ def _use_friendly_names(self) -> str:
return self._customization._get_value(key='UseFriendlyNames', default=False,
env_name_override="TC_USE_FRIENDLY_NAMES")

@property
def thousands_separator(self) -> str:
return self._customization._get_value(key='ThousandsSeparator', default="",
env_name_override="TC_THOUSANDS_SEPARATOR")

@property
def _performance_voice_channel_settings(self) -> ConfigSection:
return self._voice_channels._get_subsection(key="Performance")
Expand Down Expand Up @@ -475,9 +480,9 @@ def __init__(self, app_name: str, config_path: str, fallback_to_env: bool = True
self.pull_from_env = True
logging.debug(f"Config file not found: {config_path}, falling back to environment variables")

self.tautulli = TautulliConfig(self.config, self.pull_from_env)
self.discord = DiscordConfig(self.config, self.pull_from_env)
self.extras = ExtrasConfig(self.config, self.pull_from_env)
self.tautulli = TautulliConfig(self.config, pull_from_env=self.pull_from_env)
self.discord = DiscordConfig(self.config, pull_from_env=self.pull_from_env)
self.extras = ExtrasConfig(self.config, pull_from_env=self.pull_from_env)
self.performance = {
statics.KEY_PERFORMANCE_CATEGORY_NAME: self.tautulli._performance_voice_channel_name,
statics.KEY_PERFORMANCE_MONITOR_CPU: self.extras._performance_monitor_cpu,
Expand Down Expand Up @@ -533,6 +538,7 @@ def all(self) -> dict:
"Tautulli - Customization - Anonymize - Hide Progress": self.tautulli._anonymize_hide_progress,
"Tautulli - Customization - Anonymize - Hide ETA": self.tautulli._anonymize_hide_eta,
"Tautulli - Customization - Use Friendly Names": self.tautulli._use_friendly_names,
"Tautulli - Customization - Thousands Separator": self.tautulli.thousands_separator,
"Tautulli - Customization - Voice Channels - Performance - Voice Channel Category Name": self.tautulli._performance_voice_channel_name,
"Discord - Connection - Bot Token": "Exists" if self.discord.bot_token else "Not Set",
"Discord - Connection - Server ID": self.discord.server_id,
Expand Down
11 changes: 7 additions & 4 deletions modules/discord_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from modules.emojis import EmojiManager
from modules.settings_transports import LibraryVoiceChannelsVisibilities
from modules.tautulli_connector import TautulliConnector, TautulliDataResponse
from modules.utils import quote
from modules.utils import quote, format_thousands


async def add_emoji_reactions(message: discord.Message, count: int, emoji_manager: EmojiManager):
Expand Down Expand Up @@ -192,7 +192,8 @@ def __init__(self,
display_library_stats: bool,
nitro: bool,
performance_monitoring: dict,
analytics):
analytics,
thousands_separator: str = ""):
self.token = token
self.guild_id = guild_id
self.nitro: bool = nitro
Expand All @@ -204,6 +205,7 @@ def __init__(self,
self.voice_channel_settings = voice_channel_settings
self.display_live_stats = display_live_stats
self.display_library_stats = display_library_stats
self.thousands_separator = thousands_separator
self.tautulli_channel: discord.TextChannel = None
self.tautulli_stats_voice_category: discord.CategoryChannel = None
self.tautulli_libraries_voice_category: discord.CategoryChannel = None
Expand Down Expand Up @@ -600,6 +602,7 @@ async def update_library_stats_voice_channels(self) -> None:
for stat in stats:
stat_emoji = stat[0] if self.voice_channel_settings.get(statics.KEY_USE_EMOJIS, True) else None
stat_value = stat[1]
stat_value = format_thousands(number=stat_value, delimiter=self.thousands_separator)
channel_name = f"{library_name}"
if stat_emoji:
channel_name = f"{stat_emoji} {channel_name}"
Expand All @@ -611,14 +614,14 @@ async def update_library_stats_voice_channels(self) -> None:
async def update_performance_voice_channels(self) -> None:
logging.info("Updating performance stats...")
if self.performance_monitoring.get(statics.KEY_PERFORMANCE_MONITOR_CPU, False):
cpu_percent = f"{utils.format_number(system_stats.cpu_usage())}%"
cpu_percent = f"{utils.format_fraction(system_stats.cpu_usage())}%"
logging.info(f"Updating CPU voice channel with new CPU percent: {cpu_percent}")
await self.edit_stat_voice_channel(channel_name="CPU",
stat=cpu_percent,
category=self.performance_voice_category)

if self.performance_monitoring.get(statics.KEY_PERFORMANCE_MONITOR_MEMORY, False):
memory_percent = f"{utils.format_number(system_stats.ram_usage())} GB ({utils.format_number(system_stats.ram_usage_percentage())}%)"
memory_percent = f"{utils.format_fraction(system_stats.ram_usage())} GB ({utils.format_fraction(system_stats.ram_usage_percentage())}%)"
logging.info(f"Updating Memory voice channel with new Memory percent: {memory_percent}")
await self.edit_stat_voice_channel(channel_name="Memory",
stat=memory_percent,
Expand Down
9 changes: 7 additions & 2 deletions modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ def make_plural(word, count: int, suffix_override: str = 's') -> str:
def quote(string: str) -> str:
return f"\"{string}\""

def format_number(number: float, denominator: int = 1, decimal_places: int = 1) -> str:
def format_fraction(number: float, denominator: int = 1, decimal_places: int = 1) -> str:
if decimal_places <= 0:
return f'{int(number / denominator):d}'
else:
return f'{float(number / denominator):.{decimal_places}f}'

def format_thousands(number: int, delimiter: str = "") -> str:
if number < 1000:
return str(number)
return f"{format_thousands(number // 1000, delimiter)}{delimiter}{number % 1000:03d}"

def human_bitrate(_bytes, decimal_places: int = 1) -> str:
# Return the given bitrate as a human friendly bps, Kbps, Mbps, Gbps, or Tbps string

Expand All @@ -42,7 +47,7 @@ def human_bitrate(_bytes, decimal_places: int = 1) -> str:
denominator = TB
letter = "T"

value = format_number(number=_bytes, denominator=denominator, decimal_places=decimal_places)
value = format_fraction(number=_bytes, denominator=denominator, decimal_places=decimal_places)
return f"{value} {letter}bps"


Expand Down
1 change: 1 addition & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
voice_channel_settings=config.tautulli.voice_channel_settings,
display_live_stats=config.tautulli.any_live_stats_channels_enabled,
display_library_stats=config.tautulli.any_library_stats_channels_enabled,
thousands_separator=config.tautulli.thousands_separator,
nitro=config.discord.has_discord_nitro,
performance_monitoring=config.performance,
analytics=analytics,
Expand Down
1 change: 1 addition & 0 deletions templates/tauticord.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Config Name="Hide stream progress" Target="TC_HIDE_PROGRESS" Default="False" Description="Whether to hide stream progress in the streams view" Type="Variable" Display="always" Required="false" Mask="false">False</Config>
<Config Name="Hide stream ETAs" Target="TC_HIDE_ETA" Default="False" Description="Whether to hide stream ETAs in the streams view" Type="Variable" Display="always" Required="false" Mask="false">False</Config>
<Config Name="Use friendly names" Target="TC_USE_FRIENDLY_NAMES" Default="False" Description="Use friendly names instead of usernames if available" Type="Variable" Display="always" Required="false" Mask="false">False</Config>
<Config Name="Thousands separator" Target="TC_THOUSANDS_SEPARATOR" Default="," Description="Symbol to separate thousands in numbers" Type="Variable" Display="always" Required="false" Mask="false">,</Config>
<Config Name="Stream stats category name" Target="TC_VC_STATS_CATEGORY_NAME" Default="Tautulli Stats" Description="Name of the stream stats voice channel category" Type="Variable" Display="always" Required="false" Mask="false">Tautulli Stats</Config>
<Config Name="Display stream count" Target="TC_VC_STREAM_COUNT" Default="False" Description="Whether to display stream count in voice channels" Type="Variable" Display="always" Required="false" Mask="false">False</Config>
<Config Name="Stream count voice channel ID" Target="TC_VC_STREAM_COUNT_CHANNEL_ID" Default="0" Description="Optional ID of the Discord voice channel to display stream count" Type="Variable" Display="always" Required="false" Mask="false">0</Config>
Expand Down

0 comments on commit 39d51fa

Please sign in to comment.