diff --git a/Packages/api-client/src/index.ts b/Packages/api-client/src/index.ts index f9b4939..b055f2f 100644 --- a/Packages/api-client/src/index.ts +++ b/Packages/api-client/src/index.ts @@ -39,6 +39,10 @@ export type Attachment = Schemas["Attachment"]; export type SearchHit = Schemas["SearchHit"]; export type ShareLink = Schemas["ShareLink"]; export type Comment = Schemas["Comment"]; +export type AdminStats = Schemas["AdminStats"]; +export type AdminHealth = Schemas["AdminHealth"]; +export type Job = Schemas["Job"]; +export type Backup = Schemas["Backup"]; export type SharedPage = Schemas["SharedPage"]; export type SearchResult = Schemas["SearchResult"]; export type GroupRequest = Schemas["GroupRequest"]; @@ -200,7 +204,18 @@ export function createClient(opts: ClientOptions) { update: (patch: InstanceSettingsUpdate) => request("PATCH", "/api/v1/admin/settings", patch), testEmail: (to: string) => post("/api/v1/admin/settings/test-email", { to }), testStorage: () => post("/api/v1/admin/settings/test-storage"), - reindexSearch: () => post("/api/v1/admin/search/reindex"), + }, + + /** Instance administration (the admin panel). */ + admin: { + stats: () => request("GET", "/api/v1/admin/stats"), + health: () => request("GET", "/api/v1/admin/health"), + jobs: () => request("GET", "/api/v1/admin/jobs"), + runJob: (name: string) => post(`/api/v1/admin/jobs/${encodeURIComponent(name)}/run`), + backups: () => request("GET", "/api/v1/admin/backups"), + createBackup: () => post("/api/v1/admin/backups"), + deleteBackup: (key: string) => request("DELETE", `/api/v1/admin/backups/${encodeURIComponent(key)}`), + downloadBackup: (key: string) => download(`/api/v1/admin/backups/${encodeURIComponent(key)}`), }, users: { diff --git a/Packages/api-client/src/schema.d.ts b/Packages/api-client/src/schema.d.ts index 3d5fd54..ae6d189 100644 --- a/Packages/api-client/src/schema.d.ts +++ b/Packages/api-client/src/schema.d.ts @@ -1146,23 +1146,6 @@ export interface paths { patch?: never; trace?: never; }; - "/api/v1/admin/search/reindex": { - parameters: { - query?: never; - header?: never; - path?: never; - cookie?: never; - }; - get?: never; - put?: never; - /** Rebuild the search index from the database (superadmin) */ - post: operations["reindexSearch"]; - delete?: never; - options?: never; - head?: never; - patch?: never; - trace?: never; - }; "/api/v1/pages/{id}/export": { parameters: { query?: never; @@ -1348,6 +1331,117 @@ export interface paths { patch?: never; trace?: never; }; + "/api/v1/admin/stats": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** Dashboard numbers (superadmin - the instance; admin - their organization) */ + get: operations["getAdminStats"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/admin/health": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** Version, uptime and database health (superadmin) */ + get: operations["getAdminHealth"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/admin/jobs": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** Background jobs and how their last run went (superadmin) */ + get: operations["listJobs"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/admin/jobs/{name}/run": { + parameters: { + query?: never; + header?: never; + path: { + name: string; + }; + cookie?: never; + }; + get?: never; + put?: never; + /** Start a runnable job now (superadmin) */ + post: operations["runJob"]; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/admin/backups": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** Database backups kept by docwell-db (superadmin) */ + get: operations["listBackups"]; + put?: never; + /** + * Write a new backup of the database and its files (superadmin) + * @description Answers when the archive is complete, which can take a while on large instances. + */ + post: operations["createBackup"]; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/admin/backups/{key}": { + parameters: { + query?: never; + header?: never; + path: { + key: string; + }; + cookie?: never; + }; + /** Download a backup archive (superadmin) */ + get: operations["downloadBackup"]; + put?: never; + post?: never; + /** Delete a backup archive (superadmin) */ + delete: operations["deleteBackup"]; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; } export type webhooks = Record; export interface components { @@ -1761,6 +1855,60 @@ export interface components { created: string; canDelete: boolean; }; + AdminStats: { + /** @enum {string} */ + scope: "instance" | "organization"; + users: number; + organizations: number; + workspaces: number; + spaces: number; + /** @description Pages outside the trash */ + pages: number; + attachments: number; + /** @description Total size of attachments */ + storageBytes: number; + /** @description Pages open for editing now (instance scope only) */ + rooms: number; + /** @description People connected to those pages (instance scope only) */ + editors: number; + /** @description Pages in the search index (instance scope only) */ + indexed: number; + }; + AdminHealth: { + version: string; + goVersion: string; + /** Format: date-time */ + started: string; + uptimeSeconds: number; + database: { + ok: boolean; + latencyMs: number; + error?: string; + }; + goroutines: number; + memoryMb: number; + }; + Job: { + name: string; + description: string; + /** @description How often it runs on its own (Go duration); empty when only on demand */ + every: string; + /** @description Can be started by hand */ + runnable: boolean; + running: boolean; + /** @description Runs since the server started */ + runs: number; + failures: number; + /** @description RFC 3339; empty before the first run */ + lastStarted: string; + lastDurationMs: number; + lastError: string; + }; + Backup: { + key: string; + size: number; + modified: string; + }; }; responses: { /** @description Signed in */ @@ -3837,25 +3985,6 @@ export interface operations { 404: components["responses"]["Error"]; }; }; - reindexSearch: { - parameters: { - query?: never; - header?: never; - path?: never; - cookie?: never; - }; - requestBody?: never; - responses: { - /** @description Started */ - 202: { - headers: { - [name: string]: unknown; - }; - content?: never; - }; - 403: components["responses"]["Error"]; - }; - }; exportPage: { parameters: { query?: { @@ -4171,4 +4300,177 @@ export interface operations { 404: components["responses"]["Error"]; }; }; + getAdminStats: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["AdminStats"]; + }; + }; + 403: components["responses"]["Error"]; + }; + }; + getAdminHealth: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["AdminHealth"]; + }; + }; + 403: components["responses"]["Error"]; + }; + }; + listJobs: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["Job"][]; + }; + }; + 403: components["responses"]["Error"]; + }; + }; + runJob: { + parameters: { + query?: never; + header?: never; + path: { + name: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Started */ + 202: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + 403: components["responses"]["Error"]; + 404: components["responses"]["Error"]; + 409: components["responses"]["Error"]; + }; + }; + listBackups: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["Backup"][]; + }; + }; + 403: components["responses"]["Error"]; + }; + }; + createBackup: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Created */ + 204: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + 400: components["responses"]["Error"]; + 403: components["responses"]["Error"]; + }; + }; + downloadBackup: { + parameters: { + query?: never; + header?: never; + path: { + key: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description The archive */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/zip": string; + }; + }; + 403: components["responses"]["Error"]; + 404: components["responses"]["Error"]; + }; + }; + deleteBackup: { + parameters: { + query?: never; + header?: never; + path: { + key: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Deleted */ + 204: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + 403: components["responses"]["Error"]; + 404: components["responses"]["Error"]; + }; + }; } diff --git a/Phone-App/lib/api/generated/models.dart b/Phone-App/lib/api/generated/models.dart index c0a5664..57e5e6f 100644 --- a/Phone-App/lib/api/generated/models.dart +++ b/Phone-App/lib/api/generated/models.dart @@ -1868,3 +1868,251 @@ class Comment { 'canDelete': canDelete, }; } + +enum AdminStatsScope { + instance('instance'), + organization('organization'); + + const AdminStatsScope(this.value); + final String value; + + static AdminStatsScope fromJson(String value) => values.firstWhere( + (e) => e.value == value, + orElse: () => throw FormatException('unknown AdminStatsScope', value), + ); + + String toJson() => value; +} + +class AdminStats { + const AdminStats({ + required this.scope, + required this.users, + required this.organizations, + required this.workspaces, + required this.spaces, + required this.pages, + required this.attachments, + required this.storageBytes, + required this.rooms, + required this.editors, + required this.indexed, + }); + + factory AdminStats.fromJson(Map json) => AdminStats( + scope: AdminStatsScope.fromJson(json['scope'] as String), + users: (json['users'] as num).toInt(), + organizations: (json['organizations'] as num).toInt(), + workspaces: (json['workspaces'] as num).toInt(), + spaces: (json['spaces'] as num).toInt(), + pages: (json['pages'] as num).toInt(), + attachments: (json['attachments'] as num).toInt(), + storageBytes: (json['storageBytes'] as num).toInt(), + rooms: (json['rooms'] as num).toInt(), + editors: (json['editors'] as num).toInt(), + indexed: (json['indexed'] as num).toInt(), + ); + + final AdminStatsScope scope; + + final int users; + + final int organizations; + + final int workspaces; + + final int spaces; + + /// Pages outside the trash + final int pages; + + final int attachments; + + /// Total size of attachments + final int storageBytes; + + /// Pages open for editing now (instance scope only) + final int rooms; + + /// People connected to those pages (instance scope only) + final int editors; + + /// Pages in the search index (instance scope only) + final int indexed; + + Map toJson() => { + 'scope': scope.toJson(), + 'users': users, + 'organizations': organizations, + 'workspaces': workspaces, + 'spaces': spaces, + 'pages': pages, + 'attachments': attachments, + 'storageBytes': storageBytes, + 'rooms': rooms, + 'editors': editors, + 'indexed': indexed, + }; +} + +class AdminHealthDatabase { + const AdminHealthDatabase({ + required this.ok, + required this.latencyMs, + this.error, + }); + + factory AdminHealthDatabase.fromJson(Map json) => + AdminHealthDatabase( + ok: json['ok'] as bool, + latencyMs: (json['latencyMs'] as num).toInt(), + error: json['error'] == null ? null : json['error'] as String, + ); + + final bool ok; + + final int latencyMs; + + final String? error; + + Map toJson() => { + 'ok': ok, + 'latencyMs': latencyMs, + 'error': ?error, + }; +} + +class AdminHealth { + const AdminHealth({ + required this.version, + required this.goVersion, + required this.started, + required this.uptimeSeconds, + required this.database, + required this.goroutines, + required this.memoryMb, + }); + + factory AdminHealth.fromJson(Map json) => AdminHealth( + version: json['version'] as String, + goVersion: json['goVersion'] as String, + started: json['started'] as String, + uptimeSeconds: (json['uptimeSeconds'] as num).toInt(), + database: AdminHealthDatabase.fromJson( + json['database'] as Map, + ), + goroutines: (json['goroutines'] as num).toInt(), + memoryMb: (json['memoryMb'] as num).toInt(), + ); + + final String version; + + final String goVersion; + + final String started; + + final int uptimeSeconds; + + final AdminHealthDatabase database; + + final int goroutines; + + final int memoryMb; + + Map toJson() => { + 'version': version, + 'goVersion': goVersion, + 'started': started, + 'uptimeSeconds': uptimeSeconds, + 'database': database.toJson(), + 'goroutines': goroutines, + 'memoryMb': memoryMb, + }; +} + +class Job { + const Job({ + required this.name, + required this.description, + required this.every, + required this.runnable, + required this.running, + required this.runs, + required this.failures, + required this.lastStarted, + required this.lastDurationMs, + required this.lastError, + }); + + factory Job.fromJson(Map json) => Job( + name: json['name'] as String, + description: json['description'] as String, + every: json['every'] as String, + runnable: json['runnable'] as bool, + running: json['running'] as bool, + runs: (json['runs'] as num).toInt(), + failures: (json['failures'] as num).toInt(), + lastStarted: json['lastStarted'] as String, + lastDurationMs: (json['lastDurationMs'] as num).toInt(), + lastError: json['lastError'] as String, + ); + + final String name; + + final String description; + + /// How often it runs on its own (Go duration); empty when only on demand + final String every; + + /// Can be started by hand + final bool runnable; + + final bool running; + + /// Runs since the server started + final int runs; + + final int failures; + + /// RFC 3339; empty before the first run + final String lastStarted; + + final int lastDurationMs; + + final String lastError; + + Map toJson() => { + 'name': name, + 'description': description, + 'every': every, + 'runnable': runnable, + 'running': running, + 'runs': runs, + 'failures': failures, + 'lastStarted': lastStarted, + 'lastDurationMs': lastDurationMs, + 'lastError': lastError, + }; +} + +class Backup { + const Backup({required this.key, required this.size, required this.modified}); + + factory Backup.fromJson(Map json) => Backup( + key: json['key'] as String, + size: (json['size'] as num).toInt(), + modified: json['modified'] as String, + ); + + final String key; + + final int size; + + final String modified; + + Map toJson() => { + 'key': key, + 'size': size, + 'modified': modified, + }; +} diff --git a/Server/admin/src/App.vue b/Server/admin/src/App.vue index 5b95248..e91045d 100644 --- a/Server/admin/src/App.vue +++ b/Server/admin/src/App.vue @@ -17,6 +17,7 @@ const nav = computed(() => [ ...(isSuperadmin.value ? [ { to: "/authentication", label: "Authentication" }, + { to: "/branding", label: "Branding" }, { to: "/storage", label: "Storage" }, { to: "/email", label: "Email" }, { to: "/maintenance", label: "Maintenance" }, diff --git a/Server/admin/src/components/InvitesPanel.vue b/Server/admin/src/components/InvitesPanel.vue new file mode 100644 index 0000000..31ae4d0 --- /dev/null +++ b/Server/admin/src/components/InvitesPanel.vue @@ -0,0 +1,116 @@ + + + + diff --git a/Server/admin/src/main.ts b/Server/admin/src/main.ts index 2d69de9..d4b392a 100644 --- a/Server/admin/src/main.ts +++ b/Server/admin/src/main.ts @@ -16,26 +16,27 @@ const router = createRouter({ { path: "/authentication", name: "authentication", - component: () => import("./views/PlaceholderView.vue"), - meta: { title: "Authentication", superadmin: true }, + component: () => import("./views/AuthenticationView.vue"), + meta: { superadmin: true }, + }, + { + path: "/branding", + name: "branding", + component: () => import("./views/BrandingView.vue"), + meta: { superadmin: true }, }, { path: "/storage", name: "storage", - component: () => import("./views/PlaceholderView.vue"), - meta: { title: "Storage", superadmin: true }, - }, - { - path: "/email", - name: "email", - component: () => import("./views/PlaceholderView.vue"), - meta: { title: "Email", superadmin: true }, + component: () => import("./views/StorageView.vue"), + meta: { superadmin: true }, }, + { path: "/email", name: "email", component: () => import("./views/EmailView.vue"), meta: { superadmin: true } }, { path: "/maintenance", name: "maintenance", - component: () => import("./views/PlaceholderView.vue"), - meta: { title: "Maintenance", superadmin: true }, + component: () => import("./views/MaintenanceView.vue"), + meta: { superadmin: true }, }, ], }); diff --git a/Server/admin/src/settings.ts b/Server/admin/src/settings.ts new file mode 100644 index 0000000..1b224d4 --- /dev/null +++ b/Server/admin/src/settings.ts @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +import type { InstanceSettings, InstanceSettingsUpdate } from "@docwell/api-client"; +import { onMounted, ref } from "vue"; +import { api, errorMessage } from "./session"; + +/** Loads the instance settings and saves one section at a time. */ +export function useInstanceSettings() { + const settings = ref(null); + const error = ref(""); + const saved = ref(false); + const busy = ref(false); + + async function load() { + error.value = ""; + try { + settings.value = await api.settings.get(); + } catch (e) { + error.value = errorMessage(e); + } + } + onMounted(load); + + async function save(patch: InstanceSettingsUpdate) { + busy.value = true; + error.value = ""; + saved.value = false; + try { + settings.value = await api.settings.update(patch); + saved.value = true; + setTimeout(() => (saved.value = false), 2500); + } catch (e) { + error.value = errorMessage(e); + } finally { + busy.value = false; + } + } + + return { settings, error, saved, busy, load, save }; +} + +/** Bytes as KB/MB/GB for people. */ +export function formatBytes(n: number): string { + if (n < 1024) return `${n} B`; + const units = ["KB", "MB", "GB", "TB"]; + let v = n / 1024; + let i = 0; + while (v >= 1024 && i < units.length - 1) { + v /= 1024; + i++; + } + return `${v.toFixed(v < 10 ? 1 : 0)} ${units[i]}`; +} + +/** Hands a downloaded file to the browser. */ +export function saveBlob(blob: Blob, filename: string) { + const url = URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = filename; + document.body.append(a); + a.click(); + a.remove(); + setTimeout(() => URL.revokeObjectURL(url), 1000); +} diff --git a/Server/admin/src/views/AuthenticationView.vue b/Server/admin/src/views/AuthenticationView.vue new file mode 100644 index 0000000..5e0d97b --- /dev/null +++ b/Server/admin/src/views/AuthenticationView.vue @@ -0,0 +1,77 @@ + + + +