From 2081c25b7344865ecfb2976cc695d95c67342693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Tue, 11 Sep 2018 18:41:15 +0200 Subject: [PATCH] Expose the median_bitrate in the list view --- internal/database/actions.go | 21 ++++++++++++++++----- internal/database/actions_test.go | 20 ++++++++++++++++++++ internal/database/models.go | 8 ++++++++ internal/log/handlers/cli/results.go | 15 ++++----------- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/internal/database/actions.go b/internal/database/actions.go index ba36145..a0b3f89 100644 --- a/internal/database/actions.go +++ b/internal/database/actions.go @@ -53,17 +53,28 @@ func GetResultTestKeys(sess sqlbuilder.Database, resultID int64) (string, error) res := sess.Collection("measurements").Find("result_id", resultID) defer res.Close() - var msmt Measurement + var ( + msmt Measurement + tk PerformanceTestKeys + ) for res.Next(&msmt) { if msmt.TestName == "web_connectivity" { break } - // We only really care about the NDT TestKeys - if msmt.TestName == "ndt" { - return msmt.TestKeys, nil + // We only really care about performance keys + if msmt.TestName == "ndt" || msmt.TestName == "dash" { + if err := json.Unmarshal([]byte(msmt.TestKeys), &tk); err != nil { + log.WithError(err).Error("failed to parse testKeys") + return "{}", err + } } } - return "{}", nil + b, err := json.Marshal(tk) + if err != nil { + log.WithError(err).Error("failed to serialize testKeys") + return "{}", err + } + return string(b), nil } // GetMeasurementCounts returns the number of anomalous and total measurement for a given result diff --git a/internal/database/actions_test.go b/internal/database/actions_test.go index a060b40..504f175 100644 --- a/internal/database/actions_test.go +++ b/internal/database/actions_test.go @@ -2,6 +2,7 @@ package database import ( "database/sql" + "encoding/json" "io/ioutil" "os" "testing" @@ -145,3 +146,22 @@ func TestURLCreation(t *testing.T) { t.Error("inserting the same URL with different category code should produce the same result") } } + +func TestPerformanceTestKeys(t *testing.T) { + var tk PerformanceTestKeys + + ndtS := "{\"download\":100.0,\"upload\":20.0,\"ping\":2.2}" + dashS := "{\"median_bitrate\":102.0}" + if err := json.Unmarshal([]byte(ndtS), &tk); err != nil { + t.Fatal("failed to parse ndtS") + } + if err := json.Unmarshal([]byte(dashS), &tk); err != nil { + t.Fatal("failed to parse dashS") + } + if tk.Bitrate != 102.0 { + t.Fatalf("error Bitrate %f", tk.Bitrate) + } + if tk.Download != 100.0 { + t.Fatalf("error Download %f", tk.Download) + } +} diff --git a/internal/database/models.go b/internal/database/models.go index fbbb982..dfb9e54 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -96,6 +96,14 @@ type Result struct { MeasurementDir string `db:"measurement_dir"` } +// PerformanceTestKeys is the result summary for a performance test +type PerformanceTestKeys struct { + Upload float64 `json:"upload"` + Download float64 `json:"download"` + Ping float64 `json:"ping"` + Bitrate float64 `json:"median_bitrate"` +} + // Finished marks the result as done and sets the runtime func (r *Result) Finished(sess sqlbuilder.Database) error { if r.IsDone == true || r.Runtime != 0 { diff --git a/internal/log/handlers/cli/results.go b/internal/log/handlers/cli/results.go index 9c3462f..8d9a2a1 100644 --- a/internal/log/handlers/cli/results.go +++ b/internal/log/handlers/cli/results.go @@ -8,12 +8,13 @@ import ( "time" "github.com/apex/log" + "github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/util" ) -func formatSpeed(speed int64) string { +func formatSpeed(speed float64) string { if speed < 1000 { - return fmt.Sprintf("%d Kbit/s", speed) + return fmt.Sprintf("%.2f Kbit/s", speed) } else if speed < 1000*1000 { return fmt.Sprintf("%.2f Mbit/s", float32(speed)/1000) } else if speed < 1000*1000*1000 { @@ -23,14 +24,6 @@ func formatSpeed(speed int64) string { return fmt.Sprintf("%.2f Tbit/s", float32(speed)/(1000*1000*1000)) } -// PerformanceTestKeys is the result summary for a performance test -type PerformanceTestKeys struct { - Upload int64 `json:"upload"` - Download int64 `json:"download"` - Ping float64 `json:"ping"` - Bitrate int64 `json:"median_bitrate"` -} - var summarizers = map[string]func(uint64, uint64, string) []string{ "websites": func(totalCount uint64, anomalyCount uint64, ss string) []string { return []string{ @@ -40,7 +33,7 @@ var summarizers = map[string]func(uint64, uint64, string) []string{ } }, "performance": func(totalCount uint64, anomalyCount uint64, ss string) []string { - var tk PerformanceTestKeys + var tk database.PerformanceTestKeys if err := json.Unmarshal([]byte(ss), &tk); err != nil { return nil }