fix(peers): fixing sort on results for real
This commit is contained in:
		
							parent
							
								
									8ba2846e62
								
							
						
					
					
						commit
						d27eb21ffb
					
				@ -4,7 +4,6 @@ import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math"
 | 
			
		||||
	"net/netip"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
@ -107,7 +106,10 @@ func (lis ListResponse) Len() int {
 | 
			
		||||
	return len(lis)
 | 
			
		||||
}
 | 
			
		||||
func (lis ListResponse) Less(i, j int) bool {
 | 
			
		||||
	if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 {
 | 
			
		||||
	if lis[j].Latency == 0.0 && lis[i].Latency > 0.0 {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	if lis[i].Latency == 0.0 && lis[j].Latency > 0.0 {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,6 @@ import (
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
	"log"
 | 
			
		||||
	"math"
 | 
			
		||||
	"net"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"sort"
 | 
			
		||||
@ -91,21 +90,54 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
			return
 | 
			
		||||
 | 
			
		||||
		case strings.HasPrefix(r.URL.Path, "/peers/status"):
 | 
			
		||||
			var pickID string
 | 
			
		||||
			if strings.HasPrefix(r.URL.Path, "/peers/status/") {
 | 
			
		||||
				pickID = strings.TrimPrefix(r.URL.Path, "/peers/status/")
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			var requests []*Request
 | 
			
		||||
 | 
			
		||||
			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, p.Name)
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				for id, rq := range state.requests {
 | 
			
		||||
					fmt.Fprintln(w, "REQ: ", id, rq.RequestIP, len(rq.Responses))
 | 
			
		||||
					for id, r := range rq.Responses {
 | 
			
		||||
						fmt.Fprintln(w, "  RES: ", id, r.PeerID[24:], r.Latency, r.Jitter)
 | 
			
		||||
				if pickID != "" {
 | 
			
		||||
					if rq, ok := state.requests[pickID]; ok {
 | 
			
		||||
						requests = append(requests, rq)
 | 
			
		||||
					}
 | 
			
		||||
				} else {
 | 
			
		||||
					requests = make([]*Request, 0, len(state.requests))
 | 
			
		||||
					for i := range state.requests {
 | 
			
		||||
						rq := state.requests[i]
 | 
			
		||||
						requests = append(requests, rq)
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				for i := range requests {
 | 
			
		||||
					rq := requests[i]
 | 
			
		||||
					for i := range rq.Responses {
 | 
			
		||||
						res := rq.Responses[i]
 | 
			
		||||
						if peer, ok := state.peers[res.PeerID]; ok {
 | 
			
		||||
							res.Peer = peer
 | 
			
		||||
							res.Peer.ID = ""
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				return nil
 | 
			
		||||
			})
 | 
			
		||||
 | 
			
		||||
			for i, rq := range requests {
 | 
			
		||||
				fmt.Fprintln(w, "REQ: ", i, rq.RequestIP, len(rq.Responses))
 | 
			
		||||
				for i, peer := range fnOrderByPeer(rq) {
 | 
			
		||||
					fmt.Fprintln(w, "  PEER: ", i, peer.RequestID, peer.Name, peer.Latency, peer.Jitter)
 | 
			
		||||
					for i, res := range peer.Results {
 | 
			
		||||
						fmt.Fprintln(w, "    RES: ", i, res.RequestID, res.Latency, res.Jitter)
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		default:
 | 
			
		||||
			s.getResults(w, r)
 | 
			
		||||
			return
 | 
			
		||||
@ -531,12 +563,14 @@ var funcMap = map[string]any{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type peerResult struct {
 | 
			
		||||
	RequestID string
 | 
			
		||||
	Name      string
 | 
			
		||||
	Country   string
 | 
			
		||||
	Latency   float64
 | 
			
		||||
	Jitter    float64
 | 
			
		||||
}
 | 
			
		||||
type peer struct {
 | 
			
		||||
	RequestID string
 | 
			
		||||
	Name      string
 | 
			
		||||
	Note      string
 | 
			
		||||
	Nick      string
 | 
			
		||||
@ -553,10 +587,14 @@ func (lis listPeer) Len() int {
 | 
			
		||||
	return len(lis)
 | 
			
		||||
}
 | 
			
		||||
func (lis listPeer) Less(i, j int) bool {
 | 
			
		||||
	if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 {
 | 
			
		||||
	if lis[j].Latency == 0.0 && lis[i].Latency > 0.0 {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	if lis[i].Latency == 0.0 && lis[j].Latency > 0.0 {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	return lis[j].Latency >= lis[i].Latency
 | 
			
		||||
 | 
			
		||||
	return lis[i].Latency < lis[j].Latency
 | 
			
		||||
}
 | 
			
		||||
func (lis listPeer) Swap(i, j int) {
 | 
			
		||||
	lis[i], lis[j] = lis[j], lis[i]
 | 
			
		||||
@ -568,24 +606,31 @@ 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 {
 | 
			
		||||
	if lis[j].Latency == 0.0 && lis[i].Latency > 0.0 {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	if lis[i].Latency == 0.0 && lis[j].Latency > 0.0 {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	return lis[j].Latency >= lis[i].Latency
 | 
			
		||||
 | 
			
		||||
	return lis[i].Latency < lis[j].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) listPeer {
 | 
			
		||||
	peers := make(map[string]peer)
 | 
			
		||||
 | 
			
		||||
	for i := range rq.Responses {
 | 
			
		||||
		if rq.Responses[i] == nil || rq.Responses[i].Peer == nil {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		rs := rq.Responses[i]
 | 
			
		||||
 | 
			
		||||
		p, ok := peers[rs.Peer.Owner]
 | 
			
		||||
 | 
			
		||||
		if !ok {
 | 
			
		||||
			p.RequestID = rq.RequestID
 | 
			
		||||
			p.Country = rs.Peer.Country
 | 
			
		||||
			p.Name = rs.Peer.Name
 | 
			
		||||
			p.Nick = rs.Peer.Nick
 | 
			
		||||
@ -596,6 +641,7 @@ func fnOrderByPeer(rq *Request) any {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		p.Results = append(p.Results, peerResult{
 | 
			
		||||
			RequestID: rq.RequestID,
 | 
			
		||||
			Name:      rs.Peer.Name,
 | 
			
		||||
			Country:   rs.Peer.Country,
 | 
			
		||||
			Latency:   rs.Latency,
 | 
			
		||||
@ -617,7 +663,6 @@ func fnOrderByPeer(rq *Request) any {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	sort.Sort(peerList)
 | 
			
		||||
 | 
			
		||||
	return peerList
 | 
			
		||||
}
 | 
			
		||||
func fnCountResponses(rq *Request) int {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@ -29,7 +29,6 @@ require github.com/patrickmn/go-cache v2.1.0+incompatible
 | 
			
		||||
require (
 | 
			
		||||
	github.com/julienschmidt/httprouter v1.3.0 // indirect
 | 
			
		||||
	github.com/taigrr/go-colorhash v0.0.0-20220329080504-742db7f45eae // indirect
 | 
			
		||||
	golang.org/dl v0.0.0-20230112164944-626922ede476 // indirect
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										5
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								go.sum
									
									
									
									
									
								
							@ -69,7 +69,6 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM
 | 
			
		||||
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
 | 
			
		||||
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
 | 
			
		||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 | 
			
		||||
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
 | 
			
		||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
 | 
			
		||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
 | 
			
		||||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
 | 
			
		||||
@ -424,8 +423,6 @@ gitlab.com/jamietanna/content-negotiation-go v0.2.0 h1:vT0OLEPQ6DYRG3/1F7joXSNjV
 | 
			
		||||
gitlab.com/jamietanna/content-negotiation-go v0.2.0/go.mod h1:n4ZZ8/X5TstnjYRnjEtR/fC7MCTe+aRKM7PQlLBH3PQ=
 | 
			
		||||
go.mills.io/salty v0.0.0-20220322161301-ce2b9f6573fa h1:KBxzYJMWP7MXd72RgqsMCGOSEqV6aaDDSdSb8usJCzQ=
 | 
			
		||||
go.mills.io/salty v0.0.0-20220322161301-ce2b9f6573fa/go.mod h1:bQ9yvK7wwThD4tzoioJq/YAuwYOB2XA9tAUHIYtjre8=
 | 
			
		||||
go.mills.io/salty v0.0.0-20230121154700-b5dab41cf9e7 h1:T6kxzIrbniu7CzRF3tB2sfx5etCWlgktwh/9gryZ1rc=
 | 
			
		||||
go.mills.io/salty v0.0.0-20230121154700-b5dab41cf9e7/go.mod h1:bQ9yvK7wwThD4tzoioJq/YAuwYOB2XA9tAUHIYtjre8=
 | 
			
		||||
go.mills.io/saltyim v0.0.0-20230128070719-15a64de82829 h1:rzgfYKbCt8N0vVD3CAMoPwtvj4Zr1l3Cyl3rjN4+kHg=
 | 
			
		||||
go.mills.io/saltyim v0.0.0-20230128070719-15a64de82829/go.mod h1:ldLxf9b9mfq3QMHXenH42tvkUGJ0UlSQ/QUoTKvefs8=
 | 
			
		||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
 | 
			
		||||
@ -470,8 +467,6 @@ go.yarn.social/lextwt v0.0.0-20221221200320-31bca76a2587/go.mod h1:XcveSVLWxkBBW
 | 
			
		||||
go.yarn.social/types v0.0.0-20221025190911-9524f5b4a743/go.mod h1:XN+G4HprNn/Gp7OF2zveqsCRSWFCHtOaIRh2GlcK+U4=
 | 
			
		||||
go.yarn.social/types v0.0.0-20221027173319-2d00e96a95c1 h1:H3W7HmWrVpHs7WcncxifE7lr9JUApKPGqZTWmIaU5F4=
 | 
			
		||||
go.yarn.social/types v0.0.0-20221027173319-2d00e96a95c1/go.mod h1:+xnDkQ0T0S8emxWIsvxlCAoyF8gBaj0q81hr/VrKc0c=
 | 
			
		||||
golang.org/dl v0.0.0-20230112164944-626922ede476 h1:Fajk6nhvJWnFeoRvOR9a6XyjMgpBIsxgwySoz7uJDU4=
 | 
			
		||||
golang.org/dl v0.0.0-20230112164944-626922ede476/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user