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

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed Nov 20, 2015
1 parent 14eaef0 commit 55657ea
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions prog/weaver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ var rootTemplate = template.New("root").Funcs(map[string]interface{}{
getNickName := func(peer mesh.PeerName) string {
name, found := status.Nicknames[peer]
if !found {
return fmt.Sprintf("%s", peer)
return peer.String()
}
return name
}
getOrCreateNicknameStats := func(peer mesh.PeerName) *NicknameStats {
peerName := getNickName(peer)
stats, found := nicknameStats[peerName]
if !found {
stats = &NicknameStats{
Ranges: []address.Range{},
Addresses: []address.Address{},
Reachable: status.IsKnownPeer(peer),
}
stats = &NicknameStats{Reachable: status.IsKnownPeer(peer)}
nicknameStats[peerName] = stats
}
return stats
Expand All @@ -68,31 +64,25 @@ var rootTemplate = template.New("root").Funcs(map[string]interface{}{
stats.Ranges = append(stats.Ranges, ranges...)
}

appendAddresses := func(displayName string, reachable bool, addresses []address.Address, ranges []address.Range) {
ourNickname := getNickName(status.OurName)

// and then the rest
for nickname, stats := range nicknameStats {
if nickname == ourNickname {
nickname = "(local)"
}
reachableStr := ""
if !reachable {
if !stats.Reachable {
reachableStr = "- unreachable!"
}
ipsInRange := 0
for _, chunk := range ranges {
for _, chunk := range stats.Ranges {
ipsInRange += int(chunk.Size())
}
percentageRanges := float32(ipsInRange) * 100.0 / float32(status.RangeNumIPs)

fmt.Fprintf(&buffer, "%20s: %8d IPs (%04.1f%% of universe, in %3d ranges) - used: %8d IPs %s\n",
displayName, ipsInRange, percentageRanges, len(ranges), len(addresses), reachableStr)
}

// print the local addresses
ourNickname := getNickName(status.OurName)
ourStats := nicknameStats[ourNickname]
appendAddresses("(local)", true, ourStats.Addresses, ourStats.Ranges)

// and then the rest
for nickname, stats := range nicknameStats {
if nickname != ourNickname {
appendAddresses(nickname, stats.Reachable, stats.Addresses, stats.Ranges)
}
nickname, ipsInRange, percentageRanges, len(stats.Ranges), len(stats.Addresses), reachableStr)
}

return buffer.String()
Expand Down

0 comments on commit 55657ea

Please sign in to comment.