From 61b8249d087c63c57397dda5dff72e72a5250f6f Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Thu, 17 Sep 2015 12:40:51 -0700 Subject: [PATCH 1/2] api: sort all list responses --- api/allocations.go | 17 +++++++++++++++++ api/allocations_test.go | 20 ++++++++++++++++++++ api/evaluations.go | 21 +++++++++++++++++++++ api/evaluations_test.go | 20 ++++++++++++++++++++ api/jobs.go | 22 ++++++++++++++++++++++ api/nodes.go | 20 +++++++++++++++++++- api/nodes_test.go | 20 ++++++++++++++++++++ 7 files changed, 139 insertions(+), 1 deletion(-) diff --git a/api/allocations.go b/api/allocations.go index 32fbf8714..fe362a779 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -1,6 +1,7 @@ package api import ( + "sort" "time" ) @@ -21,6 +22,7 @@ func (a *Allocations) List(q *QueryOptions) ([]*AllocationListStub, *QueryMeta, if err != nil { return nil, nil, err } + sort.Sort(AllocIndexSort(resp)) return resp, qm, nil } @@ -84,3 +86,18 @@ type AllocationListStub struct { CreateIndex uint64 ModifyIndex uint64 } + +// AllocIndexSort reverse sorts allocs by CreateIndex. +type AllocIndexSort []*AllocationListStub + +func (a AllocIndexSort) Len() int { + return len(a) +} + +func (a AllocIndexSort) Less(i, j int) bool { + return a[i].CreateIndex > a[j].CreateIndex +} + +func (a AllocIndexSort) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} diff --git a/api/allocations_test.go b/api/allocations_test.go index be1860eee..c8fde832f 100644 --- a/api/allocations_test.go +++ b/api/allocations_test.go @@ -1,6 +1,8 @@ package api import ( + "reflect" + "sort" "testing" ) @@ -49,3 +51,21 @@ func TestAllocations_List(t *testing.T) { t.Fatalf("bad: %#v", allocs) } } + +func TestAllocations_CreateIndexSort(t *testing.T) { + allocs := []*AllocationListStub{ + &AllocationListStub{CreateIndex: 2}, + &AllocationListStub{CreateIndex: 1}, + &AllocationListStub{CreateIndex: 5}, + } + sort.Sort(AllocIndexSort(allocs)) + + expect := []*AllocationListStub{ + &AllocationListStub{CreateIndex: 5}, + &AllocationListStub{CreateIndex: 2}, + &AllocationListStub{CreateIndex: 1}, + } + if !reflect.DeepEqual(allocs, expect) { + t.Fatalf("\n\n%#v\n\n%#v", allocs, expect) + } +} diff --git a/api/evaluations.go b/api/evaluations.go index 52a0db970..9fbb83b5c 100644 --- a/api/evaluations.go +++ b/api/evaluations.go @@ -1,6 +1,7 @@ package api import ( + "sort" "time" ) @@ -21,6 +22,7 @@ func (e *Evaluations) List(q *QueryOptions) ([]*Evaluation, *QueryMeta, error) { if err != nil { return nil, nil, err } + sort.Sort(EvalIndexSort(resp)) return resp, qm, nil } @@ -42,6 +44,7 @@ func (e *Evaluations) Allocations(evalID string, q *QueryOptions) ([]*Allocation if err != nil { return nil, nil, err } + sort.Sort(AllocIndexSort(resp)) return resp, qm, nil } @@ -60,4 +63,22 @@ type Evaluation struct { Wait time.Duration NextEval string PreviousEval string + CreateIndex uint64 + ModifyIndex uint64 +} + +// EvalIndexSort is a wrapper to sort evaluations by CreateIndex. +// We reverse the test so that we get the highest index first. +type EvalIndexSort []*Evaluation + +func (e EvalIndexSort) Len() int { + return len(e) +} + +func (e EvalIndexSort) Less(i, j int) bool { + return e[i].CreateIndex > e[j].CreateIndex +} + +func (e EvalIndexSort) Swap(i, j int) { + e[i], e[j] = e[j], e[i] } diff --git a/api/evaluations_test.go b/api/evaluations_test.go index 03bd1be53..c7772dc96 100644 --- a/api/evaluations_test.go +++ b/api/evaluations_test.go @@ -1,6 +1,8 @@ package api import ( + "reflect" + "sort" "strings" "testing" ) @@ -94,3 +96,21 @@ func TestEvaluations_Allocations(t *testing.T) { t.Fatalf("expected 0 allocs, got: %d", n) } } + +func TestEvaluations_Sort(t *testing.T) { + evals := []*Evaluation{ + &Evaluation{CreateIndex: 2}, + &Evaluation{CreateIndex: 1}, + &Evaluation{CreateIndex: 5}, + } + sort.Sort(EvalIndexSort(evals)) + + expect := []*Evaluation{ + &Evaluation{CreateIndex: 5}, + &Evaluation{CreateIndex: 2}, + &Evaluation{CreateIndex: 1}, + } + if !reflect.DeepEqual(evals, expect) { + t.Fatalf("\n\n%#v\n\n%#v", evals, expect) + } +} diff --git a/api/jobs.go b/api/jobs.go index c0003a4e5..90ba82061 100644 --- a/api/jobs.go +++ b/api/jobs.go @@ -1,5 +1,9 @@ package api +import ( + "sort" +) + const ( // JobTypeService indicates a long-running processes JobTypeService = "service" @@ -38,6 +42,7 @@ func (j *Jobs) List(q *QueryOptions) ([]*JobListStub, *QueryMeta, error) { if err != nil { return nil, qm, err } + sort.Sort(JobIDSort(resp)) return resp, qm, nil } @@ -59,6 +64,7 @@ func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*AllocationListStub if err != nil { return nil, nil, err } + sort.Sort(AllocIndexSort(resp)) return resp, qm, nil } @@ -70,6 +76,7 @@ func (j *Jobs) Evaluations(jobID string, q *QueryOptions) ([]*Evaluation, *Query if err != nil { return nil, nil, err } + sort.Sort(EvalIndexSort(resp)) return resp, qm, nil } @@ -123,6 +130,21 @@ type JobListStub struct { ModifyIndex uint64 } +// JobIDSort is used to sort jobs by their job ID's. +type JobIDSort []*JobListStub + +func (j JobIDSort) Len() int { + return len(j) +} + +func (j JobIDSort) Less(a, b int) bool { + return j[a].ID < j[b].ID +} + +func (j JobIDSort) Swap(a, b int) { + j[a], j[b] = j[b], j[a] +} + // NewServiceJob creates and returns a new service-style job // for long-lived processes using the provided name, ID, and // relative job priority. diff --git a/api/nodes.go b/api/nodes.go index 77c98e662..4ee3c7242 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -1,6 +1,7 @@ package api import ( + "sort" "strconv" ) @@ -16,11 +17,12 @@ func (c *Client) Nodes() *Nodes { // List is used to list out all of the nodes func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) { - var resp []*NodeListStub + var resp NodeIndexSort qm, err := n.client.query("/v1/nodes", &resp, q) if err != nil { return nil, nil, err } + sort.Sort(NodeIndexSort(resp)) return resp, qm, nil } @@ -51,6 +53,7 @@ func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*AllocationListSt if err != nil { return nil, nil, err } + sort.Sort(AllocIndexSort(resp)) return resp, qm, nil } @@ -95,6 +98,21 @@ type NodeListStub struct { ModifyIndex uint64 } +// NodeIndexSort reverse sorts nodes by CreateIndex +type NodeIndexSort []*NodeListStub + +func (n NodeIndexSort) Len() int { + return len(n) +} + +func (n NodeIndexSort) Less(i, j int) bool { + return n[i].CreateIndex > n[j].CreateIndex +} + +func (n NodeIndexSort) Swap(i, j int) { + n[i], n[j] = n[j], n[i] +} + // nodeEvalResponse is used to decode a force-eval. type nodeEvalResponse struct { EvalID string diff --git a/api/nodes_test.go b/api/nodes_test.go index 5d71b8304..23bd1af9c 100644 --- a/api/nodes_test.go +++ b/api/nodes_test.go @@ -2,6 +2,8 @@ package api import ( "fmt" + "reflect" + "sort" "strings" "testing" @@ -201,3 +203,21 @@ func TestNodes_ForceEvaluate(t *testing.T) { t.Fatalf("err: %s", err) } } + +func TestNodes_Sort(t *testing.T) { + nodes := []*NodeListStub{ + &NodeListStub{CreateIndex: 2}, + &NodeListStub{CreateIndex: 1}, + &NodeListStub{CreateIndex: 5}, + } + sort.Sort(NodeIndexSort(nodes)) + + expect := []*NodeListStub{ + &NodeListStub{CreateIndex: 5}, + &NodeListStub{CreateIndex: 2}, + &NodeListStub{CreateIndex: 1}, + } + if !reflect.DeepEqual(nodes, expect) { + t.Fatalf("\n\n%#v\n\n%#v", nodes, expect) + } +} From c1aa89691886ee463a3820675310c8955ce4e050 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Thu, 17 Sep 2015 13:15:45 -0700 Subject: [PATCH 2/2] api: test job sort --- api/jobs_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/api/jobs_test.go b/api/jobs_test.go index e42420db2..d421dde67 100644 --- a/api/jobs_test.go +++ b/api/jobs_test.go @@ -2,6 +2,7 @@ package api import ( "reflect" + "sort" "strings" "testing" ) @@ -303,3 +304,21 @@ func TestJobs_Constrain(t *testing.T) { t.Fatalf("expect: %#v, got: %#v", expect, job.Constraints) } } + +func TestJobs_Sort(t *testing.T) { + jobs := []*JobListStub{ + &JobListStub{ID: "job2"}, + &JobListStub{ID: "job0"}, + &JobListStub{ID: "job1"}, + } + sort.Sort(JobIDSort(jobs)) + + expect := []*JobListStub{ + &JobListStub{ID: "job0"}, + &JobListStub{ID: "job1"}, + &JobListStub{ID: "job2"}, + } + if !reflect.DeepEqual(jobs, expect) { + t.Fatalf("\n\n%#v\n\n%#v", jobs, expect) + } +}