Implement the show command (#53)
This commit is contained in:
committed by
Simone Basso
parent
b9b555ba68
commit
f425d3f007
+31
-17
@@ -3,8 +3,6 @@ package run
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
@@ -50,20 +48,20 @@ func init() {
|
||||
cmd := root.Command("run", "Run a test group or OONI Run link")
|
||||
|
||||
var nettestGroupNamesBlue []string
|
||||
var ctx *ooni.Context
|
||||
var network *database.Network
|
||||
|
||||
for name := range groups.NettestGroups {
|
||||
nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name))
|
||||
}
|
||||
|
||||
nettestGroup := cmd.Arg("name",
|
||||
fmt.Sprintf("the nettest group to run. Supported tests are: %s, or nothing to run them all",
|
||||
strings.Join(nettestGroupNamesBlue, ", "))).String()
|
||||
|
||||
noCollector := cmd.Flag("no-collector", "Disable uploading measurements to a collector").Bool()
|
||||
collectorURL := cmd.Flag("collector-url", "Specify the address of a custom collector").String()
|
||||
bouncerURL := cmd.Flag("bouncer-url", "Specify the address of a custom bouncer").String()
|
||||
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
ctx, err := root.Init()
|
||||
var err error
|
||||
ctx, err = root.Init()
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
return err
|
||||
@@ -91,7 +89,7 @@ func init() {
|
||||
log.WithError(err).Error("Failed to lookup the location of the probe")
|
||||
return err
|
||||
}
|
||||
network, err := database.CreateNetwork(ctx.DB, ctx.Session.Location)
|
||||
network, err = database.CreateNetwork(ctx.DB, ctx.Session.Location)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create the network row")
|
||||
return err
|
||||
@@ -112,17 +110,33 @@ func init() {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if *nettestGroup == "" {
|
||||
log.Infof("Running %s tests", color.BlueString("all"))
|
||||
for tg := range groups.NettestGroups {
|
||||
if err := runNettestGroup(tg, ctx, network); err != nil {
|
||||
log.WithError(err).Errorf("failed to run %s", tg)
|
||||
}
|
||||
websitesCmd := cmd.Command("websites", "")
|
||||
websitesCmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
return runNettestGroup("websites", ctx, network)
|
||||
})
|
||||
imCmd := cmd.Command("im", "")
|
||||
imCmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
return runNettestGroup("im", ctx, network)
|
||||
})
|
||||
performanceCmd := cmd.Command("performance", "")
|
||||
performanceCmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
return runNettestGroup("performance", ctx, network)
|
||||
})
|
||||
middleboxCmd := cmd.Command("middlebox", "")
|
||||
middleboxCmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
return runNettestGroup("middlebox", ctx, network)
|
||||
})
|
||||
allCmd := cmd.Command("all", "").Default()
|
||||
allCmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
log.Infof("Running %s tests", color.BlueString("all"))
|
||||
for tg := range groups.NettestGroups {
|
||||
if err := runNettestGroup(tg, ctx, network); err != nil {
|
||||
log.WithError(err).Errorf("failed to run %s", tg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
log.Infof("Running %s", color.BlueString(*nettestGroup))
|
||||
return runNettestGroup(*nettestGroup, ctx, network)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,19 +4,27 @@ 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("show", "Show a specific measurement")
|
||||
|
||||
msmtID := cmd.Arg("id", "the id of the measurement to show").Int64()
|
||||
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
_, err := root.Init()
|
||||
ctx, err := root.Init()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to initialize root context")
|
||||
return err
|
||||
}
|
||||
log.Error("this function is not implemented")
|
||||
|
||||
msmt, err := database.GetMeasurementJSON(ctx.DB, *msmtID)
|
||||
if err != nil {
|
||||
log.Errorf("error: %v", err)
|
||||
return err
|
||||
}
|
||||
output.MeasurementJSON(msmt)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package database
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"bufio"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"time"
|
||||
@@ -10,6 +13,7 @@ import (
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-engine/model"
|
||||
"github.com/ooni/probe-cli/utils"
|
||||
"github.com/ooni/probe-cli/internal/util"
|
||||
"github.com/pkg/errors"
|
||||
db "upper.io/db.v3"
|
||||
"upper.io/db.v3/lib/sqlbuilder"
|
||||
@@ -38,7 +42,67 @@ func ListMeasurements(sess sqlbuilder.Database, resultID int64) ([]MeasurementUR
|
||||
return measurements, nil
|
||||
}
|
||||
|
||||
// GetResultTestKeys returns a list of TestKeys for a given measurements
|
||||
// GetMeasurementJSON will a map[string]interface{} given a database and a measurementID
|
||||
func GetMeasurementJSON(sess sqlbuilder.Database, measurementID int64) (map[string]interface{}, error) {
|
||||
var (
|
||||
measurement MeasurementURLNetwork
|
||||
msmtJSON map[string]interface{}
|
||||
)
|
||||
|
||||
req := sess.Select(
|
||||
db.Raw("urls.*"),
|
||||
db.Raw("measurements.*"),
|
||||
).From("measurements").
|
||||
LeftJoin("urls").On("urls.url_id = measurements.url_id").
|
||||
Where("measurements.measurement_id= ?", measurementID)
|
||||
|
||||
if err := req.One(&measurement); err != nil {
|
||||
log.Errorf("failed to run query %s: %v", req.String(), err)
|
||||
return nil, err
|
||||
}
|
||||
reportFilePath := measurement.Measurement.ReportFilePath
|
||||
// If the url->url is NULL then we are dealing with a single entry
|
||||
// measurement and all we have to do is read the file and return it.
|
||||
if (measurement.URL.URL.Valid == false) {
|
||||
b, err := ioutil.ReadFile(reportFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal(b, &msmtJSON); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return msmtJSON, nil
|
||||
}
|
||||
|
||||
// When the URL is a string then we need to seek until we reach the
|
||||
// measurement line in the file that matches the target input
|
||||
url := measurement.URL.URL.String
|
||||
file, err := os.Open(reportFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
reader := bufio.NewReader(file)
|
||||
|
||||
for {
|
||||
line, err := util.ReadLine(reader)
|
||||
if (err == io.EOF) {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &msmtJSON); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if (msmtJSON["input"].(string) == url) {
|
||||
return msmtJSON, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("Could not find measurement")
|
||||
}
|
||||
|
||||
// GetResultTestKeys returns a list of TestKeys for a given result
|
||||
func GetResultTestKeys(sess sqlbuilder.Database, resultID int64) (string, error) {
|
||||
res := sess.Collection("measurements").Find("result_id", resultID)
|
||||
defer res.Close()
|
||||
@@ -48,20 +112,18 @@ func GetResultTestKeys(sess sqlbuilder.Database, resultID int64) (string, error)
|
||||
tk PerformanceTestKeys
|
||||
)
|
||||
for res.Next(&msmt) {
|
||||
if msmt.TestName == "web_connectivity" {
|
||||
break
|
||||
}
|
||||
// We only really care about performance keys.
|
||||
// Note: since even in case of failure we still initialise an empty struct,
|
||||
// it could be that these keys come out as initializes with the default
|
||||
// values.
|
||||
// XXX we may want to change this behaviour by adding `omitempty` to the
|
||||
// struct definition.
|
||||
if msmt.TestName == "ndt" || msmt.TestName == "dash" {
|
||||
if err := json.Unmarshal([]byte(msmt.TestKeys), &tk); err != nil {
|
||||
log.WithError(err).Error("failed to parse testKeys")
|
||||
return "{}", err
|
||||
}
|
||||
if msmt.TestName != "ndt" && msmt.TestName != "dash" {
|
||||
return "{}", nil
|
||||
}
|
||||
if err := json.Unmarshal([]byte(msmt.TestKeys), &tk); err != nil {
|
||||
log.WithError(err).Error("failed to parse testKeys")
|
||||
return "{}", err
|
||||
}
|
||||
}
|
||||
b, err := json.Marshal(tk)
|
||||
|
||||
@@ -119,6 +119,8 @@ func (h *Handler) TypedLog(t string, e *log.Entry) error {
|
||||
return logTable(h.Writer, e.Fields)
|
||||
case "measurement_item":
|
||||
return logMeasurementItem(h.Writer, e.Fields)
|
||||
case "measurement_json":
|
||||
return logMeasurementJSON(h.Writer, e.Fields)
|
||||
case "measurement_summary":
|
||||
return logMeasurementSummary(h.Writer, e.Fields)
|
||||
case "result_item":
|
||||
|
||||
@@ -129,3 +129,14 @@ func logMeasurementSummary(w io.Writer, f log.Fields) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func logMeasurementJSON(w io.Writer, f log.Fields) error {
|
||||
m := f.Get("measurement_json").(map[string]interface{})
|
||||
|
||||
json, err := json.MarshalIndent(m, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(w, string(json))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,6 +11,14 @@ import (
|
||||
"github.com/ooni/probe-cli/internal/util"
|
||||
)
|
||||
|
||||
// MeasurementJSON prints the JSON of a measurement
|
||||
func MeasurementJSON(j map[string]interface{}) {
|
||||
log.WithFields(log.Fields{
|
||||
"type": "measurement_json",
|
||||
"measurement_json": j,
|
||||
}).Info("Measurement JSON")
|
||||
}
|
||||
|
||||
// Progress logs a progress type event
|
||||
func Progress(key string, perc float64, msg string) {
|
||||
log.WithFields(log.Fields{
|
||||
|
||||
@@ -2,6 +2,7 @@ package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
@@ -112,3 +113,18 @@ func WrapString(s string, lim uint) string {
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
|
||||
// ReadLine will read a single line from a bufio.Reader
|
||||
func ReadLine(r *bufio.Reader) (string, error) {
|
||||
var (
|
||||
isPrefix bool
|
||||
err error
|
||||
line, ln []byte
|
||||
)
|
||||
for isPrefix && err == nil {
|
||||
line, isPrefix, err = r.ReadLine()
|
||||
ln = append(ln, line...)
|
||||
}
|
||||
return string(ln), err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user