2018-02-07 19:02:18 +01:00
|
|
|
package list
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alecthomas/kingpin"
|
2018-02-21 16:06:30 +01:00
|
|
|
"github.com/apex/log"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/cli/root"
|
2018-05-03 18:40:52 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/database"
|
|
|
|
"github.com/ooni/probe-cli/internal/output"
|
2018-02-07 19:02:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-05-03 18:40:52 +02:00
|
|
|
cmd := root.Command("list", "List results")
|
2018-06-22 12:12:35 +02:00
|
|
|
resultID := cmd.Arg("id", "the id of the result to list measurements for").Int64()
|
2018-02-07 19:02:18 +01:00
|
|
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-11-13 20:07:30 +01:00
|
|
|
probeCLI, err := root.Init()
|
2018-05-03 18:40:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to initialize root context")
|
|
|
|
return err
|
|
|
|
}
|
2018-06-22 12:12:35 +02:00
|
|
|
if *resultID > 0 {
|
2020-11-13 20:07:30 +01:00
|
|
|
measurements, err := database.ListMeasurements(probeCLI.DB(), *resultID)
|
2018-06-22 12:12:35 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to list measurements")
|
|
|
|
return err
|
|
|
|
}
|
2018-09-11 15:40:42 +02:00
|
|
|
msmtSummary := output.MeasurementSummaryData{
|
2018-09-13 14:54:56 +02:00
|
|
|
TotalCount: 0,
|
|
|
|
AnomalyCount: 0,
|
|
|
|
DataUsageUp: 0.0,
|
|
|
|
DataUsageDown: 0.0,
|
|
|
|
TotalRuntime: 0,
|
|
|
|
ASN: 0,
|
|
|
|
NetworkName: "",
|
|
|
|
NetworkCountryCode: "ZZ",
|
2018-09-11 15:40:42 +02:00
|
|
|
}
|
2018-09-13 14:54:56 +02:00
|
|
|
isFirst := true
|
|
|
|
isLast := false
|
|
|
|
for idx, msmt := range measurements {
|
|
|
|
if idx > 0 {
|
|
|
|
isFirst = false
|
|
|
|
}
|
|
|
|
if idx == len(measurements)-1 {
|
|
|
|
isLast = true
|
|
|
|
}
|
2018-09-12 15:41:54 +02:00
|
|
|
// We assume that since these are summary level information the first
|
|
|
|
// item will contain the information necessary.
|
2018-09-13 14:54:56 +02:00
|
|
|
if isFirst {
|
2018-09-17 17:30:38 +02:00
|
|
|
msmtSummary.TotalRuntime = msmt.Result.Runtime
|
2018-09-11 15:40:42 +02:00
|
|
|
msmtSummary.DataUsageUp = msmt.DataUsageUp
|
|
|
|
msmtSummary.DataUsageDown = msmt.DataUsageDown
|
2018-09-13 14:54:56 +02:00
|
|
|
msmtSummary.NetworkName = msmt.NetworkName
|
2018-09-17 17:30:38 +02:00
|
|
|
msmtSummary.NetworkCountryCode = msmt.Network.CountryCode
|
2018-09-13 14:54:56 +02:00
|
|
|
msmtSummary.ASN = msmt.ASN
|
2018-09-17 17:30:38 +02:00
|
|
|
msmtSummary.StartTime = msmt.Measurement.StartTime
|
2018-09-11 15:40:42 +02:00
|
|
|
}
|
|
|
|
if msmt.IsAnomaly.Bool == true {
|
|
|
|
msmtSummary.AnomalyCount++
|
|
|
|
}
|
|
|
|
msmtSummary.TotalCount++
|
2018-09-13 14:54:56 +02:00
|
|
|
output.MeasurementItem(msmt, isFirst, isLast)
|
2018-06-22 12:12:35 +02:00
|
|
|
}
|
2018-09-11 15:40:42 +02:00
|
|
|
output.MeasurementSummary(msmtSummary)
|
2018-06-22 12:12:35 +02:00
|
|
|
} else {
|
2020-11-13 20:07:30 +01:00
|
|
|
doneResults, incompleteResults, err := database.ListResults(probeCLI.DB())
|
2018-06-22 12:12:35 +02:00
|
|
|
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-17 17:30:38 +02:00
|
|
|
ID: result.Result.ID,
|
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,
|
2018-09-11 18:06:15 +02:00
|
|
|
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,
|
2018-06-22 12:12:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
resultSummary := output.ResultSummaryData{}
|
2018-09-07 15:39:22 +02:00
|
|
|
netCount := make(map[uint]int)
|
2018-06-22 12:12:35 +02:00
|
|
|
output.SectionTitle("Results")
|
|
|
|
for idx, result := range doneResults {
|
2020-11-13 20:07:30 +01:00
|
|
|
totalCount, anmlyCount, err := database.GetMeasurementCounts(probeCLI.DB(), result.Result.ID)
|
2018-09-10 12:41:28 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to list measurement counts")
|
|
|
|
}
|
2020-11-13 20:07:30 +01:00
|
|
|
testKeys, err := database.GetResultTestKeys(probeCLI.DB(), result.Result.ID)
|
2018-09-10 15:03:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to get testKeys")
|
|
|
|
}
|
2018-06-22 12:12:35 +02:00
|
|
|
output.ResultItem(output.ResultItemData{
|
2018-09-17 17:30:38 +02:00
|
|
|
ID: result.Result.ID,
|
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,
|
2018-09-11 18:06:15 +02:00
|
|
|
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,
|
2019-10-03 11:18:07 +02:00
|
|
|
Done: result.IsDone,
|
|
|
|
DataUsageUp: result.DataUsageUp,
|
|
|
|
DataUsageDown: result.DataUsageDown,
|
2018-06-22 12:12:35 +02:00
|
|
|
})
|
|
|
|
resultSummary.TotalTests++
|
2018-09-07 15:39:22 +02:00
|
|
|
netCount[result.Network.ASN]++
|
2018-06-22 12:12:35 +02:00
|
|
|
resultSummary.TotalDataUsageUp += result.DataUsageUp
|
|
|
|
resultSummary.TotalDataUsageDown += result.DataUsageDown
|
|
|
|
}
|
|
|
|
resultSummary.TotalNetworks = int64(len(netCount))
|
|
|
|
output.ResultSummary(resultSummary)
|
|
|
|
}
|
2018-02-07 19:02:18 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|