Skip to content

Commit 9a8bca8

Browse files
authored
Compatibility and Improved output (#34)
* 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 <[email protected]>
1 parent 08d21d6 commit 9a8bca8

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

defs/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ func (s *Server) IsUp() bool {
6060
}
6161
defer resp.Body.Close()
6262
b, _ := ioutil.ReadAll(resp.Body)
63+
if len(b) > 0 {
64+
log.Debugf("Failed when parsing get IP result: %s", b)
65+
}
6366
// only return online if the ping URL returns nothing and 200
64-
return len(b) == 0 && resp.StatusCode == http.StatusOK
67+
return resp.StatusCode == http.StatusOK
6568
}
6669

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

speedtest/helper.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
3232
log.Infof("Testing against %d servers", serverCount)
3333
}
3434

35+
var reps_json []report.JSONReport
36+
var reps_csv []report.CSVReport
37+
3538
// fetch current user's IP info
3639
for _, currentServer := range servers {
3740
// get telemetry level
@@ -140,8 +143,6 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
140143
// check for --csv or --json. the program prioritize the --csv before the --json. this is the same behavior as speedtest-cli
141144
if c.Bool(defs.OptionCSV) {
142145
// print csv if --csv is given
143-
var reps []report.CSVReport
144-
145146
var rep report.CSVReport
146147
rep.Timestamp = time.Now()
147148

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

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

181175
rep.Client = report.Client{ispInfo.RawISPInfo}
182176
rep.Client.Readme = ""
183-
184-
if b, err := json.Marshal(&rep); err != nil {
185-
log.Errorf("Error generating JSON report: %s", err)
186-
} else {
187-
log.Warnf("%s", b)
188-
}
177+
178+
reps_json = append(reps_json,rep)
189179
}
190180
} else {
191181
log.Infof("Selected server %s (%s) is not responding at the moment, try again later", currentServer.Name, u.Hostname())
192182
}
193183

194-
// add a new line after each test if testing multiple servers
195-
if len(servers) > 1 {
184+
//add a new line after each test if testing multiple servers
185+
if ( len(servers) > 1 && !silent){
196186
log.Warn()
197187
}
198188
}
199189

190+
// check for --csv or --json. the program prioritize the --csv before the --json. this is the same behavior as speedtest-cli
191+
if c.Bool(defs.OptionCSV) {
192+
var buf bytes.Buffer
193+
if err := gocsv.MarshalWithoutHeaders(&reps_csv, &buf); err != nil {
194+
log.Errorf("Error generating CSV report: %s", err)
195+
} else {
196+
log.Warn(buf.String())
197+
}
198+
} else if c.Bool(defs.OptionJSON) {
199+
if b, err := json.Marshal(&reps_json); err != nil {
200+
log.Errorf("Error generating JSON report: %s", err)
201+
} else {
202+
log.Warnf("%s", b)
203+
}
204+
}
205+
200206
return nil
201207
}
202208

0 commit comments

Comments
 (0)