From e3ead981ec993a153c1d69782642ef701b13d329 Mon Sep 17 00:00:00 2001 From: RoyUP9 <87927115+RoyUP9@users.noreply.github.com> Date: Thu, 28 Oct 2021 11:20:45 +0300 Subject: [PATCH] fixed upload entries (#407) --- agent/pkg/database/main.go | 15 ++++++++------- agent/pkg/up9/main.go | 13 +++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/agent/pkg/database/main.go b/agent/pkg/database/main.go index aedf9ac4f..befc4bb92 100644 --- a/agent/pkg/database/main.go +++ b/agent/pkg/database/main.go @@ -13,11 +13,12 @@ import ( ) const ( - DBPath = "./entries.db" - OrderDesc = "desc" - OrderAsc = "asc" - LT = "lt" - GT = "gt" + DBPath = "./entries.db" + OrderDesc = "desc" + OrderAsc = "asc" + LT = "lt" + GT = "gt" + TimeFormat = "2006-01-02 15:04:05.000000000" ) var ( @@ -57,7 +58,7 @@ func initDataBase(databasePath string) *gorm.DB { return temp } -func GetEntriesFromDb(timestampFrom int64, timestampTo int64, protocolName *string) []tapApi.MizuEntry { +func GetEntriesFromDb(timeFrom time.Time, timeTo time.Time, protocolName *string) []tapApi.MizuEntry { order := OrderDesc protocolNameCondition := "1 = 1" if protocolName != nil { @@ -67,7 +68,7 @@ func GetEntriesFromDb(timestampFrom int64, timestampTo int64, protocolName *stri var entries []tapApi.MizuEntry GetEntriesTable(). Where(protocolNameCondition). - Where(fmt.Sprintf("timestamp BETWEEN %v AND %v", timestampFrom, timestampTo)). + Where(fmt.Sprintf("created_at BETWEEN '%s' AND '%s'", timeFrom.Format(TimeFormat), timeTo.Format(TimeFormat))). Order(fmt.Sprintf("timestamp %s", order)). Find(&entries) diff --git a/agent/pkg/up9/main.go b/agent/pkg/up9/main.go index 7f8673e72..6fa9304bd 100644 --- a/agent/pkg/up9/main.go +++ b/agent/pkg/up9/main.go @@ -206,13 +206,13 @@ func syncEntriesImpl(token string, model string, envPrefix string, uploadInterva sleepTime := time.Second * time.Duration(uploadIntervalSec) - var timestampFrom int64 = 0 + var timeFrom time.Time + protocolFilter := "http" for { - timestampTo := time.Now().UnixNano() / int64(time.Millisecond) - logger.Log.Infof("Getting entries from %v, to %v\n", timestampFrom, timestampTo) - protocolFilter := "http" - entriesArray := database.GetEntriesFromDb(timestampFrom, timestampTo, &protocolFilter) + timeTo := time.Now() + logger.Log.Infof("Getting entries from %v, to %v\n", timeFrom.Format(time.RFC3339Nano), timeTo.Format(time.RFC3339Nano)) + entriesArray := database.GetEntriesFromDb(timeFrom, timeTo, &protocolFilter) if len(entriesArray) > 0 { result := make([]har.Entry, 0) @@ -276,13 +276,14 @@ func syncEntriesImpl(token string, model string, envPrefix string, uploadInterva analyzeInformation.SentCount += len(entriesArray) logger.Log.Infof("Finish uploading %v entries to %s\n", len(entriesArray), GetTrafficDumpUrl(envPrefix, model)) + logger.Log.Infof("Uploaded %v entries until now", analyzeInformation.SentCount) } else { logger.Log.Infof("Nothing to upload") } logger.Log.Infof("Sleeping for %v...\n", sleepTime) time.Sleep(sleepTime) - timestampFrom = timestampTo + timeFrom = timeTo } }