Make the reupload command more efficient by sending multiple entries at once

This commit is contained in:
David Dworken 2022-10-09 17:10:11 -07:00
parent 6671a108d1
commit c16d260643
2 changed files with 30 additions and 18 deletions

View File

@ -999,13 +999,17 @@ func ReliableDbCreate(db *gorm.DB, entry interface{}) error {
return fmt.Errorf("failed to create DB entry even with %d retries: %v", i, err)
}
func EncryptAndMarshal(config hctx.ClientConfig, entry *data.HistoryEntry) ([]byte, error) {
func EncryptAndMarshal(config hctx.ClientConfig, entries []*data.HistoryEntry) ([]byte, error) {
var encEntries []shared.EncHistoryEntry
for _, entry := range entries {
encEntry, err := data.EncryptHistoryEntry(config.UserSecret, *entry)
if err != nil {
return nil, fmt.Errorf("failed to encrypt history entry")
}
encEntry.DeviceId = config.DeviceId
jsonValue, err := json.Marshal([]shared.EncHistoryEntry{encEntry})
encEntries = append(encEntries, encEntry)
}
jsonValue, err := json.Marshal(encEntries)
if err != nil {
return jsonValue, fmt.Errorf("failed to marshal encrypted history entry: %v", err)
}
@ -1074,3 +1078,20 @@ func deleteOnRemoteInstances(ctx *context.Context, historyEntries []*data.Histor
}
return nil
}
func Reupload(ctx *context.Context) error {
config := hctx.GetConf(ctx)
entries, err := data.Search(hctx.GetDb(ctx), "", 0)
if err != nil {
return fmt.Errorf("failed to reupload due to failed search: %v", err)
}
jsonValue, err := EncryptAndMarshal(config, entries)
if err != nil {
return fmt.Errorf("failed to reupload due to failed encryption: %v", err)
}
_, err = ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
if err != nil {
return fmt.Errorf("failed to reupload due to failed POST: %v", err)
}
return nil
}

View File

@ -109,16 +109,7 @@ func main() {
}
case "reupload":
// Purposefully undocumented since this command is generally not necessary to run
ctx := hctx.MakeContext()
config := hctx.GetConf(ctx)
entries, err := data.Search(hctx.GetDb(ctx), "", 0)
lib.CheckFatalError(err)
for _, entry := range entries {
jsonValue, err := lib.EncryptAndMarshal(config, entry)
lib.CheckFatalError(err)
_, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
lib.CheckFatalError(err)
}
lib.CheckFatalError(lib.Reupload(hctx.MakeContext()))
case "-h":
fallthrough
case "help":
@ -273,7 +264,7 @@ func maybeUploadSkippedHistoryEntries(ctx *context.Context) error {
}
hctx.GetLogger().Printf("Uploading %d history entries that previously failed to upload (query=%#v)\n", len(entries), query)
for _, entry := range entries {
jsonValue, err := lib.EncryptAndMarshal(config, entry)
jsonValue, err := lib.EncryptAndMarshal(config, []*data.HistoryEntry{entry})
if err != nil {
return err
}
@ -313,7 +304,7 @@ func saveHistoryEntry(ctx *context.Context) {
lib.CheckFatalError(err)
// Persist it remotely
jsonValue, err := lib.EncryptAndMarshal(config, entry)
jsonValue, err := lib.EncryptAndMarshal(config, []*data.HistoryEntry{entry})
lib.CheckFatalError(err)
_, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
if err != nil {