Skip to content

Commit

Permalink
Add list of loaded plugins to /plugins page
Browse files Browse the repository at this point in the history
  • Loading branch information
Wrench56 committed Jul 15, 2024
1 parent fe87d82 commit 9905736
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/backend/plugins/handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Tuple, Optional
from typing import Any, Dict, List, Optional, Tuple

import importlib
import logging
Expand Down Expand Up @@ -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
10 changes: 6 additions & 4 deletions src/backend/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -210,14 +211,15 @@ 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


@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()})
4 changes: 4 additions & 0 deletions src/frontend/src/lib/components/plugins/Error.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script lang="ts">
/* TODO: Implement this */
export let error: string;
</script>
27 changes: 27 additions & 0 deletions src/frontend/src/lib/components/plugins/Plugin.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts">
import type { PluginStatus } from "../shared/api/plugins.type";
export let data: PluginStatus;
</script>

{#if data.status}
<pre class="online">[<span style="color: rgb(0, 255, 0);">ONLINE</span
>] {data.name}</pre>
{:else}
<pre class="offline"><span style="color: red;">OFFLINE</span
>] {data.name}</pre>
{/if}

<style>
pre {
white-space: pre;
font-family: monaco, Consolas, "Lucida Console", monospace;
color: whitesmoke;
font-size: 16px;
margin: 0px;
}
span {
font-family: monaco, Consolas, "Lucida Console", monospace;
}
</style>
35 changes: 35 additions & 0 deletions src/frontend/src/lib/components/plugins/PluginsContainer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script lang="ts">
import { onMount } from "svelte";
import type { PluginStatus } from "../shared/api/plugins.type";
import Error from "./Error.svelte";
import Plugin from "./Plugin.svelte";
import Separator from "../shared/Separator.svelte";
let plugins: Array<PluginStatus> = [];
let error: string;
onMount(() => {
fetch("/plugins/status")
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.ok == true) {
plugins = responseJson.plugins;
} else {
plugins = [];
error = responseJson.error;
}
});
});
</script>


<Separator />
{#if plugins.length == 0}
<Error {error} />
{/if}
{#each plugins as plugin}
<Plugin data={plugin} />
{/each}

<style>
</style>
4 changes: 4 additions & 0 deletions src/frontend/src/lib/components/shared/api/plugins.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type PluginStatus = {
name: string;
status: boolean;
};
2 changes: 2 additions & 0 deletions src/frontend/src/routes/plugins/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import Box from "$lib/components/plugins/Box.svelte";
import Header from "$lib/components/plugins/Header.svelte";
import PluginsContainer from "$lib/components/plugins/PluginsContainer.svelte";
import UrlField from "$lib/components/plugins/UrlField.svelte";
import Separator from "$lib/components/shared/Separator.svelte";
import Statusbar from "$lib/components/shared/statusbar/Statusbar.svelte";
Expand All @@ -11,6 +12,7 @@
<Header />
<Separator />
<UrlField />
<PluginsContainer />
</Box>

<DNotificationContainer />
Expand Down

0 comments on commit 9905736

Please sign in to comment.