From 24316728b9bb6d09c4bb630965a8262701a914fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Tue, 7 Jan 2020 16:13:13 +0200 Subject: [PATCH 1/3] Improve the output and extraction of psiphon metrics --- internal/log/handlers/cli/results.go | 4 ++-- nettests/psiphon.go | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/internal/log/handlers/cli/results.go b/internal/log/handlers/cli/results.go index bc52806..dfef5c7 100644 --- a/internal/log/handlers/cli/results.go +++ b/internal/log/handlers/cli/results.go @@ -71,8 +71,8 @@ var summarizers = map[string]func(uint64, uint64, string) []string{ }, "circumvention": func(totalCount uint64, anomalyCount uint64, ss string) []string { return []string{ - fmt.Sprintf("Detected: %v", anomalyCount > 0), - "", + fmt.Sprintf("%d tested", totalCount), + fmt.Sprintf("%d blocked", anomalyCount), "", } }, diff --git a/nettests/psiphon.go b/nettests/psiphon.go index bdcf35a..8459831 100644 --- a/nettests/psiphon.go +++ b/nettests/psiphon.go @@ -1,5 +1,7 @@ package nettests +import "github.com/pkg/errors" + // Psiphon test implementation type Psiphon struct { } @@ -17,14 +19,27 @@ func (h Psiphon) Run(ctl *Controller) error { // PsiphonTestKeys contains the test keys type PsiphonTestKeys struct { - IsAnomaly bool `json:"-"` + IsAnomaly bool `json:"-"` + BootstrapTime float64 `json:"bootstrap_time"` + Failure string `json:"failure"` } // GetTestKeys generates a summary for a test run func (h Psiphon) GetTestKeys(tk map[string]interface{}) (interface{}, error) { - return PsiphonTestKeys{ - IsAnomaly: tk["failure"] != nil, - }, nil + var err error + testKeys := PsiphonTestKeys{IsAnomaly: false, Failure: ""} + if tk["failure"] != nil { + testKeys.IsAnomaly = true + testKeys.Failure, ok = tk["failure"].(string) + if !ok { + err = errors.Wrap(err, "failure key invalid") + } + } + testKeys.BootstrapTime, ok = tk["bootstrap_time"].(float64) + if !ok { + err = errors.Wrap(err, "bootstrap_time key invalid") + } + return testKeys, err } // LogSummary writes the summary to the standard output From 2cb66613408a15d046d49a3a0031793e2c925344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Tue, 7 Jan 2020 16:42:04 +0200 Subject: [PATCH 2/3] Fix typo --- nettests/psiphon.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nettests/psiphon.go b/nettests/psiphon.go index 8459831..228a7ba 100644 --- a/nettests/psiphon.go +++ b/nettests/psiphon.go @@ -30,12 +30,12 @@ func (h Psiphon) GetTestKeys(tk map[string]interface{}) (interface{}, error) { testKeys := PsiphonTestKeys{IsAnomaly: false, Failure: ""} if tk["failure"] != nil { testKeys.IsAnomaly = true - testKeys.Failure, ok = tk["failure"].(string) + testKeys.Failure, ok := tk["failure"].(string) if !ok { err = errors.Wrap(err, "failure key invalid") } } - testKeys.BootstrapTime, ok = tk["bootstrap_time"].(float64) + testKeys.BootstrapTime, ok := tk["bootstrap_time"].(float64) if !ok { err = errors.Wrap(err, "bootstrap_time key invalid") } From 427ca18985b85a3337f51b5b44b7ec15c31d85dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Tue, 7 Jan 2020 16:44:11 +0200 Subject: [PATCH 3/3] Refactor pattern of ok !ok --- nettests/psiphon.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nettests/psiphon.go b/nettests/psiphon.go index 228a7ba..fef9fe9 100644 --- a/nettests/psiphon.go +++ b/nettests/psiphon.go @@ -26,16 +26,19 @@ type PsiphonTestKeys struct { // GetTestKeys generates a summary for a test run func (h Psiphon) GetTestKeys(tk map[string]interface{}) (interface{}, error) { - var err error + var ( + err error + ok bool + ) testKeys := PsiphonTestKeys{IsAnomaly: false, Failure: ""} if tk["failure"] != nil { testKeys.IsAnomaly = true - testKeys.Failure, ok := tk["failure"].(string) + testKeys.Failure, ok = tk["failure"].(string) if !ok { err = errors.Wrap(err, "failure key invalid") } } - testKeys.BootstrapTime, ok := tk["bootstrap_time"].(float64) + testKeys.BootstrapTime, ok = tk["bootstrap_time"].(float64) if !ok { err = errors.Wrap(err, "bootstrap_time key invalid") }