Skip to content

Commit 417d065

Browse files
committed
improved report objects
1 parent 092760f commit 417d065

File tree

4 files changed

+54
-60
lines changed

4 files changed

+54
-60
lines changed

report/csv.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

report/json.go renamed to report/testreport.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// JSONReport represents the output data fields in a JSON file
10-
type JSONReport struct {
10+
type Report struct {
1111
Timestamp time.Time `json:"timestamp"`
1212
Server Server `json:"server"`
1313
Client Client `json:"client"`
@@ -20,6 +20,18 @@ type JSONReport struct {
2020
Share string `json:"share"`
2121
}
2222

23+
type FlatReport struct {
24+
Timestamp time.Time `csv:"Timestamp"`
25+
Name string `csv:"Server Name"`
26+
Address string `csv:"Address"`
27+
Ping float64 `csv:"Ping"`
28+
Jitter float64 `csv:"Jitter"`
29+
Download float64 `csv:"Download"`
30+
Upload float64 `csv:"Upload"`
31+
Share string `csv:"Share"`
32+
IP string `csv:"IP"`
33+
}
34+
2335
// Server represents the speed test server's information
2436
type Server struct {
2537
Name string `json:"name"`
@@ -30,3 +42,19 @@ type Server struct {
3042
type Client struct {
3143
defs.IPInfoResponse
3244
}
45+
46+
func (r Report) GetFlatReport() FlatReport {
47+
var rep FlatReport
48+
rep.Timestamp = time.Now()
49+
50+
rep.Name = r.Server.Name
51+
rep.Address = r.Server.URL
52+
rep.Ping = r.Ping
53+
rep.Jitter = r.Jitter
54+
rep.Download = r.Download
55+
rep.Upload = r.Upload
56+
rep.Share = r.Share
57+
rep.IP = r.Client.IP
58+
59+
return rep
60+
}

speedtest/helper.go

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

36-
var reps_json []report.JSONReport
37-
var reps_csv []report.CSVReport
36+
var reps []report.Report
3837

3938
// fetch current user's IP info
4039
for _, currentServer := range servers {
@@ -141,43 +140,24 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
141140
}
142141
}
143142

144-
// check for --csv or --json. the program prioritize the --csv before the --json. this is the same behavior as speedtest-cli
145-
if c.Bool(defs.OptionCSV) {
146-
// print csv if --csv is given
147-
var rep report.CSVReport
148-
rep.Timestamp = time.Now()
149-
150-
rep.Name = currentServer.Name
151-
rep.Address = u.String()
152-
rep.Ping = math.Round(p*100) / 100
153-
rep.Jitter = math.Round(jitter*100) / 100
154-
rep.Download = math.Round(downloadValue*100) / 100
155-
rep.Upload = math.Round(uploadValue*100) / 100
156-
rep.Share = shareLink
157-
rep.IP = ispInfo.RawISPInfo.IP
158-
159-
reps_csv = append(reps_csv, rep)
160-
} else if c.Bool(defs.OptionJSON) {
161-
// print json if --json is given
162-
var rep report.JSONReport
163-
rep.Timestamp = time.Now()
164-
165-
rep.Ping = math.Round(p*100) / 100
166-
rep.Jitter = math.Round(jitter*100) / 100
167-
rep.Download = math.Round(downloadValue*100) / 100
168-
rep.Upload = math.Round(uploadValue*100) / 100
169-
rep.BytesReceived = bytesRead
170-
rep.BytesSent = bytesWritten
171-
rep.Share = shareLink
172-
173-
rep.Server.Name = currentServer.Name
174-
rep.Server.URL = u.String()
175-
176-
rep.Client = report.Client{ispInfo.RawISPInfo}
177-
rep.Client.Readme = ""
178-
179-
reps_json = append(reps_json, rep)
180-
}
143+
var rep report.Report
144+
rep.Timestamp = time.Now()
145+
146+
rep.Ping = math.Round(p*100) / 100
147+
rep.Jitter = math.Round(jitter*100) / 100
148+
rep.Download = math.Round(downloadValue*100) / 100
149+
rep.Upload = math.Round(uploadValue*100) / 100
150+
rep.BytesReceived = bytesRead
151+
rep.BytesSent = bytesWritten
152+
rep.Share = shareLink
153+
154+
rep.Server.Name = currentServer.Name
155+
rep.Server.URL = u.String()
156+
157+
rep.Client = report.Client{IPInfoResponse: ispInfo.RawISPInfo}
158+
rep.Client.Readme = ""
159+
160+
reps = append(reps, rep)
181161
} else {
182162
log.Infof("Selected server %s (%s) is not responding at the moment, try again later", currentServer.Name, u.Hostname())
183163
}
@@ -191,13 +171,17 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
191171
// check for --csv or --json. the program prioritize the --csv before the --json. this is the same behavior as speedtest-cli
192172
if c.Bool(defs.OptionCSV) {
193173
var buf bytes.Buffer
174+
var reps_csv []report.FlatReport
175+
for _, rep := range reps {
176+
reps_csv = append(reps_csv, rep.GetFlatReport())
177+
}
194178
if err := gocsv.MarshalWithoutHeaders(&reps_csv, &buf); err != nil {
195179
log.Errorf("Error generating CSV report: %s", err)
196180
} else {
197181
os.Stdout.WriteString(buf.String())
198182
}
199183
} else if c.Bool(defs.OptionJSON) {
200-
if b, err := json.Marshal(&reps_json); err != nil {
184+
if b, err := json.Marshal(&reps); err != nil {
201185
log.Errorf("Error generating JSON report: %s", err)
202186
} else {
203187
os.Stdout.Write(b[:])

speedtest/speedtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func SpeedTest(c *cli.Context) error {
7979

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

0 commit comments

Comments
 (0)