feat: added pagination to listing files

This commit is contained in:
Andrey Melnikov
2021-07-16 15:33:09 -07:00
parent 1fb0d10b7c
commit afb98c295b
5 changed files with 161 additions and 21 deletions

View File

@@ -1396,6 +1396,20 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "page",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
},
{
"name": "perPage",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
}
],
"tags": [
@@ -3594,6 +3608,22 @@
"ListFilesResponse": {
"type": "object",
"properties": {
"count": {
"type": "integer",
"format": "int32"
},
"totalCount": {
"type": "integer",
"format": "int32"
},
"page": {
"type": "integer",
"format": "int32"
},
"pages": {
"type": "integer",
"format": "int32"
},
"files": {
"type": "array",
"items": {

View File

@@ -1202,6 +1202,8 @@ type ListFilesRequest struct {
Namespace string `protobuf:"bytes,1,opt,name=namespace,proto3" json:"namespace,omitempty"`
Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid,omitempty"`
Path string `protobuf:"bytes,3,opt,name=path,proto3" json:"path,omitempty"`
Page int32 `protobuf:"varint,4,opt,name=page,proto3" json:"page,omitempty"`
PerPage int32 `protobuf:"varint,5,opt,name=perPage,proto3" json:"perPage,omitempty"`
}
func (x *ListFilesRequest) Reset() {
@@ -1257,13 +1259,31 @@ func (x *ListFilesRequest) GetPath() string {
return ""
}
func (x *ListFilesRequest) GetPage() int32 {
if x != nil {
return x.Page
}
return 0
}
func (x *ListFilesRequest) GetPerPage() int32 {
if x != nil {
return x.PerPage
}
return 0
}
type ListFilesResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Files []*File `protobuf:"bytes,1,rep,name=files,proto3" json:"files,omitempty"`
ParentPath string `protobuf:"bytes,2,opt,name=parentPath,proto3" json:"parentPath,omitempty"`
Count int32 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"`
TotalCount int32 `protobuf:"varint,2,opt,name=totalCount,proto3" json:"totalCount,omitempty"`
Page int32 `protobuf:"varint,3,opt,name=page,proto3" json:"page,omitempty"`
Pages int32 `protobuf:"varint,4,opt,name=pages,proto3" json:"pages,omitempty"`
Files []*File `protobuf:"bytes,5,rep,name=files,proto3" json:"files,omitempty"`
ParentPath string `protobuf:"bytes,6,opt,name=parentPath,proto3" json:"parentPath,omitempty"`
}
func (x *ListFilesResponse) Reset() {
@@ -1298,6 +1318,34 @@ func (*ListFilesResponse) Descriptor() ([]byte, []int) {
return file_workflow_proto_rawDescGZIP(), []int{18}
}
func (x *ListFilesResponse) GetCount() int32 {
if x != nil {
return x.Count
}
return 0
}
func (x *ListFilesResponse) GetTotalCount() int32 {
if x != nil {
return x.TotalCount
}
return 0
}
func (x *ListFilesResponse) GetPage() int32 {
if x != nil {
return x.Page
}
return 0
}
func (x *ListFilesResponse) GetPages() int32 {
if x != nil {
return x.Pages
}
return 0
}
func (x *ListFilesResponse) GetFiles() []*File {
if x != nil {
return x.Files
@@ -2211,17 +2259,26 @@ var file_workflow_proto_rawDesc = []byte{
0x69, 0x66, 0x69, 0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x6c, 0x61, 0x73,
0x74, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x64, 0x69, 0x72,
0x65, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x64, 0x69,
0x72, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x22, 0x56, 0x0a, 0x10, 0x4c, 0x69, 0x73, 0x74, 0x46,
0x69, 0x6c, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x6e,
0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09,
0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x70,
0x61, 0x74, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x22,
0x54, 0x0a, 0x11, 0x4c, 0x69, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20,
0x72, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x22, 0x84, 0x01, 0x0a, 0x10, 0x4c, 0x69, 0x73, 0x74,
0x46, 0x69, 0x6c, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1c, 0x0a, 0x09,
0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x09, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69,
0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04,
0x70, 0x61, 0x74, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68,
0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x67, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04,
0x70, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x65, 0x72, 0x50, 0x61, 0x67, 0x65, 0x18,
0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x70, 0x65, 0x72, 0x50, 0x61, 0x67, 0x65, 0x22, 0xb4,
0x01, 0x0a, 0x11, 0x4c, 0x69, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20,
0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x74, 0x6f,
0x74, 0x61, 0x6c, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a,
0x74, 0x6f, 0x74, 0x61, 0x6c, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61,
0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x70, 0x61, 0x67, 0x65, 0x12, 0x14,
0x0a, 0x05, 0x70, 0x61, 0x67, 0x65, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x70,
0x61, 0x67, 0x65, 0x73, 0x12, 0x1f, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x05, 0x20,
0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x52, 0x05,
0x66, 0x69, 0x6c, 0x65, 0x73, 0x12, 0x1e, 0x0a, 0x0a, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x50,
0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x70, 0x61, 0x72, 0x65, 0x6e,
0x61, 0x74, 0x68, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x70, 0x61, 0x72, 0x65, 0x6e,
0x74, 0x50, 0x61, 0x74, 0x68, 0x22, 0x64, 0x0a, 0x0a, 0x53, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74,
0x69, 0x63, 0x73, 0x12, 0x26, 0x0a, 0x0e, 0x77, 0x6f, 0x72, 0x6b, 0x66, 0x6c, 0x6f, 0x77, 0x53,
0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x77, 0x6f, 0x72,

View File

@@ -801,6 +801,10 @@ func local_request_WorkflowService_GetArtifact_0(ctx context.Context, marshaler
}
var (
filter_WorkflowService_ListFiles_0 = &utilities.DoubleArray{Encoding: map[string]int{"namespace": 0, "uid": 1, "path": 2}, Base: []int{1, 1, 2, 3, 0, 0, 0}, Check: []int{0, 1, 1, 1, 2, 3, 4}}
)
func request_WorkflowService_ListFiles_0(ctx context.Context, marshaler runtime.Marshaler, client WorkflowServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq ListFilesRequest
var metadata runtime.ServerMetadata
@@ -842,6 +846,13 @@ func request_WorkflowService_ListFiles_0(ctx context.Context, marshaler runtime.
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "path", err)
}
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_WorkflowService_ListFiles_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := client.ListFiles(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
@@ -888,6 +899,13 @@ func local_request_WorkflowService_ListFiles_0(ctx context.Context, marshaler ru
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "path", err)
}
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_WorkflowService_ListFiles_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := server.ListFiles(ctx, &protoReq)
return msg, metadata, err

View File

@@ -253,11 +253,17 @@ message ListFilesRequest {
string namespace = 1;
string uid = 2;
string path = 3;
int32 page = 4;
int32 perPage = 5;
}
message ListFilesResponse {
repeated File files = 1;
string parentPath = 2;
int32 count = 1;
int32 totalCount = 2;
int32 page = 3;
int32 pages = 4;
repeated File files = 5;
string parentPath = 6;
}
message Statistics {

View File

@@ -14,6 +14,7 @@ import (
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"math"
"sort"
"strings"
"time"
@@ -475,6 +476,13 @@ func (s *WorkflowServer) ListFiles(ctx context.Context, req *api.ListFilesReques
return nil, err
}
if req.Page < 0 {
req.Page = 1
}
if req.PerPage <= 0 {
req.PerPage = 15
}
files, err := client.ListFiles(req.Namespace, req.Path)
if err != nil {
return nil, err
@@ -493,22 +501,43 @@ func (s *WorkflowServer) ListFiles(ctx context.Context, req *api.ListFilesReques
}
}
sort.Slice(apiFiles, func(i, j int) bool {
fileI := apiFiles[i]
fileJ := apiFiles[j]
sort.SliceStable(apiFiles, func(i, j int) bool {
lhFile := apiFiles[i]
rhFile := apiFiles[j]
if fileI.Directory && !fileJ.Directory {
if (lhFile.Directory && rhFile.Directory) ||
(!lhFile.Directory && !rhFile.Directory) {
return strings.Compare(strings.ToLower(lhFile.Name), strings.ToLower(rhFile.Name)) < 0
}
if lhFile.Directory {
return true
}
return strings.Compare(fileI.Path, fileJ.Path) < 0
return false
})
parentPath := v1.FilePathToParentPath(req.Path)
start := (req.Page - 1) * req.PerPage
if start < 0 {
start = 0
}
end := int(start + req.PerPage)
if end > len(apiFiles) {
end = len(apiFiles)
}
parts := apiFiles[start:end]
count := len(parts)
totalCount := len(apiFiles)
return &api.ListFilesResponse{
Files: apiFiles,
ParentPath: parentPath,
Count: int32(count),
Page: req.Page,
Pages: int32(math.Ceil(float64(totalCount) / float64(req.PerPage))),
TotalCount: int32(totalCount),
Files: parts,
ParentPath: v1.FilePathToParentPath(req.Path),
}, nil
}