@@ -11,6 +11,7 @@ import (
1111)
1212
1313type GetPeersRequest struct {
14+ SortBy string `json:"sort"`
1415}
1516
1617type GetPeersResponse struct {
@@ -36,7 +37,7 @@ type PeerEntry struct {
3637 LastError string `json:"last_error,omitempty"`
3738}
3839
39- func (a * AdminSocket ) getPeersHandler (_ * GetPeersRequest , res * GetPeersResponse ) error {
40+ func (a * AdminSocket ) getPeersHandler (req * GetPeersRequest , res * GetPeersResponse ) error {
4041 peers := a .core .GetPeers ()
4142 res .Peers = make ([]PeerEntry , 0 , len (peers ))
4243 for _ , p := range peers {
@@ -66,26 +67,67 @@ func (a *AdminSocket) getPeersHandler(_ *GetPeersRequest, res *GetPeersResponse)
6667 }
6768 res .Peers = append (res .Peers , peer )
6869 }
69- slices .SortStableFunc (res .Peers , func (a , b PeerEntry ) int {
70- if ! a .Inbound && b .Inbound {
71- return - 1
72- }
73- if a .Inbound && ! b .Inbound {
74- return 1
75- }
76- if d := strings .Compare (a .PublicKey , b .PublicKey ); d != 0 {
77- return d
78- }
79- if d := a .Priority - b .Priority ; d != 0 {
80- return int (d )
81- }
82- if d := a .Cost - b .Cost ; d != 0 {
83- return int (d )
84- }
85- if d := a .Uptime - b .Uptime ; d != 0 {
86- return int (d )
87- }
88- return 0
89- })
70+ switch strings .ToLower (req .SortBy ) {
71+ case "uptime" :
72+ slices .SortStableFunc (res .Peers , sortByUptime )
73+ case "cost" :
74+ slices .SortStableFunc (res .Peers , sortByCost )
75+ default :
76+ slices .SortStableFunc (res .Peers , sortByDefault )
77+ }
9078 return nil
9179}
80+
81+ func sortByDefault (a , b PeerEntry ) int {
82+ if ! a .Inbound && b .Inbound {
83+ return - 1
84+ }
85+ if a .Inbound && ! b .Inbound {
86+ return 1
87+ }
88+ if d := strings .Compare (a .PublicKey , b .PublicKey ); d != 0 {
89+ return d
90+ }
91+ if d := a .Priority - b .Priority ; d != 0 {
92+ return int (d )
93+ }
94+ if d := a .Cost - b .Cost ; d != 0 {
95+ return int (d )
96+ }
97+ if d := a .Uptime - b .Uptime ; d != 0 {
98+ return int (d )
99+ }
100+ return 0
101+ }
102+
103+ func sortByCost (a , b PeerEntry ) int {
104+ if d := a .Cost - b .Cost ; d != 0 {
105+ return int (d )
106+ }
107+ if d := strings .Compare (a .PublicKey , b .PublicKey ); d != 0 {
108+ return d
109+ }
110+ if d := a .Priority - b .Priority ; d != 0 {
111+ return int (d )
112+ }
113+ if d := a .Uptime - b .Uptime ; d != 0 {
114+ return int (d )
115+ }
116+ return 0
117+ }
118+
119+ func sortByUptime (a , b PeerEntry ) int {
120+ if d := a .Uptime - b .Uptime ; d != 0 {
121+ return int (d )
122+ }
123+ if d := strings .Compare (a .PublicKey , b .PublicKey ); d != 0 {
124+ return d
125+ }
126+ if d := a .Priority - b .Priority ; d != 0 {
127+ return int (d )
128+ }
129+ if d := a .Cost - b .Cost ; d != 0 {
130+ return int (d )
131+ }
132+ return 0
133+ }
0 commit comments