add limit param to task service

pull/10616/head
zhulongcheng 2018-11-30 00:28:17 +08:00
parent 97d29e8974
commit d8c80fa50c
4 changed files with 32 additions and 5 deletions

View File

@ -173,6 +173,7 @@ type TaskFindFlags struct {
user string
id string
orgID string
limit int
}
var taskFindFlags TaskFindFlags
@ -187,6 +188,7 @@ func init() {
taskFindCmd.Flags().StringVarP(&taskFindFlags.id, "id", "i", "", "task ID")
taskFindCmd.Flags().StringVarP(&taskFindFlags.user, "user-id", "n", "", "task owner ID")
taskFindCmd.Flags().StringVarP(&taskFindFlags.orgID, "org-id", "", "", "task organization ID")
taskFindCmd.Flags().IntVarP(&taskFindFlags.limit, "limit", "", platform.TaskDefaultPageSize, "the number of tasks to find")
taskCmd.AddCommand(taskFindCmd)
}
@ -216,6 +218,12 @@ func taskFindF(cmd *cobra.Command, args []string) {
filter.Organization = id
}
if taskFindFlags.limit < 1 || taskFindFlags.limit > platform.TaskMaxPageSize {
fmt.Printf("limit must be between 1 and %d \n", platform.TaskMaxPageSize)
os.Exit(1)
}
filter.Limit = taskFindFlags.limit
var tasks []*platform.Task
var err error

View File

@ -225,6 +225,19 @@ func decodeGetTasksRequest(ctx context.Context, r *http.Request) (*getTasksReque
req.filter.User = id
}
if limit := qp.Get("limit"); limit != "" {
lim, err := strconv.Atoi(limit)
if err != nil {
return nil, err
}
if lim < 1 || lim > platform.TaskMaxPageSize {
return nil, kerrors.InvalidDataf("limit must be between 1 and %d", platform.TaskMaxPageSize)
}
req.filter.Limit = lim
} else {
req.filter.Limit = platform.TaskDefaultPageSize
}
return req, nil
}
@ -809,6 +822,9 @@ func (t TaskService) FindTasks(ctx context.Context, filter platform.TaskFilter)
if filter.User != nil {
val.Add("user", filter.User.String())
}
if filter.Limit != 0 {
val.Add("limit", strconv.Itoa(filter.Limit))
}
u.RawQuery = val.Encode()

View File

@ -4,6 +4,11 @@ import (
"context"
)
const (
TaskDefaultPageSize = 100
TaskMaxPageSize = 500
)
// Task is a task. 🎊
type Task struct {
ID ID `json:"id,omitempty"`
@ -78,6 +83,7 @@ type TaskFilter struct {
After *ID
Organization *ID
User *ID
Limit int
}
// RunFilter represents a set of filters that restrict the returned results

View File

@ -43,9 +43,7 @@ func (p pAdapter) FindTaskByID(ctx context.Context, id platform.ID) (*platform.T
}
func (p pAdapter) FindTasks(ctx context.Context, filter platform.TaskFilter) ([]*platform.Task, int, error) {
const pageSize = 100 // According to the platform.TaskService.FindTasks API.
params := backend.TaskSearchParams{PageSize: pageSize}
params := backend.TaskSearchParams{PageSize: filter.Limit}
if filter.Organization != nil {
params.Org = *filter.Organization
}
@ -68,8 +66,7 @@ func (p pAdapter) FindTasks(ctx context.Context, filter platform.TaskFilter) ([]
}
}
totalResults := len(pts) // TODO(mr): don't lie about the total results. Update ListTasks signature?
return pts, totalResults, nil
return pts, len(pts), nil
}
func (p pAdapter) CreateTask(ctx context.Context, t *platform.Task) error {