fix(peers): fixing sort on results
This commit is contained in:
parent
ebd46555ac
commit
f5e4c4f972
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -106,10 +107,11 @@ func (lis ListResponse) Len() int {
|
|||
return len(lis)
|
||||
}
|
||||
func (lis ListResponse) Less(i, j int) bool {
|
||||
if lis[i].Latency == 0.0 {
|
||||
if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 {
|
||||
return false
|
||||
}
|
||||
return lis[i].Latency < lis[j].Latency
|
||||
|
||||
return lis[j].Latency >= lis[i].Latency
|
||||
}
|
||||
func (lis ListResponse) Swap(i, j int) {
|
||||
lis[i], lis[j] = lis[j], lis[i]
|
||||
|
|
|
@ -4,11 +4,13 @@ import (
|
|||
"context"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
@ -91,7 +93,7 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
case strings.HasPrefix(r.URL.Path, "/peers/status"):
|
||||
s.state.Modify(r.Context(), func(ctx context.Context, state *state) error {
|
||||
for id, p := range state.peers {
|
||||
fmt.Fprintln(w, "PEER: ", id[24:], " ", p.Owner)
|
||||
fmt.Fprintln(w, "PEER:", id[24:], p.Owner, p.Name)
|
||||
}
|
||||
|
||||
for id, rq := range state.requests {
|
||||
|
@ -99,9 +101,6 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
for id, r := range rq.Responses {
|
||||
fmt.Fprintln(w, " RES: ", id, r.PeerID[24:], r.Latency, r.Jitter)
|
||||
}
|
||||
for p := range rq.peers {
|
||||
fmt.Fprintln(w, " PEER: ", p[24:])
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -172,7 +171,7 @@ func (s *service) getPending(w http.ResponseWriter, r *http.Request, peerID stri
|
|||
peerResults := &PeerResults{}
|
||||
peerResults.SetStreamID(aggPeer(peerID))
|
||||
err = s.es.Load(ctx, peerResults)
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, ev.ErrNotFound) {
|
||||
span.RecordError(fmt.Errorf("peer not found: %w", err))
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
@ -546,7 +545,7 @@ type peer struct {
|
|||
Jitter float64
|
||||
VPNTypes []string
|
||||
|
||||
Results []peerResult
|
||||
Results peerResults
|
||||
}
|
||||
type listPeer []peer
|
||||
|
||||
|
@ -554,22 +553,36 @@ func (lis listPeer) Len() int {
|
|||
return len(lis)
|
||||
}
|
||||
func (lis listPeer) Less(i, j int) bool {
|
||||
if lis[i].Latency == 0.0 {
|
||||
if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 {
|
||||
return false
|
||||
}
|
||||
return lis[i].Latency < lis[j].Latency
|
||||
return lis[j].Latency >= lis[i].Latency
|
||||
}
|
||||
func (lis listPeer) Swap(i, j int) {
|
||||
lis[i], lis[j] = lis[j], lis[i]
|
||||
}
|
||||
|
||||
type peerResults []peerResult
|
||||
|
||||
func (lis peerResults) Len() int {
|
||||
return len(lis)
|
||||
}
|
||||
func (lis peerResults) Less(i, j int) bool {
|
||||
if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 {
|
||||
return false
|
||||
}
|
||||
return lis[j].Latency >= lis[i].Latency
|
||||
}
|
||||
func (lis peerResults) Swap(i, j int) {
|
||||
lis[i], lis[j] = lis[j], lis[i]
|
||||
}
|
||||
|
||||
func fnOrderByPeer(rq *Request) any {
|
||||
|
||||
peers := make(map[string]peer)
|
||||
|
||||
sort.Sort(ListResponse(rq.Responses))
|
||||
|
||||
for _, rs := range rq.Responses {
|
||||
for i := range rq.Responses {
|
||||
rs := rq.Responses[i]
|
||||
p, ok := peers[rs.Peer.Owner]
|
||||
|
||||
if !ok {
|
||||
|
@ -593,7 +606,13 @@ func fnOrderByPeer(rq *Request) any {
|
|||
}
|
||||
|
||||
peerList := make(listPeer, 0, len(peers))
|
||||
for _, v := range peers {
|
||||
for i := range peers {
|
||||
v := peers[i]
|
||||
sort.Sort(v.Results)
|
||||
|
||||
v.Latency = v.Results[0].Latency
|
||||
v.Jitter = v.Results[0].Jitter
|
||||
|
||||
peerList = append(peerList, v)
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,8 @@
|
|||
<div class=row>
|
||||
<h2>Results</h2>
|
||||
{{ with $args := . }}
|
||||
{{range $req := .Requests}}
|
||||
{{ range $req := .Requests }}
|
||||
{{ if ne $req.RequestID "" }}
|
||||
<div class="panel panel-primary">
|
||||
<div class="panel-heading">
|
||||
<a href="/peers/req/{{ $req.RequestID }}">
|
||||
|
@ -58,6 +59,7 @@
|
|||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
{{end}}
|
||||
|
|
|
@ -9,44 +9,42 @@
|
|||
{{range .Requests}}
|
||||
<h2>Results to {{.RequestIP}}{{if .Hidden}} 👁️{{end}}</h2>
|
||||
|
||||
{{with (orderByPeer .)}}
|
||||
{{range .}}
|
||||
<div class="panel panel-primary" id="peer-{{.Nick}}">
|
||||
<div class="panel-heading">
|
||||
<b> {{.Country}} :: {{.Name}} :: {{.Nick}} </b>
|
||||
<div style='float:right'>
|
||||
<a class='btn btn-success' href="#peer-{{.Nick}}">{{ if eq .Latency 0.0 }}—{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<b>Note:</b> {{.Note}}<br/>
|
||||
<b>VPN Types:</b> {{range .VPNTypes}} {{.}} {{end}}<br/>
|
||||
<b>IRC:</b> {{.Nick}}
|
||||
<h4>Other Results</h4>
|
||||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Peer Name</th>
|
||||
<th>Country</th>
|
||||
<th>Latency</th>
|
||||
<th>Jitter</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Results}}
|
||||
<tr>
|
||||
<th>{{.Name}}</th>
|
||||
<td>{{.Country}}</td>
|
||||
<td>{{ if eq .Latency 0.0 }}—{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</td>
|
||||
<td>{{ if eq .Jitter 0.0 }}—{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
{{range orderByPeer .}}
|
||||
<div class="panel panel-primary" id="peer-{{.Nick}}">
|
||||
<div class="panel-heading">
|
||||
<b> {{.Country}} :: {{.Name}} :: {{.Nick}} </b>
|
||||
<div style='float:right'>
|
||||
<a class='btn btn-success' href="#peer-{{.Nick}}">{{ if eq .Latency 0.0 }}—{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</a>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
<div class="panel-body">
|
||||
<b>Note:</b> {{.Note}}<br/>
|
||||
<b>VPN Types:</b> {{range .VPNTypes}} {{.}} {{end}}<br/>
|
||||
<b>IRC:</b> {{.Nick}}
|
||||
<h4>Other Results</h4>
|
||||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Peer Name</th>
|
||||
<th>Country</th>
|
||||
<th>Latency</th>
|
||||
<th>Jitter</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Results}}
|
||||
<tr>
|
||||
<th>{{.Name}}</th>
|
||||
<td>{{.Country}}</td>
|
||||
<td>{{ if eq .Latency 0.0 }}—{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</td>
|
||||
<td>{{ if eq .Jitter 0.0 }}—{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
Loading…
Reference in New Issue
Block a user