Skip to content

Commit

Permalink
speed-up config retrievals
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Mar 12, 2024
1 parent c3f2b0c commit cb3f902
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
17 changes: 13 additions & 4 deletions music_assistant/server/controllers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,15 @@ async def get_provider_configs(
self,
provider_type: ProviderType | None = None,
provider_domain: str | None = None,
include_values: bool = False,
) -> list[ProviderConfig]:
"""Return all known provider configurations, optionally filtered by ProviderType."""
raw_values: dict[str, dict] = self.get(CONF_PROVIDERS, {})
prov_entries = {x.domain for x in self.mass.get_provider_manifests()}
return [
await self.get_provider_config(prov_conf["instance_id"])
if include_values
else ProviderConfig.parse([], prov_conf)
for prov_conf in raw_values.values()
if (provider_type is None or prov_conf["type"] == provider_type)
and (provider_domain is None or prov_conf["domain"] == provider_domain)
Expand Down Expand Up @@ -317,11 +320,15 @@ async def reload_provider(self, instance_id: str) -> None:
await self._load_provider_config(config)

@api_command("config/players")
async def get_player_configs(self, provider: str | None = None) -> list[PlayerConfig]:
async def get_player_configs(
self, provider: str | None = None, include_values: bool = False
) -> list[PlayerConfig]:
"""Return all known player configurations, optionally filtered by provider domain."""
available_providers = {x.instance_id for x in self.mass.providers}
return [
await self.get_player_config(raw_conf["player_id"])
if include_values
else PlayerConfig.parse([], raw_conf)
for raw_conf in list(self.get(CONF_PLAYERS, {}).values())
# filter out unavailable providers
if raw_conf["provider"] in available_providers
Expand Down Expand Up @@ -501,12 +508,14 @@ async def create_default_provider_config(self, provider_domain: str) -> None:
self.set(conf_key, default_config.to_raw())

@api_command("config/core")
async def get_core_configs(
self,
) -> list[CoreConfig]:
async def get_core_configs(self, include_values: bool = False) -> list[CoreConfig]:
"""Return all core controllers config options."""
return [
await self.get_core_config(core_controller)
if include_values
else CoreConfig.parse(
[], self.get(f"{CONF_CORE}/{core_controller}", {"domain": core_controller})
)
for core_controller in CONFIGURABLE_CORE_CONTROLLERS
]

Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ async def _sync_syncgroup(self, player_id: str) -> None:

async def _register_syncgroups(self) -> None:
"""Register all (virtual/fake) syncgroup players."""
player_configs = await self.mass.config.get_player_configs()
player_configs = await self.mass.config.get_player_configs(include_values=True)
for player_config in player_configs:
if not player_config.player_id.startswith(SYNCGROUP_PREFIX):
continue
Expand Down
4 changes: 3 additions & 1 deletion music_assistant/server/providers/ugp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ async def create_group(self, name: str, members: list[str]) -> Player:

async def _register_all_players(self) -> None:
"""Register all (virtual/fake) group players in the Player controller."""
player_configs = await self.mass.config.get_player_configs(self.instance_id)
player_configs = await self.mass.config.get_player_configs(
self.instance_id, include_values=True
)
for player_config in player_configs:
members = player_config.get_value(CONF_GROUP_MEMBERS)
self._register_group_player(
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ async def load_provider(prov_conf: ProviderConfig) -> None:
self.config.set(f"{CONF_PROVIDERS}/{prov_conf.instance_id}/last_error", str(exc))

# load all configured (and enabled) providers
prov_configs = await self.config.get_provider_configs()
prov_configs = await self.config.get_provider_configs(include_values=True)
async with asyncio.TaskGroup() as tg:
for prov_conf in prov_configs:
if not prov_conf.enabled:
Expand Down

0 comments on commit cb3f902

Please sign in to comment.