Implement quick and dirty measurement listing
This commit is contained in:
parent
04eb07624c
commit
eb4e6988b3
|
@ -1,6 +1,8 @@
|
||||||
package list
|
package list
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/alecthomas/kingpin"
|
"github.com/alecthomas/kingpin"
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/ooni/probe-cli/internal/cli/root"
|
"github.com/ooni/probe-cli/internal/cli/root"
|
||||||
|
@ -11,64 +13,77 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
cmd := root.Command("list", "List results")
|
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 {
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||||
ctx, err := root.Init()
|
ctx, err := root.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error("failed to initialize root context")
|
log.WithError(err).Error("failed to initialize root context")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
doneResults, incompleteResults, err := database.ListResults(ctx.DB)
|
if *resultID > 0 {
|
||||||
if err != nil {
|
measurements, err := database.ListMeasurements(ctx.DB, *resultID)
|
||||||
log.WithError(err).Error("failed to list results")
|
if err != nil {
|
||||||
return err
|
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 {
|
if len(incompleteResults) > 0 {
|
||||||
output.SectionTitle("Incomplete results")
|
output.SectionTitle("Incomplete results")
|
||||||
}
|
}
|
||||||
for idx, result := range incompleteResults {
|
for idx, result := range incompleteResults {
|
||||||
output.ResultItem(output.ResultItemData{
|
output.ResultItem(output.ResultItemData{
|
||||||
ID: result.ID,
|
ID: result.ID,
|
||||||
Index: idx,
|
Index: idx,
|
||||||
TotalCount: len(incompleteResults),
|
TotalCount: len(incompleteResults),
|
||||||
Name: result.Name,
|
Name: result.Name,
|
||||||
StartTime: result.StartTime,
|
StartTime: result.StartTime,
|
||||||
NetworkName: result.NetworkName,
|
NetworkName: result.NetworkName,
|
||||||
Country: result.Country,
|
Country: result.Country,
|
||||||
ASN: result.ASN,
|
ASN: result.ASN,
|
||||||
Summary: result.Summary,
|
Summary: result.Summary,
|
||||||
Done: result.Done,
|
Done: result.Done,
|
||||||
DataUsageUp: result.DataUsageUp,
|
DataUsageUp: result.DataUsageUp,
|
||||||
DataUsageDown: result.DataUsageDown,
|
DataUsageDown: result.DataUsageDown,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
resultSummary := output.ResultSummaryData{}
|
resultSummary := output.ResultSummaryData{}
|
||||||
netCount := make(map[string]int)
|
netCount := make(map[string]int)
|
||||||
output.SectionTitle("Results")
|
output.SectionTitle("Results")
|
||||||
for idx, result := range doneResults {
|
for idx, result := range doneResults {
|
||||||
output.ResultItem(output.ResultItemData{
|
output.ResultItem(output.ResultItemData{
|
||||||
ID: result.ID,
|
ID: result.ID,
|
||||||
Index: idx,
|
Index: idx,
|
||||||
TotalCount: len(doneResults),
|
TotalCount: len(doneResults),
|
||||||
Name: result.Name,
|
Name: result.Name,
|
||||||
StartTime: result.StartTime,
|
StartTime: result.StartTime,
|
||||||
NetworkName: result.NetworkName,
|
NetworkName: result.NetworkName,
|
||||||
Country: result.Country,
|
Country: result.Country,
|
||||||
ASN: result.ASN,
|
ASN: result.ASN,
|
||||||
Summary: result.Summary,
|
Summary: result.Summary,
|
||||||
Done: result.Done,
|
Done: result.Done,
|
||||||
DataUsageUp: result.DataUsageUp,
|
DataUsageUp: result.DataUsageUp,
|
||||||
DataUsageDown: result.DataUsageDown,
|
DataUsageDown: result.DataUsageDown,
|
||||||
})
|
})
|
||||||
resultSummary.TotalTests++
|
resultSummary.TotalTests++
|
||||||
netCount[result.ASN]++
|
netCount[result.ASN]++
|
||||||
resultSummary.TotalDataUsageUp += result.DataUsageUp
|
resultSummary.TotalDataUsageUp += result.DataUsageUp
|
||||||
resultSummary.TotalDataUsageDown += result.DataUsageDown
|
resultSummary.TotalDataUsageDown += result.DataUsageDown
|
||||||
}
|
}
|
||||||
resultSummary.TotalNetworks = int64(len(netCount))
|
resultSummary.TotalNetworks = int64(len(netCount))
|
||||||
|
|
||||||
output.ResultSummary(resultSummary)
|
output.ResultSummary(resultSummary)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
@ -10,7 +10,12 @@ func init() {
|
||||||
cmd := root.Command("show", "Show a specific measurement")
|
cmd := root.Command("show", "Show a specific measurement")
|
||||||
|
|
||||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
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
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,42 @@ func UpdateOne(db *sqlx.DB, query string, arg interface{}) error {
|
||||||
return nil
|
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
|
// Measurement model
|
||||||
type Measurement struct {
|
type Measurement struct {
|
||||||
ID int64 `db:"id"`
|
ID int64 `db:"id"`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user