2023-05-26 15:21:39 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ncruces/zenity"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2023-05-28 19:54:40 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
2023-05-26 15:21:39 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExportContents string
|
|
|
|
type ExportFormat string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ExportContentsAll ExportContents = "all"
|
|
|
|
ExportContentsQuery ExportContents = "query"
|
|
|
|
ExportContentsQueryLimitSkip ExportContents = "querylimitskip"
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
ExportFormatJsonArray ExportFormat = "jsonarray"
|
|
|
|
ExportFormatNdJson ExportFormat = "ndjson"
|
|
|
|
ExportFormatCsv ExportFormat = "csv"
|
2023-05-26 15:21:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ExportSettings struct {
|
|
|
|
Contents ExportContents `json:"contents"`
|
|
|
|
Format ExportFormat `json:"format"`
|
|
|
|
ViewKey string `json:"viewKey"`
|
|
|
|
QueryJson string `json:"query"`
|
2023-05-28 19:54:40 +00:00
|
|
|
Limit uint `json:"limit"`
|
|
|
|
Skip uint `json:"skip"`
|
2023-05-26 15:21:39 +00:00
|
|
|
OutFile string `json:"outfile"`
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
func getptr[T any](v T) *T {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:21:39 +00:00
|
|
|
func (a *App) PerformFindExport(hostKey, dbKey, collKey, settingsJson string) bool {
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogInfof(a.ctx, "Export started for %s/%s/%s. Settings: %s", hostKey, dbKey, collKey, settingsJson)
|
|
|
|
|
2023-05-26 15:21:39 +00:00
|
|
|
var settings ExportSettings
|
|
|
|
if err := json.Unmarshal([]byte(settingsJson), &settings); err != nil {
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogWarningf(a.ctx, "Could not parse export settings: %s", err.Error())
|
2023-05-26 15:21:39 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Couldn't parse export settings!"), zenity.ErrorIcon)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
switch settings.Contents {
|
|
|
|
case ExportContentsAll:
|
|
|
|
settings.QueryJson = "{}"
|
|
|
|
settings.Limit = 0
|
|
|
|
settings.Skip = 0
|
|
|
|
case ExportContentsQuery:
|
|
|
|
settings.Limit = 0
|
|
|
|
settings.Skip = 0
|
|
|
|
case ExportContentsQueryLimitSkip:
|
2023-05-26 15:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
views, err := a.Views()
|
|
|
|
if err != nil {
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogWarningf(a.ctx, "Export: error while retrieving view: %s", err.Error())
|
2023-05-26 15:21:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
view, found := views[settings.ViewKey]
|
|
|
|
if !found {
|
|
|
|
zenity.Error(fmt.Sprintf("View %s is not known", settings.ViewKey), zenity.ErrorIcon)
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogDebugf(a.ctx, "Export: unknown view %s", settings.ViewKey)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileFilter runtime.FileFilter
|
|
|
|
defaultFilename := ""
|
|
|
|
|
|
|
|
switch settings.Format {
|
|
|
|
case ExportFormatCsv:
|
|
|
|
defaultFilename = "export.csv"
|
|
|
|
fileFilter = runtime.FileFilter{
|
|
|
|
DisplayName: "CSV files (*.csv)",
|
|
|
|
Pattern: "*.csv",
|
|
|
|
}
|
|
|
|
case ExportFormatJsonArray:
|
|
|
|
defaultFilename = "export.json"
|
|
|
|
fileFilter = runtime.FileFilter{
|
|
|
|
DisplayName: "JSON files (*.json)",
|
|
|
|
Pattern: "*.json",
|
|
|
|
}
|
|
|
|
case ExportFormatNdJson:
|
|
|
|
defaultFilename = "export.ndjson"
|
|
|
|
fileFilter = runtime.FileFilter{
|
|
|
|
DisplayName: "Newline delimited JSON files (*.ndjson)",
|
|
|
|
Pattern: "*.ndjson",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.OutFile, err = runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
|
|
|
Title: "Choose export destination",
|
|
|
|
DefaultDirectory: a.Env.DownloadDirectory,
|
|
|
|
CanCreateDirectories: true,
|
|
|
|
DefaultFilename: defaultFilename,
|
|
|
|
Filters: []runtime.FileFilter{fileFilter},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
zenity.Error("An error occured while choosing the export destination", zenity.ErrorIcon)
|
|
|
|
runtime.LogWarningf(a.ctx, "Export: error while choosing export destination: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if settings.OutFile == "" {
|
|
|
|
zenity.Error("You must specify an export destination.", zenity.ErrorIcon)
|
|
|
|
runtime.LogDebug(a.ctx, "Export: no destination specified")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(settings.OutFile); err == nil {
|
|
|
|
zenity.Error(fmt.Sprintf("File %s already exists, export aborted.", settings.OutFile), zenity.ErrorIcon)
|
|
|
|
runtime.LogDebugf(a.ctx, "Export: destination %s already exists", settings.OutFile)
|
2023-05-26 15:21:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var query bson.M
|
|
|
|
if settings.Contents != ExportContentsAll {
|
|
|
|
if err = bson.UnmarshalExtJSON([]byte(settings.QueryJson), true, &query); err != nil {
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogDebugf(a.ctx, "Invalid find query (exporting): %s", settings.QueryJson)
|
2023-05-26 15:21:39 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Invalid query"), zenity.ErrorIcon)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client, ctx, close, err := a.connectToHost(hostKey)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer close()
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
pgr, _ := zenity.Progress(zenity.Title("Performing export…"))
|
2023-05-26 15:21:39 +00:00
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
projection := bson.M{}
|
2023-05-26 15:21:39 +00:00
|
|
|
if settings.ViewKey != "list" {
|
|
|
|
for _, col := range view.Columns {
|
|
|
|
projection[col.Key] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
var count uint = 0
|
|
|
|
if settings.Limit != 0 {
|
|
|
|
count = uint(settings.Limit)
|
|
|
|
} else {
|
|
|
|
c, _ := client.Database(dbKey).Collection(collKey).CountDocuments(ctx, query, &options.CountOptions{
|
|
|
|
Skip: getptr(int64(settings.Skip)),
|
|
|
|
})
|
|
|
|
count = uint(c)
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:21:39 +00:00
|
|
|
cur, err := client.Database(dbKey).Collection(collKey).Find(ctx, query, &options.FindOptions{
|
2023-05-28 19:54:40 +00:00
|
|
|
Skip: getptr(int64(settings.Skip)),
|
|
|
|
Limit: getptr(int64(settings.Limit)),
|
2023-05-26 15:21:39 +00:00
|
|
|
Projection: projection,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogInfof(a.ctx, "Export: unable to get cursor while exporting: %s", err.Error())
|
2023-05-26 15:21:39 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Unable to get cursor"), zenity.ErrorIcon)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile(settings.OutFile, os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
zenity.Error(fmt.Sprintf(err.Error(), zenity.Title("Error while opening file"), settings.OutFile), zenity.ErrorIcon)
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogDebugf(a.ctx, "Export: unable to open file %s", settings.OutFile)
|
2023-05-26 15:21:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var csvWriter *csv.Writer
|
2023-05-28 19:54:40 +00:00
|
|
|
var csvColumnKeys []string
|
|
|
|
var index uint = 0
|
2023-05-26 15:21:39 +00:00
|
|
|
|
|
|
|
switch settings.Format {
|
|
|
|
case ExportFormatJsonArray:
|
2023-05-28 19:54:40 +00:00
|
|
|
file.WriteString("[")
|
|
|
|
|
2023-05-26 15:21:39 +00:00
|
|
|
case ExportFormatCsv:
|
|
|
|
csvWriter = csv.NewWriter(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
for cur.Next(ctx) {
|
|
|
|
switch settings.Format {
|
|
|
|
case ExportFormatCsv:
|
2023-05-28 19:54:40 +00:00
|
|
|
els, err := cur.Current.Elements()
|
|
|
|
if err != nil {
|
|
|
|
zenity.Error(err.Error(), zenity.Title("BSON invalid"), zenity.ErrorIcon)
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:21:39 +00:00
|
|
|
csvItem := make([]string, 0)
|
|
|
|
|
|
|
|
switch settings.ViewKey {
|
|
|
|
case "list":
|
|
|
|
if csvColumnKeys == nil {
|
2023-05-28 19:54:40 +00:00
|
|
|
csvColumnKeys = make([]string, 0)
|
|
|
|
|
|
|
|
for _, el := range els {
|
|
|
|
if el.Key() == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch el.Value().Type {
|
|
|
|
case bsontype.Boolean,
|
|
|
|
bsontype.Decimal128,
|
|
|
|
bsontype.Double,
|
|
|
|
bsontype.Int32,
|
|
|
|
bsontype.Int64,
|
|
|
|
bsontype.Null,
|
|
|
|
bsontype.ObjectID,
|
|
|
|
bsontype.Regex,
|
|
|
|
bsontype.String,
|
|
|
|
bsontype.Symbol,
|
|
|
|
bsontype.Timestamp,
|
|
|
|
bsontype.Undefined:
|
|
|
|
csvColumnKeys = append(csvColumnKeys, el.Key())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.LogDebugf(a.ctx, "Export csvColumnKeys: %v", csvColumnKeys)
|
|
|
|
|
|
|
|
if err := csvWriter.Write(csvColumnKeys); err != nil {
|
|
|
|
runtime.LogInfof(a.ctx, "Unable to write item %d to CSV while exporting: %s", index, err.Error())
|
|
|
|
zenity.Error(err.Error(), zenity.Title("Unable to write item %d to CSV"), zenity.ErrorIcon)
|
2023-05-26 15:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range csvColumnKeys {
|
2023-05-28 19:54:40 +00:00
|
|
|
r, err := cur.Current.LookupErr(k)
|
|
|
|
if err != nil {
|
|
|
|
csvItem = append(csvItem, "")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var v any
|
|
|
|
if err := r.Unmarshal(&v); err != nil {
|
|
|
|
zenity.Error(err.Error(), zenity.Title(fmt.Sprintf("Unable to unmarshal field %s", k)), zenity.ErrorIcon)
|
|
|
|
csvItem = append(csvItem, "")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-18 19:31:55 +00:00
|
|
|
csvItem = append(csvItem, fmt.Sprintf("%v", v))
|
2023-05-26 15:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2023-05-28 19:54:40 +00:00
|
|
|
// @todo
|
2023-05-26 15:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := csvWriter.Write(csvItem); err != nil {
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogInfof(a.ctx, "Unable to write item %d to CSV while exporting: %s", index, err.Error())
|
2023-05-26 15:21:39 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Unable to write item %d to CSV"), zenity.ErrorIcon)
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
csvWriter.Flush()
|
|
|
|
|
|
|
|
case ExportFormatJsonArray, ExportFormatNdJson:
|
|
|
|
itemJson, err := bson.MarshalExtJSON(cur.Current, true, false)
|
2023-05-26 15:21:39 +00:00
|
|
|
if err != nil {
|
2023-05-28 19:54:40 +00:00
|
|
|
runtime.LogInfof(a.ctx, "Unable to marshal item %d to JSON while exporting: %s", index, err.Error())
|
2023-05-26 15:21:39 +00:00
|
|
|
zenity.Error(err.Error(), zenity.Title("Unable to marshal item %d to JSON"), zenity.ErrorIcon)
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
if (settings.Format == ExportFormatJsonArray) && (index != 0) {
|
|
|
|
file.WriteString(",\n")
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:21:39 +00:00
|
|
|
file.Write(itemJson)
|
2023-05-28 19:54:40 +00:00
|
|
|
|
|
|
|
if settings.Format == ExportFormatNdJson {
|
|
|
|
file.WriteString("\n")
|
2023-05-26 15:21:39 +00:00
|
|
|
}
|
2023-05-28 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index++
|
|
|
|
|
|
|
|
if count != 0 && pgr != nil {
|
|
|
|
p := (float32(index) / float32(count)) * 100.0
|
|
|
|
pgr.Value(int(p))
|
2023-05-26 15:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings.Format == ExportFormatJsonArray {
|
|
|
|
file.WriteString("]\n")
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:54:40 +00:00
|
|
|
if pgr != nil {
|
|
|
|
pgr.Complete()
|
|
|
|
pgr.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
a.ui.Reveal(settings.OutFile)
|
|
|
|
runtime.LogInfo(a.ctx, "Export succeeded")
|
2023-05-26 15:21:39 +00:00
|
|
|
return true
|
|
|
|
}
|