2018-05-03 18:40:52 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
2018-06-25 16:31:44 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/util"
|
2018-05-03 18:40:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func formatSpeed(speed int64) string {
|
|
|
|
if speed < 1000 {
|
|
|
|
return fmt.Sprintf("%d Kbit/s", speed)
|
|
|
|
} else if speed < 1000*1000 {
|
|
|
|
return fmt.Sprintf("%.2f Mbit/s", float32(speed)/1000)
|
|
|
|
} else if speed < 1000*1000*1000 {
|
|
|
|
return fmt.Sprintf("%.2f Gbit/s", float32(speed)/(1000*1000))
|
|
|
|
}
|
|
|
|
// WTF, you crazy?
|
|
|
|
return fmt.Sprintf("%.2f Tbit/s", float32(speed)/(1000*1000*1000))
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:41:28 +02:00
|
|
|
// PerformanceTestKeys is the result summary for a performance test
|
|
|
|
type PerformanceTestKeys struct {
|
|
|
|
Upload int64 `json:"upload"`
|
|
|
|
Download int64 `json:"download"`
|
|
|
|
Ping float64 `json:"ping"`
|
|
|
|
Bitrate int64 `json:"median_bitrate"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var summarizers = map[string]func(uint64, uint64, string) []string{
|
|
|
|
"websites": func(totalCount uint64, anomalyCount uint64, ss string) []string {
|
2018-05-03 18:40:52 +02:00
|
|
|
return []string{
|
2018-09-10 12:41:28 +02:00
|
|
|
fmt.Sprintf("%d tested", totalCount),
|
|
|
|
fmt.Sprintf("%d blocked", anomalyCount),
|
2018-05-03 18:40:52 +02:00
|
|
|
"",
|
|
|
|
}
|
|
|
|
},
|
2018-09-10 12:41:28 +02:00
|
|
|
"performance": func(totalCount uint64, anomalyCount uint64, ss string) []string {
|
|
|
|
var tk PerformanceTestKeys
|
|
|
|
if err := json.Unmarshal([]byte(ss), &tk); err != nil {
|
2018-05-03 18:40:52 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return []string{
|
2018-09-10 12:41:28 +02:00
|
|
|
fmt.Sprintf("Download: %s", formatSpeed(tk.Download)),
|
|
|
|
fmt.Sprintf("Upload: %s", formatSpeed(tk.Upload)),
|
|
|
|
fmt.Sprintf("Ping: %.2fms", tk.Ping),
|
2018-05-03 18:40:52 +02:00
|
|
|
}
|
|
|
|
},
|
2018-09-10 12:41:28 +02:00
|
|
|
"im": func(totalCount uint64, anomalyCount uint64, ss string) []string {
|
2018-05-03 18:40:52 +02:00
|
|
|
return []string{
|
2018-09-10 12:41:28 +02:00
|
|
|
fmt.Sprintf("%d tested", totalCount),
|
|
|
|
fmt.Sprintf("%d blocked", anomalyCount),
|
2018-05-03 18:40:52 +02:00
|
|
|
"",
|
|
|
|
}
|
|
|
|
},
|
2018-09-10 12:41:28 +02:00
|
|
|
"middlebox": func(totalCount uint64, anomalyCount uint64, ss string) []string {
|
2018-05-03 18:40:52 +02:00
|
|
|
return []string{
|
2018-09-10 12:41:28 +02:00
|
|
|
fmt.Sprintf("Detected: %v", anomalyCount > 0),
|
2018-05-03 18:40:52 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:41:28 +02:00
|
|
|
func makeSummary(name string, totalCount uint64, anomalyCount uint64, ss string) []string {
|
|
|
|
return summarizers[name](totalCount, anomalyCount, ss)
|
2018-05-03 18:40:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func logResultItem(w io.Writer, f log.Fields) error {
|
|
|
|
colWidth := 24
|
|
|
|
|
|
|
|
rID := f.Get("id").(int64)
|
|
|
|
name := f.Get("name").(string)
|
|
|
|
startTime := f.Get("start_time").(time.Time)
|
|
|
|
networkName := f.Get("network_name").(string)
|
2018-09-11 18:06:15 +02:00
|
|
|
asn := fmt.Sprintf("AS%d (%s)", f.Get("asn").(uint), f.Get("country").(string))
|
2018-05-03 18:40:52 +02:00
|
|
|
//runtime := f.Get("runtime").(float64)
|
|
|
|
//dataUsageUp := f.Get("dataUsageUp").(int64)
|
|
|
|
//dataUsageDown := f.Get("dataUsageDown").(int64)
|
|
|
|
index := f.Get("index").(int)
|
|
|
|
totalCount := f.Get("total_count").(int)
|
|
|
|
if index == 0 {
|
|
|
|
fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth*2+2)+"┓\n")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "┢"+strings.Repeat("━", colWidth*2+2)+"┪\n")
|
|
|
|
}
|
|
|
|
|
2018-06-25 16:31:44 +02:00
|
|
|
firstRow := util.RightPad(fmt.Sprintf("#%d - %s", rID, startTime.Format(time.RFC822)), colWidth*2)
|
2018-05-03 18:40:52 +02:00
|
|
|
fmt.Fprintf(w, "┃ "+firstRow+" ┃\n")
|
|
|
|
fmt.Fprintf(w, "┡"+strings.Repeat("━", colWidth*2+2)+"┩\n")
|
|
|
|
|
2018-09-10 12:41:28 +02:00
|
|
|
summary := makeSummary(name,
|
|
|
|
f.Get("measurement_count").(uint64),
|
|
|
|
f.Get("measurement_anomaly_count").(uint64),
|
|
|
|
f.Get("test_keys").(string))
|
2018-05-03 18:40:52 +02:00
|
|
|
|
|
|
|
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
|
2018-06-25 16:31:44 +02:00
|
|
|
util.RightPad(name, colWidth),
|
|
|
|
util.RightPad(summary[0], colWidth)))
|
2018-05-03 18:40:52 +02:00
|
|
|
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
|
2018-06-25 16:31:44 +02:00
|
|
|
util.RightPad(networkName, colWidth),
|
|
|
|
util.RightPad(summary[1], colWidth)))
|
2018-05-03 18:40:52 +02:00
|
|
|
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
|
2018-06-25 16:31:44 +02:00
|
|
|
util.RightPad(asn, colWidth),
|
|
|
|
util.RightPad(summary[2], colWidth)))
|
2018-05-03 18:40:52 +02:00
|
|
|
|
|
|
|
if index == totalCount-1 {
|
|
|
|
fmt.Fprintf(w, "└┬──────────────┬──────────────┬──────────────┬")
|
|
|
|
fmt.Fprintf(w, strings.Repeat("─", colWidth*2-44))
|
|
|
|
fmt.Fprintf(w, "┘\n")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-22 11:53:10 +02:00
|
|
|
|
|
|
|
func logResultSummary(w io.Writer, f log.Fields) error {
|
|
|
|
|
|
|
|
networks := f.Get("total_networks").(int64)
|
|
|
|
tests := f.Get("total_tests").(int64)
|
|
|
|
dataUp := f.Get("total_data_usage_up").(int64)
|
|
|
|
dataDown := f.Get("total_data_usage_down").(int64)
|
2018-06-22 11:57:25 +02:00
|
|
|
if tests == 0 {
|
|
|
|
fmt.Fprintf(w, "No results\n")
|
|
|
|
fmt.Fprintf(w, "Try running:\n")
|
|
|
|
fmt.Fprintf(w, " ooni run websites\n")
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-22 11:53:10 +02:00
|
|
|
// └┬──────────────┬──────────────┬──────────────┬
|
|
|
|
fmt.Fprintf(w, " │ %s │ %s │ %s │\n",
|
2018-06-25 16:31:44 +02:00
|
|
|
util.RightPad(fmt.Sprintf("%d tests", tests), 12),
|
|
|
|
util.RightPad(fmt.Sprintf("%d nets", networks), 12),
|
|
|
|
util.RightPad(fmt.Sprintf("%d ⬆ %d ⬇", dataUp, dataDown), 12))
|
2018-06-22 11:53:10 +02:00
|
|
|
fmt.Fprintf(w, " └──────────────┴──────────────┴──────────────┘\n")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|