1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-06-28 05:25:11 +00:00

Made harsh loading/error experience friendlier

This commit is contained in:
2023-06-23 17:22:47 +02:00
parent 964e66e8a3
commit dc0094b27c
21 changed files with 262 additions and 253 deletions

View File

@ -8,23 +8,27 @@ import (
"go.mongodb.org/mongo-driver/bson"
)
func (a *App) OpenCollection(hostKey, dbKey, collKey string) (result bson.M) {
type OpenCollectionResult struct {
Stats bson.M `json:"stats"`
StatsError string `json:"statsError"`
}
func (a *App) OpenCollection(hostKey, dbKey, collKey string) (result OpenCollectionResult) {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return nil
return
}
defer close()
command := bson.M{"collStats": collKey}
err = client.Database(dbKey).RunCommand(ctx, command).Decode(&result)
err = client.Database(dbKey).RunCommand(ctx, command).Decode(&result.Stats)
if err != nil {
runtime.LogWarning(a.ctx, "Could not retrieve collection stats for "+collKey)
runtime.LogWarning(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Could not get stats"), zenity.ErrorIcon)
return nil
result.StatsError = err.Error()
}
defer close()
return result
return
}
func (a *App) RenameCollection(hostKey, dbKey, collKey, newCollKey string) bool {
@ -32,6 +36,7 @@ func (a *App) RenameCollection(hostKey, dbKey, collKey, newCollKey string) bool
if err != nil {
return false
}
defer close()
var result bson.M
command := bson.D{
@ -46,7 +51,6 @@ func (a *App) RenameCollection(hostKey, dbKey, collKey, newCollKey string) bool
return false
}
defer close()
return true
}
@ -60,6 +64,7 @@ func (a *App) TruncateCollection(hostKey, dbKey, collKey string) bool {
if err != nil {
return false
}
defer close()
_, err = client.Database(dbKey).Collection(collKey).DeleteMany(ctx, bson.D{})
if err != nil {
@ -69,7 +74,6 @@ func (a *App) TruncateCollection(hostKey, dbKey, collKey string) bool {
return false
}
defer close()
return true
}
@ -83,6 +87,7 @@ func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
if err != nil {
return false
}
defer close()
err = client.Database(dbKey).Collection(collKey).Drop(ctx)
if err != nil {
@ -92,6 +97,5 @@ func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
return false
}
defer close()
return true
}

View File

@ -17,13 +17,14 @@ type Query struct {
Sort string `json:"sort"`
}
type QueryResult struct {
Total int64 `json:"total"`
Results []string `json:"results"`
type FindItemsResult struct {
Total int64 `json:"total"`
Results []string `json:"results"`
ErrorTitle string `json:"errorTitle"`
ErrorDescription string `json:"errorDescription"`
}
func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult {
var out QueryResult
func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) (result FindItemsResult) {
var form Query
err := json.Unmarshal([]byte(formJson), &form)
@ -31,15 +32,15 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult {
runtime.LogError(a.ctx, "Could not parse find form:")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Could not parse form"), zenity.ErrorIcon)
return out
return
}
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return out
return
}
defer close()
var query bson.M
var projection bson.M
var sort bson.M
@ -47,22 +48,25 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult {
err = bson.UnmarshalExtJSON([]byte(form.Query), true, &query)
if err != nil {
runtime.LogInfof(a.ctx, "Invalid find query: %s", err.Error())
zenity.Error(err.Error(), zenity.Title("Invalid query"), zenity.ErrorIcon)
return out
result.ErrorTitle = "Invalid query"
result.ErrorDescription = err.Error()
return
}
err = json.Unmarshal([]byte(form.Fields), &projection)
if err != nil {
runtime.LogInfof(a.ctx, "Invalid find projection: %s", err.Error())
zenity.Error(err.Error(), zenity.Title("Invalid projection"), zenity.ErrorIcon)
return out
result.ErrorTitle = "Invalid projection"
result.ErrorDescription = err.Error()
return
}
err = json.Unmarshal([]byte(form.Sort), &sort)
if err != nil {
runtime.LogInfof(a.ctx, "Invalid find sort: %s", err.Error())
zenity.Error(err.Error(), zenity.Title("Invalid sort"), zenity.ErrorIcon)
return out
result.ErrorTitle = "Invalid sort"
result.ErrorDescription = err.Error()
return
}
opt := mongoOptions.FindOptions{
@ -75,15 +79,17 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult {
total, err := client.Database(dbKey).Collection(collKey).CountDocuments(ctx, query, nil)
if err != nil {
runtime.LogWarningf(a.ctx, "Encountered an error while counting documents: %s", err.Error())
zenity.Error(err.Error(), zenity.Title("Error while counting docs"), zenity.ErrorIcon)
return out
result.ErrorTitle = "Error while counting documents"
result.ErrorDescription = err.Error()
return
}
cur, err := client.Database(dbKey).Collection(collKey).Find(ctx, query, &opt)
if err != nil {
runtime.LogWarningf(a.ctx, "Encountered an error while performing query: %s", err.Error())
zenity.Error(err.Error(), zenity.Title("Error while querying"), zenity.ErrorIcon)
return out
result.ErrorTitle = "Error while querying"
result.ErrorDescription = err.Error()
return
}
defer cur.Close(ctx)
@ -92,25 +98,27 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult {
if err != nil {
runtime.LogWarningf(a.ctx, "Encountered an error while performing query: %s", err.Error())
zenity.Error(err.Error(), zenity.Title("Error while querying"), zenity.ErrorIcon)
return out
result.ErrorTitle = "Error while querying"
result.ErrorDescription = err.Error()
return
}
out.Total = total
out.Results = make([]string, 0)
result.Total = total
result.Results = make([]string, 0)
for _, r := range results {
marshalled, err := bson.MarshalExtJSON(r, true, true)
if err != nil {
runtime.LogError(a.ctx, "Failed to marshal find BSON:")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Failed to marshal JSON"), zenity.ErrorIcon)
return out
result.ErrorTitle = "Failed to marshal JSON"
result.ErrorDescription = err.Error()
return
}
out.Results = append(out.Results, string(marshalled))
result.Results = append(result.Results, string(marshalled))
}
return out
return
}
func (a *App) UpdateFoundDocument(hostKey, dbKey, collKey, idJson, newDocJson string) bool {

View File

@ -11,10 +11,15 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)
func (a *App) GetIndexes(hostKey, dbKey, collKey string) []bson.M {
type GetIndexesResult struct {
Indexes []bson.M `json:"indexes"`
Error string `json:"error"`
}
func (a *App) GetIndexes(hostKey, dbKey, collKey string) (result GetIndexesResult) {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return nil
return
}
defer close()
@ -22,20 +27,18 @@ func (a *App) GetIndexes(hostKey, dbKey, collKey string) []bson.M {
if err != nil {
runtime.LogWarning(a.ctx, "Encountered an error while creating index cursor:")
runtime.LogWarning(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Error while creating cursor"), zenity.ErrorIcon)
return nil
result.Error = err.Error()
return
}
var results []bson.M
err = cur.All(ctx, &results)
err = cur.All(ctx, &result.Indexes)
if err != nil {
runtime.LogWarning(a.ctx, "Encountered an error while executing index cursor:")
runtime.LogWarning(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Error while running cursor"), zenity.ErrorIcon)
return nil
result.Error = err.Error()
}
return results
return
}
func (a *App) CreateIndex(hostKey, dbKey, collKey, jsonData string) string {

View File

@ -28,8 +28,8 @@ func (a *App) InsertItems(hostKey, dbKey, collKey, jsonData string) interface{}
if err != nil {
return nil
}
defer close()
res, err := client.Database(dbKey).Collection(collKey).InsertMany(ctx, data)
if err != nil {
runtime.LogWarning(a.ctx, "Encountered an error while performing insert:")

View File

@ -12,10 +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"`
type OpenConnectionResult struct {
Databases []string `json:"databases"`
Status bson.M `json:"status"`
StatusError string `json:"statusError"`
SystemInfo bson.M `json:"systemInfo"`
SystemInfoError string `json:"systemInfoError"`
}
func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, func(), error) {
@ -49,38 +51,35 @@ func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, fun
}, nil
}
func (a *App) OpenConnection(hostKey string) (info HostInfo) {
func (a *App) OpenConnection(hostKey string) (result OpenConnectionResult) {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return
}
defer close()
info.Databases, err = client.ListDatabaseNames(ctx, bson.M{})
result.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
}
command := bson.M{"serverStatus": 1}
err = client.Database("admin").RunCommand(ctx, command).Decode(&info.Status)
err = client.Database("admin").RunCommand(ctx, command).Decode(&result.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
result.StatusError = err.Error()
}
command = bson.M{"hostInfo": 1}
err = client.Database("admin").RunCommand(ctx, command).Decode(&info.SystemInfo)
err = client.Database("admin").RunCommand(ctx, command).Decode(&result.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
result.SystemInfoError = err.Error()
}
defer close()
return
}

View File

@ -6,35 +6,34 @@ import (
"go.mongodb.org/mongo-driver/bson"
)
type DatabaseInfo struct {
type OpenDatabaseResult struct {
Collections []string `json:"collections"`
Stats bson.M `json:"stats"`
StatsError string `json:"statsError"`
}
func (a *App) OpenDatabase(hostKey, dbKey string) (info DatabaseInfo) {
func (a *App) OpenDatabase(hostKey, dbKey string) (result OpenDatabaseResult) {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return
}
defer close()
command := bson.M{"dbStats": 1}
err = client.Database(dbKey).RunCommand(ctx, command).Decode(&info.Stats)
err = client.Database(dbKey).RunCommand(ctx, command).Decode(&result.Stats)
if err != nil {
runtime.LogWarning(a.ctx, "Could not retrieve database stats for "+dbKey)
runtime.LogWarning(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Could not get stats"), zenity.ErrorIcon)
return
result.StatsError = err.Error()
}
info.Collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{})
result.Collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{})
if err != nil {
runtime.LogWarning(a.ctx, "Could not retrieve collection list for db "+dbKey)
runtime.LogWarning(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Error while getting collections"), zenity.ErrorIcon)
return
}
defer close()
return
}
@ -48,6 +47,7 @@ func (a *App) DropDatabase(hostKey, dbKey string) bool {
if err != nil {
return false
}
defer close()
err = client.Database(dbKey).Drop(ctx)
if err != nil {
@ -57,6 +57,5 @@ func (a *App) DropDatabase(hostKey, dbKey string) bool {
return false
}
defer close()
return true
}