Skip to content

Commit

Permalink
API updates for populating count in Eval.List RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Nov 3, 2022
1 parent fa30924 commit e7136a8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
10 changes: 10 additions & 0 deletions api/evaluations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func (e *Evaluations) List(q *QueryOptions) ([]*Evaluation, *QueryMeta, error) {
return resp, qm, nil
}

// Count is used to get a count of evaluations.
func (e *Evaluations) Count(q *QueryOptions) (int, *QueryMeta, error) {
var resp map[string]int
qm, err := e.client.query("/v1/evaluations?count=true", &resp, q)
if err != nil {
return 0, nil, err
}
return resp["Count"], qm, nil
}

func (e *Evaluations) PrefixList(prefix string) ([]*Evaluation, *QueryMeta, error) {
return e.List(&QueryOptions{Prefix: prefix})
}
Expand Down
13 changes: 13 additions & 0 deletions command/agent/eval_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func (s *HTTPServer) evalsListRequest(resp http.ResponseWriter, req *http.Reques
args.FilterEvalStatus = query.Get("status")
args.FilterJobID = query.Get("job")

countOnly, err := parseBool(req, "count")
if err != nil {
return nil, err
}
if countOnly != nil {
args.CountOnly = *countOnly
}

var out structs.EvalListResponse
if err := s.agent.RPC("Eval.List", &args, &out); err != nil {
return nil, err
Expand All @@ -43,6 +51,11 @@ func (s *HTTPServer) evalsListRequest(resp http.ResponseWriter, req *http.Reques
if out.Evaluations == nil {
out.Evaluations = make([]*structs.Evaluation, 0)
}

if args.CountOnly {
return map[string]int{"Count": out.Count}, nil
}

return out.Evaluations, nil
}

Expand Down
38 changes: 18 additions & 20 deletions command/eval_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,28 +294,26 @@ func correctGrammar(word string, num int) string {

func (e *EvalDeleteCommand) handleDeleteByFilter(filterExpr string) (int, error) {

// TODO: querying for millions of evals is very expensive... can we add a count RPC?

// evals, _, err := e.client.Evaluations().List(&api.QueryOptions{
// Filter: filterExpr,
// })
// if err != nil {
// return 1, err
// }
// evals := []string{}

// If the user did not wish to bypass the confirmation step, ask this now
// and handle the response.
// if !e.yes && !e.deleteByArg {
// code, deleteEvals := e.askQuestion(fmt.Sprintf(
// "Are you sure you want to delete %v evals? [y/N]",
// len(evals)), "Cancelling eval deletion")
// e.Ui.Output("")

// if !deleteEvals {
// return code, nil
// }
// }
if !e.yes && !e.deleteByArg {

count, _, err := e.client.Evaluations().Count(&api.QueryOptions{
Filter: filterExpr,
})
if err != nil {
return 1, err
}

code, deleteEvals := e.askQuestion(fmt.Sprintf(
"Are you sure you want to delete %d evals? [y/N]",
count), "Cancelling eval deletion")
e.Ui.Output("")

if !deleteEvals {
return code, nil
}
}

resp, _, err := e.client.Evaluations().DeleteOpts(&api.EvalDeleteRequest{
Filter: filterExpr,
Expand Down

0 comments on commit e7136a8

Please sign in to comment.