Skip to content

Commit

Permalink
Fix dashboard address (pingcap#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
baurine authored Jun 3, 2020
1 parent a382528 commit a22dda1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ bin/
/tests/tiup_home/
/tests/tiup_mirrors/*.sha1
/tests/tiup_mirrors/
/logs
24 changes: 14 additions & 10 deletions components/cluster/command/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,26 @@ func displayClusterTopology(clusterName string, opt *operator.Options) error {
func formatInstanceStatus(status string) string {
lowercaseStatus := strings.ToLower(status)

startsWith := func(prefixs ...string) bool {
for _, prefix := range prefixs {
if strings.HasPrefix(lowercaseStatus, prefix) {
return true
}
}
return false
}

switch {
case strings.HasPrefix(lowercaseStatus, "healthy|l"): // healthy|l , healthy|l|ui
case startsWith("healthy|l"): // healthy|l, healthy|l|ui
return color.HiGreenString(status)
case strings.HasPrefix(lowercaseStatus, "healthy"): // healthy , healthy|ui
case startsWith("healthy"): // healthy, healthy|ui
return color.GreenString(status)
case strings.HasPrefix(lowercaseStatus, "unhealthy"): // unhealthy , unhealthy|ui
case startsWith("unhealthy", "down", "err"): // unhealthy/down/err, unhealthy/down/err|ui
return color.RedString(status)
}

switch lowercaseStatus {
case "up":
case startsWith("up"):
return color.GreenString(status)
case "offline", "tombstone", "disconnected":
case startsWith("offline", "tombstone", "disconnected"):
return color.YellowString(status)
case "down", "err":
return color.RedString(status)
default:
return status
}
Expand Down
37 changes: 21 additions & 16 deletions pkg/cluster/meta/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,40 +278,45 @@ type PDSpec struct {

// Status queries current status of the instance
func (s PDSpec) Status(pdList ...string) string {
pdapi := api.NewPDClient([]string{fmt.Sprintf("%s:%d", s.Host, s.ClientPort)},
statusQueryTimeout, nil)
healths, err := pdapi.GetHealth()
curAddr := fmt.Sprintf("%s:%d", s.Host, s.ClientPort)
curPdAPI := api.NewPDClient([]string{curAddr}, statusQueryTimeout, nil)
allPdAPI := api.NewPDClient(pdList, statusQueryTimeout, nil)
suffix := ""

// find dashboard node
dashboardAddr, _ := allPdAPI.GetDashboardAddress()
if strings.HasPrefix(dashboardAddr, "http") {
r := strings.NewReplacer("http://", "", "https://", "")
dashboardAddr = r.Replace(dashboardAddr)
}
if dashboardAddr == curAddr {
suffix = "|UI"
}

healths, err := curPdAPI.GetHealth()
if err != nil {
return "Down"
return "Down" + suffix
}

// find leader node
leader, err := pdapi.GetLeader()
leader, err := curPdAPI.GetLeader()
if err != nil {
return "ERR"
return "ERR" + suffix
}

// find dashboard node
dashboardAddr, _ := pdapi.GetDashboardAddress()

for _, member := range healths.Healths {
suffix := ""
clientURL := member.ClientUrls[0]
if s.Name != member.Name {
continue
}
if s.Name == leader.Name {
suffix += "|L"
}
if clientURL == dashboardAddr {
suffix += "|UI"
suffix = "|L" + suffix
}
if member.Health {
return "Healthy" + suffix
}
return "Unhealthy" + suffix
}
return "N/A"
return "N/A" + suffix
}

// Role returns the component role of the instance
Expand Down

0 comments on commit a22dda1

Please sign in to comment.