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
+4 -9
View File
@@ -7,16 +7,11 @@ import (
"github.com/apex/log"
"github.com/jmoiron/sqlx"
"github.com/ooni/probe-cli/nettests/summary"
"github.com/ooni/probe-cli/utils"
"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
func UpdateOne(db *sqlx.DB, query string, arg interface{}) error {
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
// result
func MakeSummaryMap(db *sqlx.DB, r *Result) (SummaryMap, error) {
summaryMap := SummaryMap{}
func MakeSummaryMap(db *sqlx.DB, r *Result) (summary.SummaryMap, error) {
summaryMap := summary.SummaryMap{}
msmts := []Measurement{}
// 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
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 {
return errors.New("Result is already finished")
}
+5 -30
View File
@@ -9,34 +9,9 @@ import (
"github.com/apex/log"
"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 {
if speed < 1000 {
return fmt.Sprintf("%d Kbit/s", speed)
@@ -51,7 +26,7 @@ func formatSpeed(speed int64) string {
var summarizers = map[string]func(string) []string{
"websites": func(ss string) []string {
var summary WebsitesSummary
var summary summary.WebsitesSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil
}
@@ -62,7 +37,7 @@ var summarizers = map[string]func(string) []string{
}
},
"performance": func(ss string) []string {
var summary PerformanceSummary
var summary summary.PerformanceSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil
}
@@ -73,7 +48,7 @@ var summarizers = map[string]func(string) []string{
}
},
"im": func(ss string) []string {
var summary IMSummary
var summary summary.IMSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil
}
@@ -84,7 +59,7 @@ var summarizers = map[string]func(string) []string{
}
},
"middlebox": func(ss string) []string {
var summary MiddleboxSummary
var summary summary.MiddleboxSummary
if err := json.Unmarshal([]byte(ss), &summary); err != nil {
return nil
}