github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/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  	ClassEligibility     map[string]bool
    73  	EscapedComputedClass bool
    74  	AnnotatePlan         bool
    75  	QueuedAllocations    map[string]int
    76  	SnapshotIndex        uint64
    77  	CreateIndex          uint64
    78  	ModifyIndex          uint64
    79  }
    80  
    81  // EvalIndexSort is a wrapper to sort evaluations by CreateIndex.
    82  // We reverse the test so that we get the highest index first.
    83  type EvalIndexSort []*Evaluation
    84  
    85  func (e EvalIndexSort) Len() int {
    86  	return len(e)
    87  }
    88  
    89  func (e EvalIndexSort) Less(i, j int) bool {
    90  	return e[i].CreateIndex > e[j].CreateIndex
    91  }
    92  
    93  func (e EvalIndexSort) Swap(i, j int) {
    94  	e[i], e[j] = e[j], e[i]
    95  }