From be4e3e778e37e929c232ede5c798026a4c70f5c2 Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Wed, 7 Jun 2023 21:52:43 +0200 Subject: [PATCH] Display host stats --- CHANGELOG.md | 2 +- frontend/src/lib/objects.js | 2 - .../connection/database/index.svelte | 2 +- .../organisms/connection/host/index.svelte | 64 +++++++++++++++++++ .../organisms/connection/host/status.svelte | 45 +++++++++++++ .../connection/host/systeminfo.svelte | 45 +++++++++++++ .../src/organisms/connection/hosttree.svelte | 11 +++- .../src/organisms/connection/index.svelte | 6 ++ frontend/wailsjs/go/app/App.d.ts | 2 +- frontend/wailsjs/go/models.ts | 16 +++++ internal/app/connection.go | 36 +++++++++-- 11 files changed, 219 insertions(+), 12 deletions(-) create mode 100644 frontend/src/organisms/connection/host/index.svelte create mode 100644 frontend/src/organisms/connection/host/status.svelte create mode 100644 frontend/src/organisms/connection/host/systeminfo.svelte diff --git a/CHANGELOG.md b/CHANGELOG.md index d748c64..0ee846e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## Unreleased -* Display database statistics generated by the corresponding diagnostic MongoDB commands in addition to collection stats (#15). +* Display host and database statistics generated by the corresponding diagnostic MongoDB commands in addition to collection stats (#15). * Added version number of the running Rolens instance in the about dialog (#25, #28). * Fixed host/database selection bug in grid. diff --git a/frontend/src/lib/objects.js b/frontend/src/lib/objects.js index a3151d2..3d7a246 100644 --- a/frontend/src/lib/objects.js +++ b/frontend/src/lib/objects.js @@ -64,10 +64,8 @@ export function deepClone(obj) { export function pathsAreEqual(x, y) { const lengthOfLongest = (x.length >= y.length) ? x.length : y.length; - console.log(x, y) for (let i = 0; i < lengthOfLongest; i++) { - console.log(x[i], y[i]) if (x[i] !== y[i]) { return false; } diff --git a/frontend/src/organisms/connection/database/index.svelte b/frontend/src/organisms/connection/database/index.svelte index 768b897..982ba12 100644 --- a/frontend/src/organisms/connection/database/index.svelte +++ b/frontend/src/organisms/connection/database/index.svelte @@ -25,7 +25,7 @@
{#if database} {#key database} - +
{#if tab === 'stats'} {/if} diff --git a/frontend/src/organisms/connection/host/index.svelte b/frontend/src/organisms/connection/host/index.svelte new file mode 100644 index 0000000..c9e585d --- /dev/null +++ b/frontend/src/organisms/connection/host/index.svelte @@ -0,0 +1,64 @@ + + +
+ {#if host} + {#key host} + + +
+ {#if tab === 'status'} + {:else if tab === 'systemInfo'} + {/if} +
+ {/key} + {:else} + + {/if} +
+ + diff --git a/frontend/src/organisms/connection/host/status.svelte b/frontend/src/organisms/connection/host/status.svelte new file mode 100644 index 0000000..1068450 --- /dev/null +++ b/frontend/src/organisms/connection/host/status.svelte @@ -0,0 +1,45 @@ + + +
+ + +
+ +
+ +
+ +
+
+ + diff --git a/frontend/src/organisms/connection/host/systeminfo.svelte b/frontend/src/organisms/connection/host/systeminfo.svelte new file mode 100644 index 0000000..0f43e2b --- /dev/null +++ b/frontend/src/organisms/connection/host/systeminfo.svelte @@ -0,0 +1,45 @@ + + +
+ + +
+ +
+ +
+ +
+
+ + diff --git a/frontend/src/organisms/connection/hosttree.svelte b/frontend/src/organisms/connection/hosttree.svelte index 7458792..b63a540 100644 --- a/frontend/src/organisms/connection/hosttree.svelte +++ b/frontend/src/organisms/connection/hosttree.svelte @@ -30,17 +30,24 @@ async function openConnection(hostKey) { const progress = startProgress(`Connecting to "${hostKey}"…`); + activeCollKey = ''; activeDbKey = ''; activeHostKey = hostKey; - const databases = await OpenConnection(hostKey); + + const { databases, status, systemInfo } = await OpenConnection(hostKey); if (databases) { - $connections[hostKey] = { databases: {} }; + $connections[hostKey] = $connections[hostKey] || {}; + $connections[hostKey].status = status; + $connections[hostKey].systemInfo = systemInfo; + + $connections[hostKey].databases = $connections[hostKey].databases || {}; databases.forEach(dbKey => { $connections[hostKey].databases[dbKey] = $connections[hostKey].databases[dbKey] || { collections: {} }; }); + activeHostKey = hostKey; dispatch('connected', hostKey); WindowSetTitle(`${$hosts[activeHostKey].name} - Rolens`); diff --git a/frontend/src/organisms/connection/index.svelte b/frontend/src/organisms/connection/index.svelte index 88dbcb8..c150ded 100644 --- a/frontend/src/organisms/connection/index.svelte +++ b/frontend/src/organisms/connection/index.svelte @@ -5,6 +5,7 @@ import { EnterText } from '$wails/go/ui/UI'; import { EventsOn } from '$wails/runtime/runtime'; import { onMount } from 'svelte'; + import HostView from './host/index.svelte'; import DatabaseView from './database/index.svelte'; import CollectionView from './collection/index.svelte'; import DumpInfo from './dump.svelte'; @@ -124,6 +125,11 @@ hostKey={activeHostKey} dbKey={activeDbKey} /> +{:else if activeHostKey} + {/if} ; export function OpenCollection(arg1:string,arg2:string,arg3:string):Promise; -export function OpenConnection(arg1:string):Promise>; +export function OpenConnection(arg1:string):Promise; export function OpenDatabase(arg1:string,arg2:string):Promise; diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index c04a1d6..679a460 100755 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -44,6 +44,22 @@ export namespace app { this.downloadDirectory = source["downloadDirectory"]; } } + export class HostInfo { + databases: string[]; + status: {[key: string]: any}; + systemInfo: {[key: string]: any}; + + static createFrom(source: any = {}) { + return new HostInfo(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.databases = source["databases"]; + this.status = source["status"]; + this.systemInfo = source["systemInfo"]; + } + } export class QueryResult { total: number; results: string[]; diff --git a/internal/app/connection.go b/internal/app/connection.go index d2f0153..9807719 100644 --- a/internal/app/connection.go +++ b/internal/app/connection.go @@ -12,6 +12,12 @@ import ( mongoOptions "go.mongodb.org/mongo-driver/mongo/options" ) +type HostInfo struct { + Databases []string `json:"databases"` + Status bson.M `json:"status"` + SystemInfo bson.M `json:"systemInfo"` +} + func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, func(), error) { hosts, err := a.Hosts() if err != nil { @@ -43,18 +49,38 @@ func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, fun }, nil } -func (a *App) OpenConnection(hostKey string) (databases []string) { +func (a *App) OpenConnection(hostKey string) (info HostInfo) { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - return nil + return } - databases, err = client.ListDatabaseNames(ctx, bson.M{}) + + info.Databases, err = client.ListDatabaseNames(ctx, bson.M{}) if err != nil { runtime.LogWarning(a.ctx, "Could not retrieve database names for host "+hostKey) runtime.LogWarning(a.ctx, err.Error()) zenity.Error(err.Error(), zenity.Title("Error while getting databases"), zenity.ErrorIcon) - return nil + return } + + command := bson.M{"serverStatus": 1} + err = client.Database("admin").RunCommand(ctx, command).Decode(&info.Status) + if err != nil { + runtime.LogWarning(a.ctx, "Could not retrieve server status") + runtime.LogWarning(a.ctx, err.Error()) + zenity.Error(err.Error(), zenity.Title("Could not get server status"), zenity.ErrorIcon) + return + } + + command = bson.M{"hostInfo": 1} + err = client.Database("admin").RunCommand(ctx, command).Decode(&info.SystemInfo) + if err != nil { + runtime.LogWarning(a.ctx, "Could not retrieve system info") + runtime.LogWarning(a.ctx, err.Error()) + zenity.Error(err.Error(), zenity.Title("Could not get system info"), zenity.ErrorIcon) + return + } + defer close() - return databases + return }