Skip to content

Commit abbce08

Browse files
committed
Better result summary output
1 parent 4219eec commit abbce08

File tree

1 file changed

+54
-22
lines changed

1 file changed

+54
-22
lines changed

internal/grawl/grawler.go

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"regexp"
1313
"slices"
14+
"sort"
1415
"strings"
1516
"time"
1617
)
@@ -21,11 +22,13 @@ const (
2122
)
2223

2324
type Grawler struct {
24-
flags Flags
25-
headerAuth string
26-
requestCount uint32
27-
responseCount int
28-
errorCount uint32
25+
flags Flags
26+
headerAuth string
27+
requestCount uint32
28+
responseCount int
29+
errorCount uint32
30+
//durationMin int
31+
//durationMax int
2932
totalDuration time.Duration
3033
runningRequests *RunningRequests
3134
fileWriter *FileWriter
@@ -271,20 +274,6 @@ func (g *Grawler) Grawl(url string) {
271274
g.printSummary()
272275
}
273276

274-
func isXmlResponse(resp *colly.Response) bool {
275-
contentType := strings.ToLower(resp.Headers.Get("Content-Type"))
276-
isXMLFile := strings.HasSuffix(strings.ToLower(resp.Request.URL.Path), ".xml") || strings.HasSuffix(strings.ToLower(resp.Request.URL.Path), ".xml.gz")
277-
isXmlContentType := strings.Contains(contentType, "xml")
278-
isHtmlContentType := strings.Contains(contentType, "html")
279-
280-
return !isHtmlContentType && (isXMLFile || isXmlContentType)
281-
}
282-
283-
func isHtmlResponse(resp *colly.Response) bool {
284-
contentType := strings.ToLower(resp.Headers.Get("Content-Type"))
285-
return strings.Contains(contentType, "html")
286-
}
287-
288277
func (g *Grawler) visit(c *colly.Collector, r *colly.Request, url string, foundOnUrl string) {
289278
url = strings.Trim(url, " ")
290279
visited, err := c.HasVisited(url)
@@ -296,13 +285,42 @@ func (g *Grawler) visit(c *colly.Collector, r *colly.Request, url string, foundO
296285
}
297286

298287
func (g *Grawler) printSummary() {
288+
durationMin := time.Hour
289+
durationMax := time.Duration(0)
290+
returnCodes := map[int]int{}
291+
returnErrors := 0
292+
for _, result := range *g.runningRequests.GetValues() {
293+
durationMin = min(durationMin, result.duration)
294+
durationMax = max(durationMax, result.duration)
295+
if result.statusCode > 0 {
296+
returnCodes[result.statusCode]++
297+
} else {
298+
returnErrors++
299+
}
300+
}
301+
302+
// Eine Slice für die Schlüssel erstellen
303+
returnCodeKeys := make([]int, 0, len(returnCodes))
304+
305+
// Alle Schlüssel der Map sammeln
306+
for key := range returnCodes {
307+
returnCodeKeys = append(returnCodeKeys, key)
308+
}
309+
310+
// Die Schlüssel aufsteigend sortieren
311+
sort.Ints(returnCodeKeys)
312+
299313
fmt.Println("")
300314
fmt.Println("Grawling finished at:", time.Now().Format(DateFormat))
301315
fmt.Println("Duration: ", g.totalDuration.Round(time.Millisecond))
302-
fmt.Println("Avg request duration:", time.Duration(int64(g.totalDuration)/int64(g.requestCount)).Round(time.Millisecond))
303-
fmt.Println("Responses: ", g.responseCount)
316+
fmt.Println(" - Min: ", durationMin.Round(time.Millisecond))
317+
fmt.Println(" - Max: ", durationMax.Round(time.Millisecond))
318+
fmt.Println(" - Avg: ", time.Duration(int64(g.totalDuration)/int64(g.requestCount)).Round(time.Millisecond))
304319
fmt.Println("Requests: ", g.requestCount)
305-
fmt.Println("Errors/Skipped: ", g.errorCount)
320+
for _, code := range returnCodeKeys {
321+
fmt.Printf(" - Status code %d: %d\n", code, returnCodes[code])
322+
}
323+
fmt.Printf(" - Other errors: %d\n", returnErrors)
306324
}
307325

308326
func (g *Grawler) promptPassword() (string, error) {
@@ -392,3 +410,17 @@ func (g *Grawler) promptResume() interface{} {
392410
}
393411
}
394412
}
413+
414+
func isXmlResponse(resp *colly.Response) bool {
415+
contentType := strings.ToLower(resp.Headers.Get("Content-Type"))
416+
isXMLFile := strings.HasSuffix(strings.ToLower(resp.Request.URL.Path), ".xml") || strings.HasSuffix(strings.ToLower(resp.Request.URL.Path), ".xml.gz")
417+
isXmlContentType := strings.Contains(contentType, "xml")
418+
isHtmlContentType := strings.Contains(contentType, "html")
419+
420+
return !isHtmlContentType && (isXMLFile || isXmlContentType)
421+
}
422+
423+
func isHtmlResponse(resp *colly.Response) bool {
424+
contentType := strings.ToLower(resp.Headers.Get("Content-Type"))
425+
return strings.Contains(contentType, "html")
426+
}

0 commit comments

Comments
 (0)