github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/api/evaluations.go (about)

     1  package api
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  )
     7  
     8  // Evaluations is used to query the evaluation endpoints.
     9  type Evaluations struct {
    10  	client *Client
    11  }
    12  
    13  // Evaluations returns a new handle on the evaluations.
    14  func (c *Client) Evaluations() *Evaluations {
    15  	return &Evaluations{client: c}
    16  }
    17  
    18  // List is used to dump all of the evaluations.
    19  func (e *Evaluations) List(q *QueryOptions) ([]*Evaluation, *QueryMeta, error) {
    20  	var resp []*Evaluation
    21  	qm, err := e.client.query("/v1/evaluations", &resp, q)
    22  	if err != nil {
    23  		return nil, nil, err
    24  	}
    25  	sort.Sort(EvalIndexSort(resp))
    26  	return resp, qm, nil
    27  }
    28  
    29  func (e *Evaluations) PrefixList(prefix string) ([]*Evaluation, *QueryMeta, error) {
    30  	return e.List(&QueryOptions{Prefix: prefix})
    31  }
    32  
    33  // Info is used to query a single evaluation by its ID.
    34  func (e *Evaluations) Info(evalID string, q *QueryOptions) (*Evaluation, *QueryMeta, error) {
    35  	var resp Evaluation
    36  	qm, err := e.client.query("/v1/evaluation/"+evalID, &resp, q)
    37  	if err != nil {
    38  		return nil, nil, err
    39  	}
    40  	return &resp, qm, nil
    41  }
    42  
    43  // Allocations is used to retrieve a set of allocations given
    44  // an evaluation ID.
    45  func (e *Evaluations) Allocations(evalID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
    46  	var resp []*AllocationListStub
    47  	qm, err := e.client.query("/v1/evaluation/"+evalID+"/allocations", &resp, q)
    48  	if err != nil {
    49  		return nil, nil, err
    50  	}
    51  	sort.Sort(AllocIndexSort(resp))
    52  	return resp, qm, nil
    53  }
    54  
    55  // Evaluation is used to serialize an evaluation.
    56  type Evaluation struct {
    57  	ID                string
    58  	Priority          int
    59  	Type              string
    60  	TriggeredBy       string
    61  	JobID             string
    62  	JobModifyIndex    uint64
    63  	NodeID            string
    64  	NodeModifyIndex   uint64
    65  	Status            string
    66  	StatusDescription string
    67  	Wait              time.Duration
    68  	NextEval          string
    69  	PreviousEval      string
    70  	BlockedEval       string
    71  	FailedTGAllocs    map[string]*AllocationMetric
    72  	CreateIndex       uint64
    73  	ModifyIndex       uint64
    74  }
    75  
    76  // EvalIndexSort is a wrapper to sort evaluations by CreateIndex.
    77  // We reverse the test so that we get the highest index first.
    78  type EvalIndexSort []*Evaluation
    79  
    80  func (e EvalIndexSort) Len() int {
    81  	return len(e)
    82  }
    83  
    84  func (e EvalIndexSort) Less(i, j int) bool {
    85  	return e[i].CreateIndex > e[j].CreateIndex
    86  }
    87  
    88  func (e EvalIndexSort) Swap(i, j int) {
    89  	e[i], e[j] = e[j], e[i]
    90  }