From 51ea546b087849fd0f647b752242b66b210ba87e Mon Sep 17 00:00:00 2001 From: zekaifeng <37854724+dormanze@users.noreply.github.com> Date: Fri, 7 Jun 2024 17:14:12 +0800 Subject: [PATCH 1/2] Add the printing of event time --- cmd/mirror-main.go | 59 +++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 15497c01cc..8315e71c4e 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -20,6 +20,7 @@ package cmd import ( "context" "fmt" + "github.com/dustin/go-humanize" "math/rand" "net/http" "path" @@ -277,17 +278,31 @@ type mirrorJob struct { // mirrorMessage container for file mirror messages type mirrorMessage struct { - Status string `json:"status"` - Source string `json:"source"` - Target string `json:"target"` - Size int64 `json:"size"` - TotalCount int64 `json:"totalCount"` - TotalSize int64 `json:"totalSize"` + Status string `json:"status"` + Source string `json:"source"` + Target string `json:"target"` + Size int64 `json:"size"` + TotalCount int64 `json:"totalCount"` + TotalSize int64 `json:"totalSize"` + EventTime string `json:"eventTime"` + EventType notification.EventType `json:"eventType"` } // String colorized mirror message func (m mirrorMessage) String() string { - return console.Colorize("Mirror", fmt.Sprintf("`%s` -> `%s`", m.Source, m.Target)) + var msg string + if m.EventTime != "" { + msg = console.Colorize("Time", fmt.Sprintf("[%s] ", m.EventTime)) + } + if m.EventType == notification.ObjectRemovedDelete { + return msg + "Removed " + console.Colorize("Removed", fmt.Sprintf("`%s`", m.Target)) + } + if m.EventTime == "" { + return console.Colorize("Mirror", fmt.Sprintf("`%s` -> `%s`", m.Source, m.Target)) + } + msg += console.Colorize("Size", fmt.Sprintf("%6s ", humanize.IBytes(uint64(m.Size)))) + msg += console.Colorize("Mirror", fmt.Sprintf("`%s` -> `%s`", m.Source, m.Target)) + return msg } // JSON jsonified mirror message @@ -345,7 +360,7 @@ func (mj *mirrorJob) doDeleteBucket(ctx context.Context, sURLs URLs) URLs { } // doRemove - removes files on target. -func (mj *mirrorJob) doRemove(ctx context.Context, sURLs URLs) URLs { +func (mj *mirrorJob) doRemove(ctx context.Context, sURLs URLs, event EventInfo) URLs { if mj.opts.isFake { return sURLs.WithError(nil) } @@ -375,13 +390,21 @@ func (mj *mirrorJob) doRemove(ctx context.Context, sURLs URLs) URLs { } return sURLs.WithError(result.Err) } + targetPath := filepath.ToSlash(filepath.Join(sURLs.TargetAlias, sURLs.TargetContent.URL.Path)) + mj.status.PrintMsg(mirrorMessage{ + Target: targetPath, + TotalCount: sURLs.TotalCount, + TotalSize: sURLs.TotalSize, + EventTime: event.Time, + EventType: event.Type, + }) } return sURLs.WithError(nil) } // doMirror - Mirror an object to multiple destination. URLs status contains a copy of sURLs and error if any. -func (mj *mirrorJob) doMirrorWatch(ctx context.Context, targetPath string, tgtSSE encrypt.ServerSide, sURLs URLs) URLs { +func (mj *mirrorJob) doMirrorWatch(ctx context.Context, targetPath string, tgtSSE encrypt.ServerSide, sURLs URLs, event EventInfo) URLs { shouldQueue := false if !mj.opts.isOverwrite && !mj.opts.activeActive { targetClient, err := newClient(targetPath) @@ -405,7 +428,7 @@ func (mj *mirrorJob) doMirrorWatch(ctx context.Context, targetPath string, tgtSS mj.status.AddCounts(1) sURLs.TotalSize = mj.status.Get() sURLs.TotalCount = mj.status.GetCounts() - return mj.doMirror(ctx, sURLs) + return mj.doMirror(ctx, sURLs, event) } return sURLs.WithError(probe.NewError(ObjectAlreadyExists{})) } @@ -428,7 +451,7 @@ func convertSizeToTag(size int64) string { } // doMirror - Mirror an object to multiple destination. URLs status contains a copy of sURLs and error if any. -func (mj *mirrorJob) doMirror(ctx context.Context, sURLs URLs) URLs { +func (mj *mirrorJob) doMirror(ctx context.Context, sURLs URLs, event EventInfo) URLs { if sURLs.Error != nil { // Erroneous sURLs passed. return sURLs.WithError(sURLs.Error.Trace()) } @@ -481,6 +504,8 @@ func (mj *mirrorJob) doMirror(ctx context.Context, sURLs URLs) URLs { Size: length, TotalCount: sURLs.TotalCount, TotalSize: sURLs.TotalSize, + EventTime: event.Time, + EventType: event.Type, }) } sURLs.MD5 = mj.opts.md5 @@ -593,10 +618,6 @@ func (mj *mirrorJob) monitorMirrorStatus(cancel context.CancelFunc) (errDuringMi if sURLs.SourceContent != nil { mirrorTotalUploadedBytes.Add(float64(sURLs.SourceContent.Size)) - } else if sURLs.TargetContent != nil { - // Construct user facing message and path. - targetPath := filepath.ToSlash(filepath.Join(sURLs.TargetAlias, sURLs.TargetContent.URL.Path)) - mj.status.PrintMsg(rmMessage{Key: targetPath}) } } @@ -691,7 +712,7 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) continue } mj.parallel.queueTask(func() URLs { - return mj.doMirrorWatch(ctx, targetPath, tgtSSE, mirrorURL) + return mj.doMirrorWatch(ctx, targetPath, tgtSSE, mirrorURL, event) }, mirrorURL.SourceContent.Size) } else if event.Type == notification.ObjectRemovedDelete { if targetAlias != "" && strings.Contains(event.UserAgent, uaMirrorAppName+":"+targetAlias) { @@ -711,7 +732,7 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) mirrorURL.TotalSize = mj.status.Get() if mirrorURL.TargetContent != nil && (mj.opts.isRemove || mj.opts.activeActive) { mj.parallel.queueTask(func() URLs { - return mj.doRemove(ctx, mirrorURL) + return mj.doRemove(ctx, mirrorURL, event) }, 0) } } else if event.Type == notification.BucketCreatedAll { @@ -811,11 +832,11 @@ func (mj *mirrorJob) startMirror(ctx context.Context) { if sURLs.SourceContent != nil { mj.parallel.queueTask(func() URLs { - return mj.doMirror(ctx, sURLs) + return mj.doMirror(ctx, sURLs, EventInfo{}) }, sURLs.SourceContent.Size) } else if sURLs.TargetContent != nil && mj.opts.isRemove { mj.parallel.queueTask(func() URLs { - return mj.doRemove(ctx, sURLs) + return mj.doRemove(ctx, sURLs, EventInfo{}) }, 0) } case <-ctx.Done(): From f0faa54a333119e1de27ea231a97b0b73f103d2a Mon Sep 17 00:00:00 2001 From: zekaifeng <37854724+dormanze@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:57:07 +0800 Subject: [PATCH 2/2] Update mirror-main.go --- cmd/mirror-main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 8315e71c4e..f0c6bf7d85 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -20,7 +20,6 @@ package cmd import ( "context" "fmt" - "github.com/dustin/go-humanize" "math/rand" "net/http" "path" @@ -30,6 +29,8 @@ import ( "sync" "time" + "github.com/dustin/go-humanize" + "github.com/fatih/color" "github.com/minio/cli" json "github.com/minio/colorjson"