Add support for generating the Measurement summary

To generate the result summary we need to refactor how the SummaryMap
works
This commit is contained in:
Arturo Filastò 2018-03-23 13:17:39 +01:00
parent 00859e87a6
commit f5ff3ac87a
3 changed files with 56 additions and 4 deletions

View File

@ -129,13 +129,19 @@ func (m *Measurement) WriteSummary(db *sqlx.DB, summary string) error {
// AddToResult adds a measurement to a result // AddToResult adds a measurement to a result
func (m *Measurement) AddToResult(db *sqlx.DB, result *Result) error { func (m *Measurement) AddToResult(db *sqlx.DB, result *Result) error {
var err error
m.ResultID = result.ID m.ResultID = result.ID
finalPath := filepath.Join(result.MeasurementDir, finalPath := filepath.Join(result.MeasurementDir,
filepath.Base(m.ReportFilePath)) filepath.Base(m.ReportFilePath))
err := os.Rename(m.ReportFilePath, finalPath) // If the finalPath already exists, it means it has already been moved there.
if err != nil { // This happens in multi input reports
return errors.Wrap(err, "moving report file") if _, err = os.Stat(finalPath); os.IsNotExist(err) {
err = os.Rename(m.ReportFilePath, finalPath)
if err != nil {
return errors.Wrap(err, "moving report file")
}
} }
m.ReportFilePath = finalPath m.ReportFilePath = finalPath

View File

@ -37,6 +37,12 @@ type IMSummary struct {
Detected bool Detected bool
} }
// WebsitesSummary is the summary for the websites test
type WebsitesSummary struct {
Tested uint
Blocked uint
}
// NettestGroups that can be run by the user // NettestGroups that can be run by the user
var NettestGroups = map[string]NettestGroup{ var NettestGroups = map[string]NettestGroup{
"websites": NettestGroup{ "websites": NettestGroup{
@ -45,6 +51,7 @@ var NettestGroups = map[string]NettestGroup{
websites.WebConnectivity{}, websites.WebConnectivity{},
}, },
Summary: func(m database.SummaryMap) (string, error) { Summary: func(m database.SummaryMap) (string, error) {
// XXX to generate this I need to create the summary map as a list
return "{}", nil return "{}", nil
}, },
}, },

View File

@ -11,11 +11,13 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// URLInfo contains the URL and the citizenlab category code for that URL
type URLInfo struct { type URLInfo struct {
URL string `json:"url"` URL string `json:"url"`
CategoryCode string `json:"category_code"` CategoryCode string `json:"category_code"`
} }
// URLResponse is the orchestrate url response containing a list of URLs
type URLResponse struct { type URLResponse struct {
Results []URLInfo `json:"results"` Results []URLInfo `json:"results"`
} }
@ -69,9 +71,46 @@ func (n WebConnectivity) Run(ctl *nettests.Controller) error {
return nt.Run() return nt.Run()
} }
// WebConnectivitySummary for the test
type WebConnectivitySummary struct {
Accessible bool
Blocking string
Blocked bool
}
// Summary generates a summary for a test run // Summary generates a summary for a test run
func (n WebConnectivity) Summary(tk map[string]interface{}) interface{} { func (n WebConnectivity) Summary(tk map[string]interface{}) interface{} {
return nil var (
blocked bool
blocking string
accessible bool
)
// We need to do these complicated type assertions, because some of the fields
// are "nullable" and/or can be of different types
switch v := tk["blocking"].(type) {
case bool:
blocked = false
blocking = "none"
case string:
blocked = true
blocking = v
default:
blocked = false
blocking = "none"
}
if tk["accessible"] == nil {
accessible = false
} else {
accessible = tk["accessible"].(bool)
}
return WebConnectivitySummary{
Accessible: accessible,
Blocking: blocking,
Blocked: blocked,
}
} }
// LogSummary writes the summary to the standard output // LogSummary writes the summary to the standard output