Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved reports #63

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions report/csv.go

This file was deleted.

32 changes: 0 additions & 32 deletions report/json.go

This file was deleted.

61 changes: 61 additions & 0 deletions report/testreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package report

import (
"time"

"github.com/librespeed/speedtest-cli/defs"
)

// Report represents the output data fields in a nestable file data such as JSON.
type Report struct {
Timestamp time.Time `json:"timestamp"`
Server Server `json:"server"`
Client Client `json:"client"`
BytesSent int `json:"bytes_sent"`
BytesReceived int `json:"bytes_received"`
Ping float64 `json:"ping"`
Jitter float64 `json:"jitter"`
Upload float64 `json:"upload"`
Download float64 `json:"download"`
Share string `json:"share"`
}

// FlatReport represents the output data fields in a flat file data such as CSV.
type FlatReport struct {
Timestamp time.Time `csv:"Timestamp"`
Name string `csv:"Server Name"`
Address string `csv:"Address"`
Ping float64 `csv:"Ping"`
Jitter float64 `csv:"Jitter"`
Download float64 `csv:"Download"`
Upload float64 `csv:"Upload"`
Share string `csv:"Share"`
IP string `csv:"IP"`
}

// Server represents the speed test server's information
type Server struct {
Name string `json:"name"`
URL string `json:"url"`
}

// Client represents the speed test client's information
type Client struct {
defs.IPInfoResponse
}

func (r Report) GetFlatReport() FlatReport {
var rep FlatReport

rep.Timestamp = r.Timestamp
rep.Name = r.Server.Name
rep.Address = r.Server.URL
rep.Ping = r.Ping
rep.Jitter = r.Jitter
rep.Download = r.Download
rep.Upload = r.Upload
rep.Share = r.Share
rep.IP = r.Client.IP

return rep
}
72 changes: 27 additions & 45 deletions speedtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"math"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
"time"
Expand All @@ -33,8 +32,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
log.Infof("Testing against %d servers", serverCount)
}

var reps_json []report.JSONReport
var reps_csv []report.CSVReport
var reps []report.Report

// fetch current user's IP info
for _, currentServer := range servers {
Expand Down Expand Up @@ -141,43 +139,24 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
}
}

// check for --csv or --json. the program prioritize the --csv before the --json. this is the same behavior as speedtest-cli
if c.Bool(defs.OptionCSV) {
// print csv if --csv is given
var rep report.CSVReport
rep.Timestamp = time.Now()

rep.Name = currentServer.Name
rep.Address = u.String()
rep.Ping = math.Round(p*100) / 100
rep.Jitter = math.Round(jitter*100) / 100
rep.Download = math.Round(downloadValue*100) / 100
rep.Upload = math.Round(uploadValue*100) / 100
rep.Share = shareLink
rep.IP = ispInfo.RawISPInfo.IP

reps_csv = append(reps_csv, rep)
} else if c.Bool(defs.OptionJSON) {
// print json if --json is given
var rep report.JSONReport
rep.Timestamp = time.Now()

rep.Ping = math.Round(p*100) / 100
rep.Jitter = math.Round(jitter*100) / 100
rep.Download = math.Round(downloadValue*100) / 100
rep.Upload = math.Round(uploadValue*100) / 100
rep.BytesReceived = bytesRead
rep.BytesSent = bytesWritten
rep.Share = shareLink

rep.Server.Name = currentServer.Name
rep.Server.URL = u.String()

rep.Client = report.Client{ispInfo.RawISPInfo}
rep.Client.Readme = ""

reps_json = append(reps_json, rep)
}
var rep report.Report
rep.Timestamp = time.Now()

rep.Ping = math.Round(p*100) / 100
rep.Jitter = math.Round(jitter*100) / 100
rep.Download = math.Round(downloadValue*100) / 100
rep.Upload = math.Round(uploadValue*100) / 100
rep.BytesReceived = bytesRead
rep.BytesSent = bytesWritten
rep.Share = shareLink

rep.Server.Name = currentServer.Name
rep.Server.URL = u.String()

rep.Client = report.Client{IPInfoResponse: ispInfo.RawISPInfo}
rep.Client.Readme = ""

reps = append(reps, rep)
} else {
log.Infof("Selected server %s (%s) is not responding at the moment, try again later", currentServer.Name, u.Hostname())
}
Expand All @@ -190,17 +169,20 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel

// check for --csv or --json. the program prioritize the --csv before the --json. this is the same behavior as speedtest-cli
if c.Bool(defs.OptionCSV) {
var buf bytes.Buffer
if err := gocsv.MarshalWithoutHeaders(&reps_csv, &buf); err != nil {
var reps_csv []report.FlatReport
for _, rep := range reps {
reps_csv = append(reps_csv, rep.GetFlatReport())
}
if resultStrig, err := gocsv.MarshalStringWithoutHeaders(&reps_csv); err != nil {
log.Errorf("Error generating CSV report: %s", err)
} else {
os.Stdout.WriteString(buf.String())
fmt.Print(resultStrig)
}
} else if c.Bool(defs.OptionJSON) {
if b, err := json.Marshal(&reps_json); err != nil {
if jsonBytes, err := json.Marshal(&reps); err != nil {
log.Errorf("Error generating JSON report: %s", err)
} else {
os.Stdout.Write(b[:])
fmt.Println(string(jsonBytes))
}
}

Expand Down
6 changes: 3 additions & 3 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func SpeedTest(c *cli.Context) error {

// if --csv-header is given, print the header and exit (same behavior speedtest-cli)
if c.Bool(defs.OptionCSVHeader) {
var rep []report.CSVReport
b, _ := gocsv.MarshalBytes(&rep)
os.Stdout.WriteString(string(b))
var rep []report.FlatReport
header, _ := gocsv.MarshalString(&rep)
fmt.Print(header)
return nil
}

Expand Down