github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/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  	Namespace            string
    62  	JobID                string
    63  	JobModifyIndex       uint64
    64  	NodeID               string
    65  	NodeModifyIndex      uint64
    66  	DeploymentID         string
    67  	Status               string
    68  	StatusDescription    string
    69  	Wait                 time.Duration
    70  	NextEval             string
    71  	PreviousEval         string
    72  	BlockedEval          string
    73  	FailedTGAllocs       map[string]*AllocationMetric
    74  	ClassEligibility     map[string]bool
    75  	EscapedComputedClass bool
    76  	QuotaLimitReached    string
    77  	AnnotatePlan         bool
    78  	QueuedAllocations    map[string]int
    79  	SnapshotIndex        uint64
    80  	CreateIndex          uint64
    81  	ModifyIndex          uint64
    82  }
    83  
    84  // EvalIndexSort is a wrapper to sort evaluations by CreateIndex.
    85  // We reverse the test so that we get the highest index first.
    86  type EvalIndexSort []*Evaluation
    87  
    88  func (e EvalIndexSort) Len() int {
    89  	return len(e)
    90  }
    91  
    92  func (e EvalIndexSort) Less(i, j int) bool {
    93  	return e[i].CreateIndex > e[j].CreateIndex
    94  }
    95  
    96  func (e EvalIndexSort) Swap(i, j int) {
    97  	e[i], e[j] = e[j], e[i]
    98  }