diff --git a/internal/cli/list/list.go b/internal/cli/list/list.go index d62d293..b52882b 100644 --- a/internal/cli/list/list.go +++ b/internal/cli/list/list.go @@ -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 }) diff --git a/internal/cli/show/show.go b/internal/cli/show/show.go index de919e3..ff79442 100644 --- a/internal/cli/show/show.go +++ b/internal/cli/show/show.go @@ -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 }) } diff --git a/internal/database/models.go b/internal/database/models.go index daff59b..3bc2539 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -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"`