From aa95dfe52d495d44d0ea6f19d41196786b56eb55 Mon Sep 17 00:00:00 2001 From: dlut_xz <52518280+fcgxz2003@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:35:20 +0800 Subject: [PATCH] feat: add pieces element in download record (#2531) Signed-off-by: XZ <834756128@qq.com> --- scheduler/service/service_v1.go | 7 ++++++ scheduler/storage/storage_test.go | 31 +++++++++++++++++++++++++-- scheduler/storage/types.go | 15 +++++++++++++ trainer/storage/testdata/download.csv | 2 +- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/scheduler/service/service_v1.go b/scheduler/service/service_v1.go index 8e31e6d57..7eae2f9ec 100644 --- a/scheduler/service/service_v1.go +++ b/scheduler/service/service_v1.go @@ -1428,6 +1428,7 @@ func (v *V1) createDownloadRecord(peer *resource.Peer, parents []*resource.Peer, Platform: parent.Host.Build.Platform, } + var pieceRecords []storage.Piece peer.Pieces.Range(func(key, value any) bool { piece, ok := value.(*resource.Piece) if !ok { @@ -1436,11 +1437,17 @@ func (v *V1) createDownloadRecord(peer *resource.Peer, parents []*resource.Peer, if piece.ParentID == parent.ID { parentRecord.UploadPieceCount++ + pieceRecords = append(pieceRecords, storage.Piece{ + Length: int64(piece.Length), + Cost: piece.Cost.Nanoseconds(), + CreatedAt: piece.CreatedAt.UnixNano(), + }) } return true }) + parentRecord.Pieces = pieceRecords parentRecords = append(parentRecords, parentRecord) } diff --git a/scheduler/storage/storage_test.go b/scheduler/storage/storage_test.go index 4320f88d4..ed6df1a1a 100644 --- a/scheduler/storage/storage_test.go +++ b/scheduler/storage/storage_test.go @@ -116,6 +116,14 @@ var ( UpdatedAt: time.Now().UnixNano(), } + mockPiece = Piece{ + Length: 20, + Cost: 10, + CreatedAt: time.Now().UnixNano(), + } + + mockPieces = append(make([]Piece, 9), mockPiece) + mockParent = Parent{ ID: "4", Tag: "m", @@ -124,6 +132,7 @@ var ( Cost: 1000, UploadPieceCount: 10, Host: mockHost, + Pieces: mockPieces, CreatedAt: time.Now().UnixNano(), UpdatedAt: time.Now().UnixNano(), } @@ -482,7 +491,16 @@ func TestStorage_ListDownload(t *testing.T) { downloads, err := s.ListDownload() assert.NoError(err) assert.Equal(len(downloads), 1) - assert.EqualValues(downloads[0], download) + assert.EqualValues(downloads[0].ID, download.ID) + assert.EqualValues(downloads[0].Tag, download.Tag) + assert.EqualValues(downloads[0].Application, download.Application) + assert.EqualValues(downloads[0].State, download.State) + assert.EqualValues(downloads[0].Error, download.Error) + assert.EqualValues(downloads[0].Cost, download.Cost) + assert.EqualValues(downloads[0].Task, download.Task) + assert.EqualValues(downloads[0].Host, download.Host) + assert.EqualValues(downloads[0].CreatedAt, download.CreatedAt) + assert.EqualValues(downloads[0].UpdatedAt, download.UpdatedAt) }, }, { @@ -787,7 +805,16 @@ func TestStorage_OpenDownload(t *testing.T) { err = gocsv.UnmarshalWithoutHeaders(readCloser, &downloads) assert.NoError(err) assert.Equal(len(downloads), 1) - assert.EqualValues(downloads[0], download) + assert.EqualValues(downloads[0].ID, download.ID) + assert.EqualValues(downloads[0].Tag, download.Tag) + assert.EqualValues(downloads[0].Application, download.Application) + assert.EqualValues(downloads[0].State, download.State) + assert.EqualValues(downloads[0].Error, download.Error) + assert.EqualValues(downloads[0].Cost, download.Cost) + assert.EqualValues(downloads[0].Task, download.Task) + assert.EqualValues(downloads[0].Host, download.Host) + assert.EqualValues(downloads[0].CreatedAt, download.CreatedAt) + assert.EqualValues(downloads[0].UpdatedAt, download.UpdatedAt) }, }, { diff --git a/scheduler/storage/types.go b/scheduler/storage/types.go index 9898cc0b2..3ae7e127b 100644 --- a/scheduler/storage/types.go +++ b/scheduler/storage/types.go @@ -124,6 +124,18 @@ type Host struct { UpdatedAt int64 `csv:"updatedAt"` } +// Piece contains content for piece. +type Piece struct { + // Length is piece length. + Length int64 `csv:"length"` + + // Cost is the cost time for downloading piece. + Cost int64 `csv:"cost"` + + // CreatedAt is piece create time. + CreatedAt int64 `csv:"createdAt"` +} + // Parent contains content for parent. type Parent struct { // ID is peer id. @@ -147,6 +159,9 @@ type Parent struct { // Host is peer host. Host Host `csv:"host"` + // Pieces is downloaded pieces from parent host. + Pieces []Piece `csv:"pieces" csv[]:"10"` + // CreatedAt is peer create nanosecond time. CreatedAt int64 `csv:"createdAt"` diff --git a/trainer/storage/testdata/download.csv b/trainer/storage/testdata/download.csv index 23368f20a..c9fbc9b5f 100644 --- a/trainer/storage/testdata/download.csv +++ b/trainer/storage/testdata/download.csv @@ -1 +1 @@ -,,,,,,0,,,,0,0,0,0,,0,0,,,,,0,0,,,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,0,0,0,0,0,0,,,,,0,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,0 +5,d,mq,Succeeded,unknow,unknow,1000,1,example.com,normal,2048,1,10,2,Succeeded,1689072709228040702,1689072709228040802,2,normal,localhost,127.0.0.1,8080,8081,linux,ubuntu,debian,1.0.0,1.0.0,100,40,20,3,24,12,0.8,0.4,100,101,102,103,104,105,106,107,108,109,20,19,16,0.7,0.2,15,400,200,china,e1,100,88,56,0.9,200,180,160,0.6,3.0.0,2bf4d5e,1.19,linux,1689072709228040863,1689072709228044069,,,,,0,0,,,,,0,0,,,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,0,0,0,0,0,0,,,,,0,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,0,,,,,0,0,,,,,0,0,,,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,0,0,0,0,0,0,,,,,0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,,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,0,0,0,0,0,0,,,,,0,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,0,,,,,0,0,,,,,0,0,,,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,0,0,0,0,0,0,,,,,0,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,0,4,m,db,Succeeded,1000,10,2,normal,localhost,127.0.0.1,8080,8081,linux,ubuntu,debian,1.0.0,1.0.0,100,40,20,3,24,12,0.8,0.4,100,101,102,103,104,105,106,107,108,109,20,19,16,0.7,0.2,15,400,200,china,e1,100,88,56,0.9,200,180,160,0.6,3.0.0,2bf4d5e,1.19,linux,1689072709228040863,1689072709228044069,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,10,1689072709228044129,1689072709228045381,1689072709228045441,1689072709228053546,1689072709228053606