Move summary related code into it's own package

This commit is contained in:
Arturo Filastò 2018-06-28 13:51:43 +02:00
parent 533ea77aa2
commit cf588c8466
4 changed files with 63 additions and 85 deletions

View File

@ -7,16 +7,11 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/ooni/probe-cli/nettests/summary"
"github.com/ooni/probe-cli/utils" "github.com/ooni/probe-cli/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// ResultSummaryFunc is the function used to generate result summaries
type ResultSummaryFunc func(SummaryMap) (string, error)
// SummaryMap contains a mapping from test name to serialized summary for it
type SummaryMap map[string][]string
// UpdateOne will run the specified update query and check that it only affected one row // UpdateOne will run the specified update query and check that it only affected one row
func UpdateOne(db *sqlx.DB, query string, arg interface{}) error { func UpdateOne(db *sqlx.DB, query string, arg interface{}) error {
res, err := db.NamedExec(query, arg) res, err := db.NamedExec(query, arg)
@ -298,8 +293,8 @@ func ListResults(db *sqlx.DB) ([]*Result, []*Result, error) {
// MakeSummaryMap return a mapping of test names to summaries for the given // MakeSummaryMap return a mapping of test names to summaries for the given
// result // result
func MakeSummaryMap(db *sqlx.DB, r *Result) (SummaryMap, error) { func MakeSummaryMap(db *sqlx.DB, r *Result) (summary.SummaryMap, error) {
summaryMap := SummaryMap{} summaryMap := summary.SummaryMap{}
msmts := []Measurement{} msmts := []Measurement{}
// XXX maybe we only want to select some of the columns // XXX maybe we only want to select some of the columns
@ -319,7 +314,7 @@ func MakeSummaryMap(db *sqlx.DB, r *Result) (SummaryMap, error) {
} }
// Finished marks the result as done and sets the runtime // Finished marks the result as done and sets the runtime
func (r *Result) Finished(db *sqlx.DB, makeSummary ResultSummaryFunc) error { func (r *Result) Finished(db *sqlx.DB, makeSummary summary.ResultSummaryFunc) error {
if r.Done == true || r.Runtime != 0 { if r.Done == true || r.Runtime != 0 {
return errors.New("Result is already finished") return errors.New("Result is already finished")
} }

View File

@ -9,34 +9,9 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/internal/util"
"github.com/ooni/probe-cli/nettests/summary"
) )
// XXX Copy-pasta from nettest/groups
// PerformanceSummary is the result summary for a performance test
type PerformanceSummary struct {
Upload int64
Download int64
Ping float64
Bitrate int64
}
// MiddleboxSummary is the summary for the middlebox tests
type MiddleboxSummary struct {
Detected bool
}
// IMSummary is the summary for the im tests
type IMSummary struct {
Tested uint
Blocked uint
}
// WebsitesSummary is the summary for the websites test
type WebsitesSummary struct {
Tested uint
Blocked uint
}
func formatSpeed(speed int64) string { func formatSpeed(speed int64) string {
if speed < 1000 { if speed < 1000 {
return fmt.Sprintf("%d Kbit/s", speed) return fmt.Sprintf("%d Kbit/s", speed)
@ -51,7 +26,7 @@ func formatSpeed(speed int64) string {
var summarizers = map[string]func(string) []string{ var summarizers = map[string]func(string) []string{
"websites": func(ss string) []string { "websites": func(ss string) []string {
var summary WebsitesSummary var summary summary.WebsitesSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil { if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil return nil
} }
@ -62,7 +37,7 @@ var summarizers = map[string]func(string) []string{
} }
}, },
"performance": func(ss string) []string { "performance": func(ss string) []string {
var summary PerformanceSummary var summary summary.PerformanceSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil { if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil return nil
} }
@ -73,7 +48,7 @@ var summarizers = map[string]func(string) []string{
} }
}, },
"im": func(ss string) []string { "im": func(ss string) []string {
var summary IMSummary var summary summary.IMSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil { if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil return nil
} }
@ -84,7 +59,7 @@ var summarizers = map[string]func(string) []string{
} }
}, },
"middlebox": func(ss string) []string { "middlebox": func(ss string) []string {
var summary MiddleboxSummary var summary summary.MiddleboxSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil { if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil return nil
} }

View File

@ -2,14 +2,13 @@ package groups
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/nettests" "github.com/ooni/probe-cli/nettests"
"github.com/ooni/probe-cli/nettests/im" "github.com/ooni/probe-cli/nettests/im"
"github.com/ooni/probe-cli/nettests/middlebox" "github.com/ooni/probe-cli/nettests/middlebox"
"github.com/ooni/probe-cli/nettests/performance" "github.com/ooni/probe-cli/nettests/performance"
"github.com/ooni/probe-cli/nettests/summary"
"github.com/ooni/probe-cli/nettests/websites" "github.com/ooni/probe-cli/nettests/websites"
) )
@ -17,42 +16,7 @@ import (
type NettestGroup struct { type NettestGroup struct {
Label string Label string
Nettests []nettests.Nettest Nettests []nettests.Nettest
Summary database.ResultSummaryFunc Summary summary.ResultSummaryFunc
}
// PerformanceSummary is the result summary for a performance test
type PerformanceSummary struct {
Upload int64
Download int64
Ping float64
Bitrate int64
}
// MiddleboxSummary is the summary for the middlebox tests
type MiddleboxSummary struct {
Detected bool
}
// IMSummary is the summary for the im tests
type IMSummary struct {
Tested uint
Blocked uint
}
// WebsitesSummary is the summary for the websites test
type WebsitesSummary struct {
Tested uint
Blocked uint
}
func checkRequiredKeys(rk []string, m database.SummaryMap) error {
for _, key := range rk {
if _, ok := m[key]; ok {
continue
}
return fmt.Errorf("missing SummaryMap key '%s'", key)
}
return nil
} }
// NettestGroups that can be run by the user // NettestGroups that can be run by the user
@ -62,14 +26,14 @@ var NettestGroups = map[string]NettestGroup{
Nettests: []nettests.Nettest{ Nettests: []nettests.Nettest{
websites.WebConnectivity{}, websites.WebConnectivity{},
}, },
Summary: func(m database.SummaryMap) (string, error) { Summary: func(m summary.SummaryMap) (string, error) {
if err := checkRequiredKeys([]string{"WebConnectivity"}, m); err != nil { if err := checkRequiredKeys([]string{"WebConnectivity"}, m); err != nil {
log.WithError(err).Error("missing keys") log.WithError(err).Error("missing keys")
return "", err return "", err
} }
// XXX to generate this I need to create the summary map as a list // XXX to generate this I need to create the summary map as a list
var summary WebsitesSummary var summary summary.WebsitesSummary
summary.Tested = 0 summary.Tested = 0
summary.Blocked = 0 summary.Blocked = 0
for _, msmtSummaryStr := range m["WebConnectivity"] { for _, msmtSummaryStr := range m["WebConnectivity"] {
@ -98,7 +62,7 @@ var NettestGroups = map[string]NettestGroup{
performance.Dash{}, performance.Dash{},
performance.NDT{}, performance.NDT{},
}, },
Summary: func(m database.SummaryMap) (string, error) { Summary: func(m summary.SummaryMap) (string, error) {
if err := checkRequiredKeys([]string{"Dash", "Ndt"}, m); err != nil { if err := checkRequiredKeys([]string{"Dash", "Ndt"}, m); err != nil {
log.WithError(err).Error("missing keys") log.WithError(err).Error("missing keys")
return "", err return "", err
@ -108,7 +72,7 @@ var NettestGroups = map[string]NettestGroup{
err error err error
ndtSummary performance.NDTSummary ndtSummary performance.NDTSummary
dashSummary performance.DashSummary dashSummary performance.DashSummary
summary PerformanceSummary summary summary.PerformanceSummary
) )
err = json.Unmarshal([]byte(m["Dash"][0]), &dashSummary) err = json.Unmarshal([]byte(m["Dash"][0]), &dashSummary)
if err != nil { if err != nil {
@ -137,7 +101,7 @@ var NettestGroups = map[string]NettestGroup{
middlebox.HTTPInvalidRequestLine{}, middlebox.HTTPInvalidRequestLine{},
middlebox.HTTPHeaderFieldManipulation{}, middlebox.HTTPHeaderFieldManipulation{},
}, },
Summary: func(m database.SummaryMap) (string, error) { Summary: func(m summary.SummaryMap) (string, error) {
if err := checkRequiredKeys([]string{"WebConnectivity"}, m); err != nil { if err := checkRequiredKeys([]string{"WebConnectivity"}, m); err != nil {
log.WithError(err).Error("missing keys") log.WithError(err).Error("missing keys")
return "", err return "", err
@ -147,7 +111,7 @@ var NettestGroups = map[string]NettestGroup{
err error err error
hhfmSummary middlebox.HTTPHeaderFieldManipulationSummary hhfmSummary middlebox.HTTPHeaderFieldManipulationSummary
hirlSummary middlebox.HTTPInvalidRequestLineSummary hirlSummary middlebox.HTTPInvalidRequestLineSummary
summary MiddleboxSummary summary summary.MiddleboxSummary
) )
err = json.Unmarshal([]byte(m["HttpHeaderFieldManipulation"][0]), &hhfmSummary) err = json.Unmarshal([]byte(m["HttpHeaderFieldManipulation"][0]), &hhfmSummary)
if err != nil { if err != nil {
@ -174,7 +138,7 @@ var NettestGroups = map[string]NettestGroup{
im.Telegram{}, im.Telegram{},
im.WhatsApp{}, im.WhatsApp{},
}, },
Summary: func(m database.SummaryMap) (string, error) { Summary: func(m summary.SummaryMap) (string, error) {
if err := checkRequiredKeys([]string{"Whatsapp", "Telegram", "FacebookMessenger"}, m); err != nil { if err := checkRequiredKeys([]string{"Whatsapp", "Telegram", "FacebookMessenger"}, m); err != nil {
log.WithError(err).Error("missing keys") log.WithError(err).Error("missing keys")
return "", err return "", err
@ -184,7 +148,7 @@ var NettestGroups = map[string]NettestGroup{
waSummary im.WhatsAppSummary waSummary im.WhatsAppSummary
tgSummary im.TelegramSummary tgSummary im.TelegramSummary
fbSummary im.FacebookMessengerSummary fbSummary im.FacebookMessengerSummary
summary IMSummary summary summary.IMSummary
) )
err = json.Unmarshal([]byte(m["Whatsapp"][0]), &waSummary) err = json.Unmarshal([]byte(m["Whatsapp"][0]), &waSummary)
if err != nil { if err != nil {

View File

@ -0,0 +1,44 @@
package summary
import "fmt"
// ResultSummaryFunc is the function used to generate result summaries
type ResultSummaryFunc func(SummaryMap) (string, error)
// SummaryMap contains a mapping from test name to serialized summary for it
type SummaryMap map[string][]string
// PerformanceSummary is the result summary for a performance test
type PerformanceSummary struct {
Upload int64
Download int64
Ping float64
Bitrate int64
}
// MiddleboxSummary is the summary for the middlebox tests
type MiddleboxSummary struct {
Detected bool
}
// IMSummary is the summary for the im tests
type IMSummary struct {
Tested uint
Blocked uint
}
// WebsitesSummary is the summary for the websites test
type WebsitesSummary struct {
Tested uint
Blocked uint
}
func checkRequiredKeys(rk []string, m SummaryMap) error {
for _, key := range rk {
if _, ok := m[key]; ok {
continue
}
return fmt.Errorf("missing SummaryMap key '%s'", key)
}
return nil
}