Skip to content

Commit

Permalink
Compatibility and Improved output (#34)
Browse files Browse the repository at this point in the history
* Added compatibility for cambridge-fibre backend

GetIP for speedtest.cambridgefibre.uk retuns
* a body which is not empty when queried with empty parameters
* Just a string with IP informaiton (no distance)
The code will not stop in this case but will printout a debug message.

* Improved format for JSON and CSV output for multiple server.

The JSON output is now a list of objects one per server.
The CSV output has no empty rows.

Co-authored-by: Cisco Cervellera <ciscoski@users.noreply.github.com>
  • Loading branch information
ciscoski and ciscoski authored May 15, 2021
1 parent 08d21d6 commit 9a8bca8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
6 changes: 5 additions & 1 deletion defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ func (s *Server) IsUp() bool {
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
if len(b) > 0 {
log.Debugf("Failed when parsing get IP result: %s", b)
}
// only return online if the ping URL returns nothing and 200
return len(b) == 0 && resp.StatusCode == http.StatusOK
return resp.StatusCode == http.StatusOK
}

// ICMPPingAndJitter pings the server via ICMP echos and calculate the average ping and jitter
Expand Down Expand Up @@ -412,6 +415,7 @@ func (s *Server) GetIPInfo(distanceUnit string) (*GetIPResult, error) {
if err := json.Unmarshal(b, &ipInfo); err != nil {
log.Debugf("Failed when parsing get IP result: %s", err)
log.Debugf("Received payload: %s", b)
ipInfo.ProcessedString = string(b[:])
}
}

Expand Down
42 changes: 24 additions & 18 deletions speedtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ 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

// fetch current user's IP info
for _, currentServer := range servers {
// get telemetry level
Expand Down Expand Up @@ -140,8 +143,6 @@ 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 reps []report.CSVReport

var rep report.CSVReport
rep.Timestamp = time.Now()

Expand All @@ -154,14 +155,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
rep.Share = shareLink
rep.IP = ispInfo.RawISPInfo.IP

reps = append(reps, rep)

var buf bytes.Buffer
if err := gocsv.MarshalWithoutHeaders(&reps, &buf); err != nil {
log.Errorf("Error generating CSV report: %s", err)
} else {
log.Warn(buf.String())
}
reps_csv = append(reps_csv, rep)
} else if c.Bool(defs.OptionJSON) {
// print json if --json is given
var rep report.JSONReport
Expand All @@ -180,23 +174,35 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel

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

if b, err := json.Marshal(&rep); err != nil {
log.Errorf("Error generating JSON report: %s", err)
} else {
log.Warnf("%s", b)
}

reps_json = append(reps_json,rep)
}
} else {
log.Infof("Selected server %s (%s) is not responding at the moment, try again later", currentServer.Name, u.Hostname())
}

// add a new line after each test if testing multiple servers
if len(servers) > 1 {
//add a new line after each test if testing multiple servers
if ( len(servers) > 1 && !silent){
log.Warn()
}
}

// 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 {
log.Errorf("Error generating CSV report: %s", err)
} else {
log.Warn(buf.String())
}
} else if c.Bool(defs.OptionJSON) {
if b, err := json.Marshal(&reps_json); err != nil {
log.Errorf("Error generating JSON report: %s", err)
} else {
log.Warnf("%s", b)
}
}

return nil
}

Expand Down

0 comments on commit 9a8bca8

Please sign in to comment.