fix(peers): fixing sort on results

This commit is contained in:
Jon Lundy 2023-03-18 19:55:24 -06:00
parent ebd46555ac
commit f5e4c4f972
Signed by untrusted user who does not match committer: xuu
GPG Key ID: C63E6D61F3035024
4 changed files with 72 additions and 51 deletions

View File

@ -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]

View File

@ -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)
}

View File

@ -46,6 +46,7 @@
<h2>Results</h2>
{{ with $args := . }}
{{ 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}}

View File

@ -9,8 +9,7 @@
{{range .Requests}}
<h2>Results to {{.RequestIP}}{{if .Hidden}} 👁️{{end}}</h2>
{{with (orderByPeer .)}}
{{range .}}
{{range orderByPeer .}}
<div class="panel panel-primary" id="peer-{{.Nick}}">
<div class="panel-heading">
<b> {{.Country}} :: {{.Name}} :: {{.Nick}} </b>
@ -49,4 +48,3 @@
{{end}}
{{end}}
{{end}}
{{end}}