From 98e95b8b7e51630e62587d4210b425aa1de97bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 23 Mar 2018 14:58:33 +0100 Subject: [PATCH] Add support for generating the web connectivity summary --- internal/database/models.go | 9 +++++++-- nettests/groups/groups.go | 30 +++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/internal/database/models.go b/internal/database/models.go index bd704a4..be101c3 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -15,7 +15,7 @@ import ( type ResultSummaryFunc func(SummaryMap) (string, error) // SummaryMap contains a mapping from test name to serialized summary for it -type SummaryMap map[string]string +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 { @@ -210,7 +210,12 @@ func MakeSummaryMap(db *sqlx.DB, r *Result) (SummaryMap, error) { return nil, errors.Wrap(err, "failed to get measurements") } for _, msmt := range msmts { - summaryMap[msmt.Name] = msmt.Summary + val, ok := summaryMap[msmt.Name] + if ok { + summaryMap[msmt.Name] = append(val, msmt.Summary) + } else { + summaryMap[msmt.Name] = []string{msmt.Summary} + } } return summaryMap, nil } diff --git a/nettests/groups/groups.go b/nettests/groups/groups.go index 6a82f74..5ab291a 100644 --- a/nettests/groups/groups.go +++ b/nettests/groups/groups.go @@ -52,7 +52,27 @@ var NettestGroups = map[string]NettestGroup{ }, Summary: func(m database.SummaryMap) (string, error) { // XXX to generate this I need to create the summary map as a list - return "{}", nil + var summary WebsitesSummary + summary.Tested = 0 + summary.Blocked = 0 + for _, msmtSummaryStr := range m["WebConnectivity"] { + var wcSummary websites.WebConnectivitySummary + + err := json.Unmarshal([]byte(msmtSummaryStr), &wcSummary) + if err != nil { + log.WithError(err).Error("failed to unmarshal WebConnectivity summary") + return "", err + } + if wcSummary.Blocked { + summary.Blocked++ + } + summary.Tested++ + } + summaryBytes, err := json.Marshal(summary) + if err != nil { + return "", err + } + return string(summaryBytes), nil }, }, "performance": NettestGroup{ @@ -68,12 +88,12 @@ var NettestGroups = map[string]NettestGroup{ dashSummary performance.DashSummary summary PerformanceSummary ) - err = json.Unmarshal([]byte(m["Dash"]), &dashSummary) + err = json.Unmarshal([]byte(m["Dash"][0]), &dashSummary) if err != nil { log.WithError(err).Error("failed to unmarshal Dash summary") return "", err } - err = json.Unmarshal([]byte(m["Ndt"]), &ndtSummary) + err = json.Unmarshal([]byte(m["Ndt"][0]), &ndtSummary) if err != nil { log.WithError(err).Error("failed to unmarshal NDT summary") return "", err @@ -102,12 +122,12 @@ var NettestGroups = map[string]NettestGroup{ hirlSummary middlebox.HTTPInvalidRequestLineSummary summary MiddleboxSummary ) - err = json.Unmarshal([]byte(m["HttpHeaderFieldManipulation"]), &hhfmSummary) + err = json.Unmarshal([]byte(m["HttpHeaderFieldManipulation"][0]), &hhfmSummary) if err != nil { log.WithError(err).Error("failed to unmarshal hhfm summary") return "", err } - err = json.Unmarshal([]byte(m["HttpInvalidRequestLine"]), &hirlSummary) + err = json.Unmarshal([]byte(m["HttpInvalidRequestLine"][0]), &hirlSummary) if err != nil { log.WithError(err).Error("failed to unmarshal hirl summary") return "", err