Implement quick and dirty measurement listing

This commit is contained in:
Arturo Filastò 2018-06-22 12:12:35 +02:00
parent 04eb07624c
commit eb4e6988b3
3 changed files with 106 additions and 50 deletions

View File

@ -1,6 +1,8 @@
package list
import (
"fmt"
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/cli/root"
@ -11,64 +13,77 @@ import (
func init() {
cmd := root.Command("list", "List results")
resultID := cmd.Arg("id", "the id of the result to list measurements for").Int64()
cmd.Action(func(_ *kingpin.ParseContext) error {
ctx, err := root.Init()
if err != nil {
log.WithError(err).Error("failed to initialize root context")
return err
}
doneResults, incompleteResults, err := database.ListResults(ctx.DB)
if err != nil {
log.WithError(err).Error("failed to list results")
return err
}
if *resultID > 0 {
measurements, err := database.ListMeasurements(ctx.DB, *resultID)
if err != nil {
log.WithError(err).Error("failed to list measurements")
return err
}
for idx, msmt := range measurements {
fmt.Printf("%d: %v\n", idx, msmt)
}
} else {
doneResults, incompleteResults, err := database.ListResults(ctx.DB)
if err != nil {
log.WithError(err).Error("failed to list results")
return err
}
if len(incompleteResults) > 0 {
output.SectionTitle("Incomplete results")
}
for idx, result := range incompleteResults {
output.ResultItem(output.ResultItemData{
ID: result.ID,
Index: idx,
TotalCount: len(incompleteResults),
Name: result.Name,
StartTime: result.StartTime,
NetworkName: result.NetworkName,
Country: result.Country,
ASN: result.ASN,
Summary: result.Summary,
Done: result.Done,
DataUsageUp: result.DataUsageUp,
DataUsageDown: result.DataUsageDown,
})
}
if len(incompleteResults) > 0 {
output.SectionTitle("Incomplete results")
}
for idx, result := range incompleteResults {
output.ResultItem(output.ResultItemData{
ID: result.ID,
Index: idx,
TotalCount: len(incompleteResults),
Name: result.Name,
StartTime: result.StartTime,
NetworkName: result.NetworkName,
Country: result.Country,
ASN: result.ASN,
Summary: result.Summary,
Done: result.Done,
DataUsageUp: result.DataUsageUp,
DataUsageDown: result.DataUsageDown,
})
}
resultSummary := output.ResultSummaryData{}
netCount := make(map[string]int)
output.SectionTitle("Results")
for idx, result := range doneResults {
output.ResultItem(output.ResultItemData{
ID: result.ID,
Index: idx,
TotalCount: len(doneResults),
Name: result.Name,
StartTime: result.StartTime,
NetworkName: result.NetworkName,
Country: result.Country,
ASN: result.ASN,
Summary: result.Summary,
Done: result.Done,
DataUsageUp: result.DataUsageUp,
DataUsageDown: result.DataUsageDown,
})
resultSummary.TotalTests++
netCount[result.ASN]++
resultSummary.TotalDataUsageUp += result.DataUsageUp
resultSummary.TotalDataUsageDown += result.DataUsageDown
}
resultSummary.TotalNetworks = int64(len(netCount))
resultSummary := output.ResultSummaryData{}
netCount := make(map[string]int)
output.SectionTitle("Results")
for idx, result := range doneResults {
output.ResultItem(output.ResultItemData{
ID: result.ID,
Index: idx,
TotalCount: len(doneResults),
Name: result.Name,
StartTime: result.StartTime,
NetworkName: result.NetworkName,
Country: result.Country,
ASN: result.ASN,
Summary: result.Summary,
Done: result.Done,
DataUsageUp: result.DataUsageUp,
DataUsageDown: result.DataUsageDown,
})
resultSummary.TotalTests++
netCount[result.ASN]++
resultSummary.TotalDataUsageUp += result.DataUsageUp
resultSummary.TotalDataUsageDown += result.DataUsageDown
}
resultSummary.TotalNetworks = int64(len(netCount))
output.ResultSummary(resultSummary)
output.ResultSummary(resultSummary)
}
return nil
})

View File

@ -10,7 +10,12 @@ func init() {
cmd := root.Command("show", "Show a specific measurement")
cmd.Action(func(_ *kingpin.ParseContext) error {
log.Info("Show")
_, err := root.Init()
if err != nil {
log.WithError(err).Error("failed to initialize root context")
return err
}
return nil
})
}

View File

@ -34,6 +34,42 @@ func UpdateOne(db *sqlx.DB, query string, arg interface{}) error {
return nil
}
// ListMeasurements given a result ID
func ListMeasurements(db *sqlx.DB, resultID int64) ([]*Measurement, error) {
measurements := []*Measurement{}
rows, err := db.Query(`SELECT id, name,
start_time, runtime,
country,
asn,
summary,
input
FROM measurements
WHERE result_id = ?
ORDER BY start_time;`, resultID)
if err != nil {
return measurements, errors.Wrap(err, "failed to get measurement list")
}
for rows.Next() {
msmt := Measurement{}
err = rows.Scan(&msmt.ID, &msmt.Name,
&msmt.StartTime, &msmt.Runtime,
&msmt.CountryCode,
&msmt.ASN,
&msmt.Summary, &msmt.Input,
//&result.DataUsageUp, &result.DataUsageDown)
)
if err != nil {
log.WithError(err).Error("failed to fetch a row")
continue
}
measurements = append(measurements, &msmt)
}
return measurements, nil
}
// Measurement model
type Measurement struct {
ID int64 `db:"id"`