From 258e2c153d2327efd8c000f2661471ee30c38e86 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Sat, 17 Oct 2015 16:02:06 +0000 Subject: [PATCH] Sort the structs returned by /api/topology --- app/api_topologies.go | 12 +++++------ app/router.go | 50 +++++++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/app/api_topologies.go b/app/api_topologies.go index e5495a772..8b3160e11 100644 --- a/app/api_topologies.go +++ b/app/api_topologies.go @@ -38,21 +38,21 @@ func makeTopologyList(rep xfer.Reporter) func(w http.ResponseWriter, r *http.Req rpt = rep.Report() topologies = []APITopologyDesc{} ) - topologyRegistry.walk(func(name string, def topologyView, subDefs map[string]topologyView) { + topologyRegistry.walk(func(def topologyViewAndID, subDefs topologyViewAndIDs) { describedSubDefs := []APITopologyDesc{} - for subName, subDef := range subDefs { + for _, subDef := range subDefs { describedSubDefs = append(describedSubDefs, APITopologyDesc{ Name: subDef.human, - URL: "/api/topology/" + subName, - Options: makeTopologyOptions(subDef), + URL: "/api/topology/" + subDef.id, + Options: makeTopologyOptions(subDef.topologyView), Stats: stats(subDef.renderer, rpt), }) } topologies = append(topologies, APITopologyDesc{ Name: def.human, - URL: "/api/topology/" + name, + URL: "/api/topology/" + def.id, SubTopologies: describedSubDefs, - Options: makeTopologyOptions(def), + Options: makeTopologyOptions(def.topologyView), Stats: stats(def.renderer, rpt), }) }) diff --git a/app/router.go b/app/router.go index 93f58a1cd..a5bd928ab 100644 --- a/app/router.go +++ b/app/router.go @@ -5,6 +5,7 @@ import ( "encoding/gob" "net/http" "net/url" + "sort" "strings" "sync" @@ -143,6 +144,32 @@ type registry struct { items map[string]topologyView } +type topologyViewAndID struct { + topologyView + id string +} + +type topologyViewAndIDs []topologyViewAndID + +func (a topologyViewAndIDs) Len() int { return len(a) } +func (a topologyViewAndIDs) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a topologyViewAndIDs) Less(i, j int) bool { return a[i].id < a[j].id } + +func (r *registry) collectTopologyViews(f func(topologyView) bool) topologyViewAndIDs { + result := topologyViewAndIDs{} + for name, def := range r.items { + if !f(def) { + continue + } + result = append(result, topologyViewAndID{ + topologyView: def, + id: name, + }) + } + sort.Sort(result) + return result +} + func (r *registry) add(ts map[string]topologyView) { r.Lock() defer r.Unlock() @@ -163,23 +190,18 @@ func (r *registry) get(name string) (topologyView, bool) { return t, ok } -func (r *registry) walk(f func(string, topologyView, map[string]topologyView)) { +func (r *registry) walk(f func(topologyViewAndID, topologyViewAndIDs)) { r.RLock() defer r.RUnlock() - for name, def := range r.items { - if def.parent != "" { - continue - } + parents := r.collectTopologyViews(func(def topologyView) bool { + return def.parent == "" + }) - subDefs := map[string]topologyView{} - for subName, subDef := range r.items { - if subDef.parent != name { - continue - } - subDefs[subName] = subDef - } - - f(name, def, subDefs) + for _, parent := range parents { + subDefs := r.collectTopologyViews(func(def topologyView) bool { + return def.parent == parent.id + }) + f(parent, subDefs) } }