ooni-probe-cli/internal/cli/list/list.go

123 lines
4.0 KiB
Go
Raw Normal View History

package list
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/output"
)
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
}
if *resultID > 0 {
measurements, err := database.ListMeasurements(ctx.DB, *resultID)
if err != nil {
log.WithError(err).Error("failed to list measurements")
return err
}
msmtSummary := output.MeasurementSummaryData{
TotalCount: 0,
AnomalyCount: 0,
2018-09-12 13:42:16 +02:00
DataUsageUp: 0.0,
DataUsageDown: 0.0,
TotalRuntime: 0,
}
2018-09-10 18:03:32 +02:00
for _, msmt := range measurements {
if msmtSummary.TotalRuntime == 0 {
msmtSummary.TotalRuntime = msmt.ResultRuntime
}
// FIXME this logic should be adjusted for test groups that have many
// measurements in them
if msmtSummary.DataUsageUp == 0 {
msmtSummary.DataUsageUp = msmt.DataUsageUp
msmtSummary.DataUsageDown = msmt.DataUsageDown
}
if msmt.IsAnomaly.Bool == true {
msmtSummary.AnomalyCount++
}
msmtSummary.TotalCount++
2018-09-10 18:03:32 +02:00
output.MeasurementItem(msmt)
}
output.MeasurementSummary(msmtSummary)
} 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{
2018-09-10 16:56:32 +02:00
ID: result.ResultID,
2018-09-10 12:41:28 +02:00
Index: idx,
TotalCount: len(incompleteResults),
Name: result.TestGroupName,
StartTime: result.StartTime,
NetworkName: result.Network.NetworkName,
Country: result.Network.CountryCode,
ASN: result.Network.ASN,
2018-09-10 12:41:28 +02:00
MeasurementCount: 0,
MeasurementAnomalyCount: 0,
TestKeys: "{}", // FIXME this used to be Summary we probably need to use a list now
Done: result.IsDone,
DataUsageUp: result.DataUsageUp,
DataUsageDown: result.DataUsageDown,
})
}
resultSummary := output.ResultSummaryData{}
2018-09-07 15:39:22 +02:00
netCount := make(map[uint]int)
output.SectionTitle("Results")
for idx, result := range doneResults {
2018-09-10 16:56:32 +02:00
totalCount, anmlyCount, err := database.GetMeasurementCounts(ctx.DB, result.ResultID)
2018-09-10 12:41:28 +02:00
if err != nil {
log.WithError(err).Error("failed to list measurement counts")
}
2018-09-10 16:56:32 +02:00
testKeys, err := database.GetResultTestKeys(ctx.DB, result.ResultID)
2018-09-10 15:03:52 +02:00
if err != nil {
log.WithError(err).Error("failed to get testKeys")
}
output.ResultItem(output.ResultItemData{
2018-09-10 16:56:32 +02:00
ID: result.ResultID,
2018-09-10 12:41:28 +02:00
Index: idx,
TotalCount: len(doneResults),
Name: result.TestGroupName,
StartTime: result.StartTime,
NetworkName: result.Network.NetworkName,
Country: result.Network.CountryCode,
ASN: result.Network.ASN,
2018-09-10 15:03:52 +02:00
TestKeys: testKeys,
2018-09-10 12:41:28 +02:00
MeasurementCount: totalCount,
MeasurementAnomalyCount: anmlyCount,
Done: result.IsDone,
DataUsageUp: result.DataUsageUp,
DataUsageDown: result.DataUsageDown,
})
resultSummary.TotalTests++
2018-09-07 15:39:22 +02:00
netCount[result.Network.ASN]++
resultSummary.TotalDataUsageUp += result.DataUsageUp
resultSummary.TotalDataUsageDown += result.DataUsageDown
}
resultSummary.TotalNetworks = int64(len(netCount))
output.ResultSummary(resultSummary)
}
return nil
})
}