@@ -70,8 +70,24 @@ func raftListPeers(client *api.Client, stale bool) (string, error) {
7070 return "" , fmt .Errorf ("Failed to retrieve raft configuration: %v" , err )
7171 }
7272
73+ leaderLastCommitIndex := uint64 (0 )
74+ serverIdLastIndexMap := make (map [string ]uint64 )
75+
76+ for _ , raftServer := range reply .Servers {
77+ serverIdLastIndexMap [raftServer .ID ] = raftServer .LastIndex
78+ }
79+
80+ for _ , s := range reply .Servers {
81+ if s .Leader {
82+ lastIndex , ok := serverIdLastIndexMap [s .ID ]
83+ if ok {
84+ leaderLastCommitIndex = lastIndex
85+ }
86+ }
87+ }
88+
7389 // Format it as a nice table.
74- result := []string {"Node\x1f ID\x1f Address\x1f State\x1f Voter\x1f RaftProtocol" }
90+ result := []string {"Node\x1f ID\x1f Address\x1f State\x1f Voter\x1f RaftProtocol\x1f Commit Index \x1f Trails Leader By " }
7591 for _ , s := range reply .Servers {
7692 raftProtocol := s .ProtocolVersion
7793
@@ -82,8 +98,20 @@ func raftListPeers(client *api.Client, stale bool) (string, error) {
8298 if s .Leader {
8399 state = "leader"
84100 }
85- result = append (result , fmt .Sprintf ("%s\x1f %s\x1f %s\x1f %s\x1f %v\x1f %s" ,
86- s .Node , s .ID , s .Address , state , s .Voter , raftProtocol ))
101+
102+ trailsLeaderByText := "-"
103+ serverLastIndex , ok := serverIdLastIndexMap [s .ID ]
104+ if ok {
105+ trailsLeaderBy := leaderLastCommitIndex - serverLastIndex
106+ trailsLeaderByText = fmt .Sprintf ("%d commits" , trailsLeaderBy )
107+ if s .Leader {
108+ trailsLeaderByText = "-"
109+ } else if trailsLeaderBy == 1 {
110+ trailsLeaderByText = fmt .Sprintf ("%d commit" , trailsLeaderBy )
111+ }
112+ }
113+ result = append (result , fmt .Sprintf ("%s\x1f %s\x1f %s\x1f %s\x1f %v\x1f %s\x1f %v\x1f %s" ,
114+ s .Node , s .ID , s .Address , state , s .Voter , raftProtocol , serverLastIndex , trailsLeaderByText ))
87115 }
88116
89117 return columnize .Format (result , & columnize.Config {Delim : string ([]byte {0x1f })}), nil
0 commit comments