Skip to content

Commit

Permalink
calc in/outbound peers counter server side
Browse files Browse the repository at this point in the history
  • Loading branch information
skylenet committed Jul 10, 2024
1 parent 72cf809 commit f06010b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 48 deletions.
32 changes: 20 additions & 12 deletions handlers/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ func buildClientsPageData() (*models.ClientsPageData, time.Duration) {

peers := client.GetNodePeers()
resPeers := []*models.ClientPageDataClientPeers{}
for _, peer := range peers {

var inPeerCount, outPeerCount uint32
for _, peer := range peers {
peerAlias := peer.PeerID
peerType := "external"
if alias, ok := aliases[peer.PeerID]; ok {
Expand All @@ -167,26 +168,33 @@ func buildClientsPageData() (*models.ClientsPageData, time.Duration) {
Alias: peerAlias,
PeerType: peerType,
})

if peer.Direction == "inbound" {
inPeerCount++
} else {
outPeerCount++
}
}
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(),
Version: client.GetVersion(),
Peers: resPeers,
PeerId: client.GetPeerId(),
HeadSlot: uint64(lastHeadSlot),
HeadRoot: lastHeadRoot,
Status: client.GetStatus(),
LastRefresh: clientRefresh,
LastError: client.GetLastClientError(),
Index: int(client.GetIndex()) + 1,
Name: client.GetName(),
Version: client.GetVersion(),
Peers: resPeers,
PeerId: client.GetPeerId(),
PeersInboundCounter: inPeerCount,
PeersOutboundCounter: outPeerCount,
HeadSlot: uint64(lastHeadSlot),
HeadRoot: lastHeadRoot,
Status: client.GetStatus(),
LastRefresh: clientRefresh,
LastError: client.GetLastClientError(),
}
pageData.Clients = append(pageData.Clients, resClient)

Expand Down
27 changes: 2 additions & 25 deletions templates/clients/clients.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,6 @@ <h1 class="h4 mb-1 mb-md-0"><i class="fas fa-server mx-2"></i>Clients</h1>
$(".collapse.peerInfo").collapse("hide");
$("#peerInfo-" + params.nodes[0]).collapse("show");
});

var nodeStats = {};

data.nodes.forEach(element => {
if (element.group == "internal") {
nodeStats[element.id] = { alias: element.label };
}
});

clients.forEach(client => {
inboundPeerCount = 0;
outboundPeerCount = 0;
for (var i = 0; i < client.peers.length; i++) {
if (client.peers[i].direction == "inbound") {
inboundPeerCount++;
} else {
outboundPeerCount++;
}
}
nodeStats[client.peer_id].InboundPeerCount = inboundPeerCount
nodeStats[client.peer_id].OutboundPeerCount = outboundPeerCount
});

</script>
<div class="card mt-2">
<div class="card-body px-0 py-3">
Expand Down Expand Up @@ -204,11 +181,11 @@ <h1 class="h4 mb-1 mb-md-0"><i class="fas fa-server mx-2"></i>Clients</h1>
</td>
<td style="font-size: 0.8rem; vertical-align: middle;">
<span style="width:30px;display: inline-block;" class="text-success" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Inbound Peers">
<script type="text/javascript">document.write(nodeStats["{{ $client.PeerId }}"].InboundPeerCount);</script>
{{ $client.PeersInboundCounter}}
<i class="fa-solid fa-arrow-down"></i>
</span>
<span style="width:30px;display: inline-block;" class="text-danger" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Outbound Peers">
<script type="text/javascript">document.write(nodeStats["{{ $client.PeerId }}"].OutboundPeerCount);</script>
{{ $client.PeersOutboundCounter}}
<i class="fa-solid fa-arrow-up"></i>
</span>
<span style="width:30px;display: inline-block;" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Total Peers">
Expand Down
24 changes: 13 additions & 11 deletions types/models/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ type ClientsPageData struct {
}

type ClientsPageDataClient struct {
Index int `json:"index"`
Name string `json:"name"`
Version string `json:"version"`
HeadSlot uint64 `json:"head_slot"`
HeadRoot []byte `json:"head_root"`
Status string `json:"status"`
LastRefresh time.Time `json:"refresh"`
LastError string `json:"error"`
PeerId string `json:"peer_id"`
Peers []*ClientPageDataClientPeers `json:"peers"`
Index int `json:"index"`
Name string `json:"name"`
Version string `json:"version"`
HeadSlot uint64 `json:"head_slot"`
HeadRoot []byte `json:"head_root"`
Status string `json:"status"`
LastRefresh time.Time `json:"refresh"`
LastError string `json:"error"`
PeerId string `json:"peer_id"`
Peers []*ClientPageDataClientPeers `json:"peers"`
PeersInboundCounter uint32 `json:"peers_inbound_counter"`
PeersOutboundCounter uint32 `json:"peers_outbound_counter"`
}

type ClientPageDataClientPeers struct {
PeerID string `json:"peer_id"`
Alias string `json:"alias"`
PeerType string `json"type"`
PeerType string `json:"type"`
State string `json:"state"`
Direction string `json:"direction"`
}
Expand Down

0 comments on commit f06010b

Please sign in to comment.