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" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"net/netip" "net/netip"
"strconv" "strconv"
"time" "time"
@ -106,10 +107,11 @@ func (lis ListResponse) Len() int {
return len(lis) return len(lis)
} }
func (lis ListResponse) Less(i, j int) bool { 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 false
} }
return lis[i].Latency < lis[j].Latency
return lis[j].Latency >= lis[i].Latency
} }
func (lis ListResponse) Swap(i, j int) { func (lis ListResponse) Swap(i, j int) {
lis[i], lis[j] = lis[j], lis[i] lis[i], lis[j] = lis[j], lis[i]

View File

@ -4,11 +4,13 @@ import (
"context" "context"
"embed" "embed"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"io/fs" "io/fs"
"log" "log"
"math"
"net" "net"
"net/http" "net/http"
"sort" "sort"
@ -91,7 +93,7 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case strings.HasPrefix(r.URL.Path, "/peers/status"): case strings.HasPrefix(r.URL.Path, "/peers/status"):
s.state.Modify(r.Context(), func(ctx context.Context, state *state) error { s.state.Modify(r.Context(), func(ctx context.Context, state *state) error {
for id, p := range state.peers { 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 { 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 { for id, r := range rq.Responses {
fmt.Fprintln(w, " RES: ", id, r.PeerID[24:], r.Latency, r.Jitter) 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 return nil
@ -172,7 +171,7 @@ func (s *service) getPending(w http.ResponseWriter, r *http.Request, peerID stri
peerResults := &PeerResults{} peerResults := &PeerResults{}
peerResults.SetStreamID(aggPeer(peerID)) peerResults.SetStreamID(aggPeer(peerID))
err = s.es.Load(ctx, peerResults) 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)) span.RecordError(fmt.Errorf("peer not found: %w", err))
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }
@ -546,7 +545,7 @@ type peer struct {
Jitter float64 Jitter float64
VPNTypes []string VPNTypes []string
Results []peerResult Results peerResults
} }
type listPeer []peer type listPeer []peer
@ -554,22 +553,36 @@ func (lis listPeer) Len() int {
return len(lis) return len(lis)
} }
func (lis listPeer) Less(i, j int) bool { 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 false
} }
return lis[i].Latency < lis[j].Latency return lis[j].Latency >= lis[i].Latency
} }
func (lis listPeer) Swap(i, j int) { func (lis listPeer) Swap(i, j int) {
lis[i], lis[j] = lis[j], lis[i] 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 { func fnOrderByPeer(rq *Request) any {
peers := make(map[string]peer) peers := make(map[string]peer)
sort.Sort(ListResponse(rq.Responses)) for i := range rq.Responses {
rs := rq.Responses[i]
for _, rs := range rq.Responses {
p, ok := peers[rs.Peer.Owner] p, ok := peers[rs.Peer.Owner]
if !ok { if !ok {
@ -593,7 +606,13 @@ func fnOrderByPeer(rq *Request) any {
} }
peerList := make(listPeer, 0, len(peers)) 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) peerList = append(peerList, v)
} }

View File

@ -45,7 +45,8 @@
<div class=row> <div class=row>
<h2>Results</h2> <h2>Results</h2>
{{ with $args := . }} {{ with $args := . }}
{{range $req := .Requests}} {{ range $req := .Requests }}
{{ if ne $req.RequestID "" }}
<div class="panel panel-primary"> <div class="panel panel-primary">
<div class="panel-heading"> <div class="panel-heading">
<a href="/peers/req/{{ $req.RequestID }}"> <a href="/peers/req/{{ $req.RequestID }}">
@ -58,6 +59,7 @@
</div> </div>
{{end}} {{end}}
{{end}} {{end}}
{{end}}
</div> </div>
{{end}} {{end}}

View File

@ -9,44 +9,42 @@
{{range .Requests}} {{range .Requests}}
<h2>Results to {{.RequestIP}}{{if .Hidden}} 👁️{{end}}</h2> <h2>Results to {{.RequestIP}}{{if .Hidden}} 👁️{{end}}</h2>
{{with (orderByPeer .)}} {{range orderByPeer .}}
{{range .}} <div class="panel panel-primary" id="peer-{{.Nick}}">
<div class="panel panel-primary" id="peer-{{.Nick}}"> <div class="panel-heading">
<div class="panel-heading"> <b> {{.Country}} :: {{.Name}} :: {{.Nick}} </b>
<b> {{.Country}} :: {{.Name}} :: {{.Nick}} </b> <div style='float:right'>
<div style='float:right'> <a class='btn btn-success' href="#peer-{{.Nick}}">{{ if eq .Latency 0.0 }}&mdash;{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</a>
<a class='btn btn-success' href="#peer-{{.Nick}}">{{ if eq .Latency 0.0 }}&mdash;{{ 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 }}&mdash;{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</td>
<td>{{ if eq .Jitter 0.0 }}&mdash;{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}</td>
</tr>
{{end}}
</tbody>
</table>
</div> </div>
</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 }}&mdash;{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</td>
<td>{{ if eq .Jitter 0.0 }}&mdash;{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
{{end}} {{end}}
{{end}} {{end}}
{{end}} {{end}}