From 99057364077c18da26b3df681ab6a0888bd49bbb Mon Sep 17 00:00:00 2001 From: Wrench56 Date: Mon, 15 Jul 2024 04:08:00 -0400 Subject: [PATCH] Add list of loaded plugins to `/plugins` page --- src/backend/plugins/handler.py | 13 ++++++- src/backend/server/main.py | 10 +++--- .../src/lib/components/plugins/Error.svelte | 4 +++ .../src/lib/components/plugins/Plugin.svelte | 27 ++++++++++++++ .../plugins/PluginsContainer.svelte | 35 +++++++++++++++++++ .../lib/components/shared/api/plugins.type.ts | 4 +++ src/frontend/src/routes/plugins/+page.svelte | 2 ++ 7 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/frontend/src/lib/components/plugins/Error.svelte create mode 100644 src/frontend/src/lib/components/plugins/Plugin.svelte create mode 100644 src/frontend/src/lib/components/plugins/PluginsContainer.svelte create mode 100644 src/frontend/src/lib/components/shared/api/plugins.type.ts diff --git a/src/backend/plugins/handler.py b/src/backend/plugins/handler.py index 96e16b5..892fbe9 100644 --- a/src/backend/plugins/handler.py +++ b/src/backend/plugins/handler.py @@ -1,4 +1,4 @@ -from typing import Dict, Tuple, Optional +from typing import Any, Dict, List, Optional, Tuple import importlib import logging @@ -78,3 +78,14 @@ def unload_all() -> bool: def get_plugin_names() -> Tuple[str, ...]: return tuple(_PLUGINS.keys()) + + +def get_plugin_statuses() -> List[Dict[str, Any]]: + result = [] + for name, _ in priority.fetch_plugins(reload=False): + if name in _PLUGINS: + result.append({'name': name, 'status': True}) + continue + result.append({'name': name, 'status': False}) + + return result diff --git a/src/backend/server/main.py b/src/backend/server/main.py index 51fa417..37efa25 100644 --- a/src/backend/server/main.py +++ b/src/backend/server/main.py @@ -182,7 +182,8 @@ async def plugins(request: Request, plugin: str, endpoint: str) -> Any: return_value = await callback(endpoint, request) else: # Sync function detected - logging.error(f'Plugin "{plugin}" uses synchronous functions, request blocked') + logging.error( + f'Plugin "{plugin}" uses synchronous functions, request blocked') response.status_code = 503 if return_value is not None: return return_value @@ -210,7 +211,8 @@ async def ws_plugins(websocket: WebSocket, plugin: str, endpoint: str) -> Any: await callback(endpoint, websocket) else: # Sync function detected - logging.error(f'Plugin "{plugin}" uses synchronous functions, request blocked') + logging.error( + f'Plugin "{plugin}" uses synchronous functions, request blocked') response.status_code = 503 return response @@ -218,6 +220,6 @@ async def ws_plugins(websocket: WebSocket, plugin: str, endpoint: str) -> Any: @app.get('/plugins/status', response_class=ORJSONResponse) async def plugin_status(request: Request) -> ORJSONResponse: if not database.uuid_exists(request.cookies.get('auth_cookie')): - return ORJSONResponse({'ok': False}) + return ORJSONResponse({'ok': False, 'error': 'Auth failed'}) - return ORJSONResponse({'ok': True, 'plugins': handler.get_plugin_names()}) + return ORJSONResponse({'ok': True, 'plugins': handler.get_plugin_statuses()}) diff --git a/src/frontend/src/lib/components/plugins/Error.svelte b/src/frontend/src/lib/components/plugins/Error.svelte new file mode 100644 index 0000000..e8b10eb --- /dev/null +++ b/src/frontend/src/lib/components/plugins/Error.svelte @@ -0,0 +1,4 @@ + diff --git a/src/frontend/src/lib/components/plugins/Plugin.svelte b/src/frontend/src/lib/components/plugins/Plugin.svelte new file mode 100644 index 0000000..ba3fa6b --- /dev/null +++ b/src/frontend/src/lib/components/plugins/Plugin.svelte @@ -0,0 +1,27 @@ + + +{#if data.status} +
[ONLINE]   {data.name}
+{:else} +
OFFLINE]    {data.name}
+{/if} + + diff --git a/src/frontend/src/lib/components/plugins/PluginsContainer.svelte b/src/frontend/src/lib/components/plugins/PluginsContainer.svelte new file mode 100644 index 0000000..4486afe --- /dev/null +++ b/src/frontend/src/lib/components/plugins/PluginsContainer.svelte @@ -0,0 +1,35 @@ + + + + +{#if plugins.length == 0} + +{/if} +{#each plugins as plugin} + +{/each} + + diff --git a/src/frontend/src/lib/components/shared/api/plugins.type.ts b/src/frontend/src/lib/components/shared/api/plugins.type.ts new file mode 100644 index 0000000..09d8696 --- /dev/null +++ b/src/frontend/src/lib/components/shared/api/plugins.type.ts @@ -0,0 +1,4 @@ +export type PluginStatus = { + name: string; + status: boolean; +}; diff --git a/src/frontend/src/routes/plugins/+page.svelte b/src/frontend/src/routes/plugins/+page.svelte index 8d126b5..a14a6b3 100644 --- a/src/frontend/src/routes/plugins/+page.svelte +++ b/src/frontend/src/routes/plugins/+page.svelte @@ -1,6 +1,7 @@