Skip to content

Commit

Permalink
sort peers by names and types
Browse files Browse the repository at this point in the history
  • Loading branch information
skylenet committed Jul 10, 2024
1 parent 4071910 commit a039de8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
22 changes: 22 additions & 0 deletions handlers/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func buildClientsPageData() (*models.ClientsPageData, time.Duration) {
}
cacheTime := time.Duration(utils.Config.Chain.Config.SecondsPerSlot) * time.Second

aliases := map[string]string{}
for _, client := range services.GlobalBeaconService.GetClients() {
aliases[client.GetPeerId()] = client.GetName()
}

for _, client := range services.GlobalBeaconService.GetClients() {
lastHeadSlot, lastHeadRoot, clientRefresh := client.GetLastHead()
if lastHeadSlot < 0 {
Expand All @@ -148,12 +153,29 @@ func buildClientsPageData() (*models.ClientsPageData, time.Duration) {
peers := client.GetNodePeers()
resPeers := []*models.ClientPageDataClientPeers{}
for _, peer := range peers {

peerAlias := peer.PeerID
peerType := "external"
if alias, ok := aliases[peer.PeerID]; ok {
peerAlias = alias
peerType = "internal"
}
resPeers = append(resPeers, &models.ClientPageDataClientPeers{
PeerID: peer.PeerID,
State: peer.State,
Direction: peer.Direction,
Alias: peerAlias,
PeerType: peerType,
})
}
sort.Slice(resPeers, func(i, j int) bool {
if resPeers[i].PeerType == resPeers[j].PeerType {
return resPeers[i].Alias < resPeers[j].Alias
}

return resPeers[i].PeerType > resPeers[j].PeerType
})

resClient := &models.ClientsPageDataClient{
Index: int(client.GetIndex()) + 1,
Name: client.GetName(),
Expand Down
18 changes: 2 additions & 16 deletions templates/clients/clients.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,7 @@ <h1 class="h4 mb-1 mb-md-0"><i class="fas fa-server mx-2"></i>Clients</h1>
<div style="padding-left: 20px;">
<img src="/identicon?key={{ $peer.PeerID }}" class="peer-table-icon {{ $peer.State }}" alt="{{ $peer.State }}"/>
<code data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="{{ $peer.PeerID }}">
<script type="text/javascript">
if ("{{ $peer.PeerID }}" in nodeStats){
node = nodeStats["{{ $peer.PeerID }}"];
document.write(node.alias);
} else {
document.write("{{ $peer.PeerID }}");
}
</script>
{{ $peer.Alias }}
</code>
</div>
{{end}}
Expand All @@ -272,14 +265,7 @@ <h1 class="h4 mb-1 mb-md-0"><i class="fas fa-server mx-2"></i>Clients</h1>
<img src="/identicon?key={{ $peer.PeerID }}" class="peer-table-icon {{ $peer.State }}" alt="{{ $peer.State }}"/>

<code data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="{{ $peer.PeerID }}">
<script type="text/javascript">
if ("{{ $peer.PeerID }}" in nodeStats){
node = nodeStats["{{ $peer.PeerID }}"];
document.write(node.alias);
} else {
document.write("{{ $peer.PeerID }}");
}
</script>
{{ $peer.Alias }}
</code>

</div>
Expand Down
2 changes: 2 additions & 0 deletions types/models/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ClientsPageDataClient struct {

type ClientPageDataClientPeers struct {
PeerID string `json:"peer_id"`
Alias string `json:"alias"`
PeerType string `json"type"`

Check failure on line 30 in types/models/clients.go

View workflow job for this annotation

GitHub Actions / Run code checks / Run code checks

struct field tag `json"type"` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair
State string `json:"state"`
Direction string `json:"direction"`
}
Expand Down

0 comments on commit a039de8

Please sign in to comment.