mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Sort snowflake-ips stats by country count.
We currently don't sort the snowflake-ips metrics: snowflake-ips CA=1,DE=1,AR=1,NL=1,FR=1,GB=2,US=4,CH=1 To facilitate eyeballing our metrics, this patch sorts snowflake-ips by value. If the value is identical, we sort by string, i.e.: snowflake-ips US=4,GB=2,AR=1,CA=1,CH=1,DE=1,FR=1,NL=1 This patch fixes tpo/anti-censorship/pluggable-transports/snowflake#40011
This commit is contained in:
parent
665d76c5b0
commit
5efcde5187
2 changed files with 39 additions and 1 deletions
|
@ -10,6 +10,7 @@ import (
|
|||
"log"
|
||||
"math"
|
||||
"net"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
@ -51,10 +52,32 @@ type Metrics struct {
|
|||
lock sync.Mutex
|
||||
}
|
||||
|
||||
type record struct {
|
||||
cc string
|
||||
count int
|
||||
}
|
||||
type records []record
|
||||
|
||||
func (r records) Len() int { return len(r) }
|
||||
func (r records) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
||||
func (r records) Less(i, j int) bool {
|
||||
if r[i].count == r[j].count {
|
||||
return r[i].cc > r[j].cc
|
||||
}
|
||||
return r[i].count < r[j].count
|
||||
}
|
||||
|
||||
func (s CountryStats) Display() string {
|
||||
output := ""
|
||||
|
||||
// Use the records struct to sort our counts map by value.
|
||||
rs := records{}
|
||||
for cc, count := range s.counts {
|
||||
output += fmt.Sprintf("%s=%d,", cc, count)
|
||||
rs = append(rs, record{cc: cc, count: count})
|
||||
}
|
||||
sort.Sort(sort.Reverse(rs))
|
||||
for _, r := range rs {
|
||||
output += fmt.Sprintf("%s=%d,", r.cc, r.count)
|
||||
}
|
||||
|
||||
// cut off trailing ","
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue