aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Winter <phw@nymity.ch>2020-11-27 11:04:00 -0800
committerPhilipp Winter <phw@nymity.ch>2020-11-27 11:20:40 -0800
commit5efcde518796e319231fd68c816e1ab74dd66129 (patch)
tree29303721fcf1b3f775d285566ea7d869cbee1b53
parent665d76c5b04c4e470d85a826ea617a2404ed4a1d (diff)
downloadsnowflake-5efcde518796e319231fd68c816e1ab74dd66129.tar.gz
snowflake-5efcde518796e319231fd68c816e1ab74dd66129.zip
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
-rw-r--r--broker/metrics.go25
-rw-r--r--broker/snowflake-broker_test.go15
2 files changed, 39 insertions, 1 deletions
diff --git a/broker/metrics.go b/broker/metrics.go
index d1beae2..c3ffa92 100644
--- a/broker/metrics.go
+++ b/broker/metrics.go
@@ -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 ","
diff --git a/broker/snowflake-broker_test.go b/broker/snowflake-broker_test.go
index 4e8e5f0..7b87313 100644
--- a/broker/snowflake-broker_test.go
+++ b/broker/snowflake-broker_test.go
@@ -679,5 +679,20 @@ func TestMetrics(t *testing.T) {
ctx.metrics.printMetrics()
So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0")
})
+ Convey("for country stats order", func() {
+
+ stats := map[string]int{
+ "IT": 50,
+ "FR": 200,
+ "TZ": 100,
+ "CN": 250,
+ "RU": 150,
+ "CA": 1,
+ "BE": 1,
+ "PH": 1,
+ }
+ ctx.metrics.countryStats.counts = stats
+ So(ctx.metrics.countryStats.Display(), ShouldEqual, "CN=250,FR=200,RU=150,TZ=100,IT=50,BE=1,CA=1,PH=1")
+ })
})
}