This is an automated email from the ASF dual-hosted git repository.

sunnianjun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new d186b41  refactor: optimize backup metadata time format (#464)
d186b41 is described below

commit d186b4199302e681f869dc8cd9371f971e123117
Author: liyao <[email protected]>
AuthorDate: Thu Nov 16 11:00:02 2023 +0800

    refactor: optimize backup metadata time format (#464)
    
    * feat: add time utils for formatting
    
    Signed-off-by: mlycore <[email protected]>
    
    * refactor: using string and unified format for time
    
    Signed-off-by: mlycore <[email protected]>
    
    * feat: rename mytime to atime
    
    Signed-off-by: mlycore <[email protected]>
    
    * refactor: refactor time string
    
    Signed-off-by: mlycore <[email protected]>
    
    * chore: update test with timeutil
    
    Signed-off-by: mlycore <[email protected]>
    
    ---------
    
    Signed-off-by: mlycore <[email protected]>
---
 pitr/cli/internal/cmd/backup.go             | 15 ++++----
 pitr/cli/internal/cmd/show_test.go          | 13 ++++---
 pitr/cli/internal/pkg/local-storage_test.go | 13 ++++---
 pitr/cli/internal/pkg/model/ls_backup.go    |  8 ++--
 pitr/cli/pkg/timeutil/time.go               | 57 +++++++++++++++++++++++++++++
 5 files changed, 83 insertions(+), 23 deletions(-)

diff --git a/pitr/cli/internal/cmd/backup.go b/pitr/cli/internal/cmd/backup.go
index 3f2a7a7..649cfca 100644
--- a/pitr/cli/internal/cmd/backup.go
+++ b/pitr/cli/internal/cmd/backup.go
@@ -27,6 +27,7 @@ import (
        "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
        "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/logging"
        "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/prettyoutput"
+       "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/timeutil"
 
        "github.com/google/uuid"
        "github.com/jedib0t/go-pretty/v6/progress"
@@ -225,8 +226,8 @@ func exportData(proxy pkg.IShardingSphereProxy, ls 
pkg.ILocalStorage) (lsBackup
                Info: &model.BackupMetaInfo{
                        ID:         uuid.New().String(), // generate uuid for 
this backup
                        CSN:        csn,
-                       StartTime:  time.Now().Unix(),
-                       EndTime:    0,
+                       StartTime:  timeutil.Now().String(),
+                       EndTime:    timeutil.Init(),
                        BackupMode: BackupMode,
                },
                SsBackup: &model.SsBackup{
@@ -299,8 +300,8 @@ func _execBackup(as pkg.IAgentServer, node 
*model.StorageNode, dnCh chan *model.
                Port:      node.Port,
                Status:    model.SsBackupStatusRunning,
                BackupID:  backupID,
-               StartTime: time.Now().Unix(),
-               EndTime:   0,
+               StartTime: timeutil.Now().String(),
+               EndTime:   timeutil.Init(),
        }
        dnCh <- dn
        return nil
@@ -363,7 +364,7 @@ func checkBackupStatus(lsBackup *model.LsBackup) 
model.BackupStatus {
 
        lsBackup.DnList = dnResult
        lsBackup.SsBackup.Status = backupFinalStatus
-       lsBackup.Info.EndTime = time.Now().Unix()
+       lsBackup.Info.EndTime = timeutil.Now().String()
        return backupFinalStatus
 }
 
@@ -388,14 +389,14 @@ func checkStatus(as pkg.IAgentServer, sn 
*model.StorageNode, dn *model.DataNode,
                        if err != nil {
                                tracker.MarkAsErrored()
                                dn.Status = status
-                               dn.EndTime = time.Now().Unix()
+                               dn.EndTime = timeutil.Now().String()
                                dnCh <- dn
                                done <- struct{}{}
                        }
                        if status == model.SsBackupStatusCompleted || status == 
model.SsBackupStatusFailed {
                                tracker.MarkAsDone()
                                dn.Status = status
-                               dn.EndTime = time.Now().Unix()
+                               dn.EndTime = timeutil.Now().String()
                                dnCh <- dn
                                done <- struct{}{}
                        }
diff --git a/pitr/cli/internal/cmd/show_test.go 
b/pitr/cli/internal/cmd/show_test.go
index d4f9519..ef2f427 100644
--- a/pitr/cli/internal/cmd/show_test.go
+++ b/pitr/cli/internal/cmd/show_test.go
@@ -25,6 +25,7 @@ import (
        "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg"
        mock_pkg 
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/mocks"
        "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
+       "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/timeutil"
        "github.com/golang/mock/gomock"
        . "github.com/onsi/ginkgo/v2"
        . "github.com/onsi/gomega"
@@ -37,8 +38,8 @@ var _ = Describe("Show", func() {
                                Info: &model.BackupMetaInfo{
                                        ID:        "back-id",
                                        CSN:       "csn",
-                                       StartTime: time.Now().Unix(),
-                                       EndTime:   time.Now().Add(time.Second * 
10).Unix(),
+                                       StartTime: timeutil.Now().String(),
+                                       EndTime:   
timeutil.Now().Add(time.Second * 10).String(),
                                },
                                DnList: []*model.DataNode{
                                        {
@@ -46,16 +47,16 @@ var _ = Describe("Show", func() {
                                                Port:      3306,
                                                Status:    "Running",
                                                BackupID:  "back-id",
-                                               StartTime: time.Now().Unix(),
-                                               EndTime:   
time.Now().Add(time.Second * 10).Unix(),
+                                               StartTime: 
timeutil.Now().String(),
+                                               EndTime:   
timeutil.Now().Add(time.Second * 10).String(),
                                        },
                                        {
                                                IP:        "127.0.0.2",
                                                Port:      3306,
                                                Status:    "Completed",
                                                BackupID:  "back-id",
-                                               StartTime: time.Now().Unix(),
-                                               EndTime:   
time.Now().Add(time.Second * 10).Unix(),
+                                               StartTime: 
timeutil.Now().String(),
+                                               EndTime:   
timeutil.Now().Add(time.Second * 10).String(),
                                        },
                                },
                                SsBackup: &model.SsBackup{
diff --git a/pitr/cli/internal/pkg/local-storage_test.go 
b/pitr/cli/internal/pkg/local-storage_test.go
index ae9e4ec..91976b6 100644
--- a/pitr/cli/internal/pkg/local-storage_test.go
+++ b/pitr/cli/internal/pkg/local-storage_test.go
@@ -23,6 +23,7 @@ import (
        "time"
 
        "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
+       "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/timeutil"
 
        "github.com/google/uuid"
        . "github.com/onsi/ginkgo/v2"
@@ -90,8 +91,8 @@ var _ = Describe("ILocalStorage", func() {
                                Info: &model.BackupMetaInfo{
                                        ID:        backupID,
                                        CSN:       uuid.New().String(),
-                                       StartTime: time.Now().Unix(),
-                                       EndTime:   
time.Now().Add(time.Minute).Unix(),
+                                       StartTime: timeutil.Now().String(),
+                                       EndTime:   
timeutil.Now().Add(time.Minute).String(),
                                },
                                DnList: []*model.DataNode{
                                        {
@@ -99,16 +100,16 @@ var _ = Describe("ILocalStorage", func() {
                                                Port:      5432,
                                                Status:    "Completed",
                                                BackupID:  "SK08DAK1",
-                                               StartTime: time.Now().Unix(),
-                                               EndTime:   time.Now().Unix(),
+                                               StartTime: 
timeutil.Now().String(),
+                                               EndTime:   
timeutil.Now().String(),
                                        },
                                        {
                                                IP:        "1.1.1.2",
                                                Port:      5432,
                                                Status:    "Completed",
                                                BackupID:  "SK08DAK2",
-                                               StartTime: time.Now().Unix(),
-                                               EndTime:   time.Now().Unix(),
+                                               StartTime: 
timeutil.Now().String(),
+                                               EndTime:   
timeutil.Now().String(),
                                        },
                                },
                                SsBackup: &model.SsBackup{
diff --git a/pitr/cli/internal/pkg/model/ls_backup.go 
b/pitr/cli/internal/pkg/model/ls_backup.go
index 8b5a428..e1e59da 100644
--- a/pitr/cli/internal/pkg/model/ls_backup.go
+++ b/pitr/cli/internal/pkg/model/ls_backup.go
@@ -29,8 +29,8 @@ type (
                ID         string       `json:"id"`
                CSN        string       `json:"csn"`
                BackupMode DBBackupMode `json:"backup_mode"`
-               StartTime  int64        `json:"start_time"` // Unix time
-               EndTime    int64        `json:"end_time"`   // Unix time
+               StartTime  string       `json:"start_time"`
+               EndTime    string       `json:"end_time"`
                FileName   string
        }
 
@@ -39,8 +39,8 @@ type (
                Port      uint16       `json:"port"`
                Status    BackupStatus `json:"status"`
                BackupID  string       `json:"backup_id"`
-               StartTime int64        `json:"start_time"` // Unix time
-               EndTime   int64        `json:"end_time"`   // Unix time
+               StartTime string       `json:"start_time"`
+               EndTime   string       `json:"end_time"`
        }
 )
 
diff --git a/pitr/cli/pkg/timeutil/time.go b/pitr/cli/pkg/timeutil/time.go
new file mode 100644
index 0000000..e1e7297
--- /dev/null
+++ b/pitr/cli/pkg/timeutil/time.go
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package timeutil
+
+import "time"
+
+const (
+       unifiedTimeFormat = "2006-01-02 15:04:05"
+)
+
+type atime struct {
+       time.Time
+}
+
+func Now() atime {
+       return atime{time.Now()}
+}
+
+func (t atime) Add(d time.Duration) atime {
+       _ = t.Time.Add(d)
+       return t
+}
+
+func (t atime) String() string {
+       return UnifiedTimeFormat(t.Time)
+}
+
+func (t atime) Unit() int64 {
+       return UnixTimestampFormat(t.Time)
+}
+
+func Init() string {
+       return ""
+}
+
+func UnifiedTimeFormat(t time.Time) string {
+       return t.Format(unifiedTimeFormat)
+}
+
+func UnixTimestampFormat(t time.Time) int64 {
+       return t.Unix()
+}

Reply via email to