Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #291 from github/topology-ascii-orchestrator-client
Browse files Browse the repository at this point in the history
Topology ascii orchestrator client
  • Loading branch information
Shlomi Noach authored Sep 12, 2017
2 parents 3b797eb + ec50ccf commit 85ad11b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions go/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,7 @@ func (this *HttpAPI) AsciiTopology(params martini.Params, r render.Render, req *
Respond(r, &APIResponse{Code: ERROR, Message: fmt.Sprintf("%+v", err)})
return
}
asciiOutput = strings.Replace(asciiOutput, " ", "\u00a0", -1)

Respond(r, &APIResponse{Code: OK, Message: fmt.Sprintf("Topology for cluster %s", clusterName), Details: asciiOutput})
}
Expand Down
21 changes: 12 additions & 9 deletions go/inst/instance_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ const (
StopReplicationNicely = "StopReplicationNicely"
)

var asciiFillerCharacter = " "

// getASCIITopologyEntry will get an ascii topology tree rooted at given instance. Ir recursively
// draws the tree
func getASCIITopologyEntry(depth int, instance *Instance, replicationMap map[*Instance]([]*Instance), extendedOutput bool) []string {
func getASCIITopologyEntry(depth int, instance *Instance, replicationMap map[*Instance]([]*Instance), extendedOutput bool, fillerCharacter string) []string {
if instance == nil {
return []string{}
}
Expand All @@ -48,27 +50,28 @@ func getASCIITopologyEntry(depth int, instance *Instance, replicationMap map[*In
}
prefix := ""
if depth > 0 {
prefix = strings.Repeat(" ", (depth-1)*2)
prefix = strings.Repeat(fillerCharacter, (depth-1)*2)
if instance.ReplicaRunning() && instance.IsLastCheckValid && instance.IsRecentlyChecked {
prefix += "+ "
prefix += "+" + fillerCharacter
} else {
prefix += "- "
prefix += "-" + fillerCharacter
}
}
entry := fmt.Sprintf("%s%s", prefix, instance.Key.DisplayString())
if extendedOutput {
entry = fmt.Sprintf("%s %s", entry, instance.HumanReadableDescription())
entry = fmt.Sprintf("%s%s%s", entry, fillerCharacter, instance.HumanReadableDescription())
}
result := []string{entry}
for _, replica := range replicationMap[instance] {
replicasResult := getASCIITopologyEntry(depth+1, replica, replicationMap, extendedOutput)
replicasResult := getASCIITopologyEntry(depth+1, replica, replicationMap, extendedOutput, fillerCharacter)
result = append(result, replicasResult...)
}
return result
}

// ASCIITopology returns a string representation of the topology of given cluster.
func ASCIITopology(clusterName string, historyTimestampPattern string) (result string, err error) {
fillerCharacter := asciiFillerCharacter
var instances [](*Instance)
if historyTimestampPattern == "" {
instances, err = ReadClusterInstances(clusterName)
Expand Down Expand Up @@ -103,12 +106,12 @@ func ASCIITopology(clusterName string, historyTimestampPattern string) (result s
var entries []string
if masterInstance != nil {
// Single master
entries = getASCIITopologyEntry(0, masterInstance, replicationMap, historyTimestampPattern == "")
entries = getASCIITopologyEntry(0, masterInstance, replicationMap, historyTimestampPattern == "", fillerCharacter)
} else {
// Co-masters? For visualization we put each in its own branch while ignoring its other co-masters.
for _, instance := range instances {
if instance.IsCoMaster {
entries = append(entries, getASCIITopologyEntry(1, instance, replicationMap, historyTimestampPattern == "")...)
entries = append(entries, getASCIITopologyEntry(1, instance, replicationMap, historyTimestampPattern == "", fillerCharacter)...)
}
}
}
Expand All @@ -122,7 +125,7 @@ func ASCIITopology(clusterName string, historyTimestampPattern string) (result s
entryIndent := strings.Index(entry, "[")
if maxIndent > entryIndent {
tokens := strings.Split(entry, "[")
newEntry := fmt.Sprintf("%s%s[%s", tokens[0], strings.Repeat(" ", maxIndent-entryIndent), tokens[1])
newEntry := fmt.Sprintf("%s%s[%s", tokens[0], strings.Repeat(fillerCharacter, maxIndent-entryIndent), tokens[1])
entries[i] = newEntry
}
}
Expand Down

0 comments on commit 85ad11b

Please sign in to comment.