Skip to content

Commit e4d7271

Browse files
author
jdv
committed
cleaning code a bit
1 parent 79620a4 commit e4d7271

File tree

1 file changed

+68
-151
lines changed

1 file changed

+68
-151
lines changed

pkg/display/display.go

Lines changed: 68 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -582,205 +582,134 @@ func saveReportCSV(item *models.Report, stats *models.ReportStats, withIPs bool)
582582
reportWriter := csv.NewWriter(reportFile)
583583
defer reportWriter.Flush()
584584

585-
// General section
586-
if err := reportWriter.Write([]string{"General", "", ""}); err != nil {
587-
return err
588-
}
589-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
590-
return err
591-
}
585+
// Collect all CSV rows
586+
var csvRows [][]string
592587

593-
if err := reportWriter.Write([]string{"Report ID", strconv.Itoa(int(item.ID)), ""}); err != nil {
594-
return err
595-
}
596-
if err := reportWriter.Write([]string{"Report Name", item.Name, ""}); err != nil {
597-
return err
598-
}
599-
if err := reportWriter.Write([]string{"Creation Date", item.CreatedAt.Format("2006-01-02 15:04:05"), ""}); err != nil {
600-
return err
601-
}
588+
// General section
589+
csvRows = append(csvRows, []string{"General", "", ""})
590+
csvRows = append(csvRows, []string{"", "", ""})
591+
csvRows = append(csvRows, []string{"Report ID", strconv.Itoa(int(item.ID)), ""})
592+
csvRows = append(csvRows, []string{"Report Name", item.Name, ""})
593+
csvRows = append(csvRows, []string{"Creation Date", item.CreatedAt.Format("2006-01-02 15:04:05"), ""})
602594

603595
if item.IsFile {
604-
if err := reportWriter.Write([]string{"File path", item.FilePath, ""}); err != nil {
605-
return err
606-
}
607-
if err := reportWriter.Write([]string{"SHA256", item.FileHash, ""}); err != nil {
608-
return err
609-
}
596+
csvRows = append(csvRows, []string{"File path", item.FilePath, ""})
597+
csvRows = append(csvRows, []string{"SHA256", item.FileHash, ""})
610598
}
611599

612600
if item.IsQuery {
613-
if err := reportWriter.Write([]string{"Query", item.Query, ""}); err != nil {
614-
return err
615-
}
616-
if err := reportWriter.Write([]string{"Since Duration", item.Since, ""}); err != nil {
617-
return err
618-
}
619-
if err := reportWriter.Write([]string{"Since Time", item.SinceTime.Format("2006-01-02 15:04:05"), ""}); err != nil {
620-
return err
621-
}
601+
csvRows = append(csvRows, []string{"Query", item.Query, ""})
602+
csvRows = append(csvRows, []string{"Since Duration", item.Since, ""})
603+
csvRows = append(csvRows, []string{"Since Time", item.SinceTime.Format("2006-01-02 15:04:05"), ""})
622604
}
623605

624-
if err := reportWriter.Write([]string{"Number of IPs", strconv.Itoa(len(item.IPs)), ""}); err != nil {
625-
return err
626-
}
606+
csvRows = append(csvRows, []string{"Number of IPs", strconv.Itoa(len(item.IPs)), ""})
627607

628608
knownIPPercent := float64(stats.NbIPs-stats.NbUnknownIPs) / float64(stats.NbIPs) * 100
629609
ipsInBlocklistPercent := float64(stats.IPsBlockedByBlocklist) / float64(stats.NbIPs) * 100
630610

631-
if err := reportWriter.Write([]string{"Number of known IPs", fmt.Sprintf("%d", stats.NbIPs-stats.NbUnknownIPs), fmt.Sprintf("%.0f%%", knownIPPercent)}); err != nil {
632-
return err
633-
}
634-
if err := reportWriter.Write([]string{"Number of IPs in Blocklist", fmt.Sprintf("%d", stats.IPsBlockedByBlocklist), fmt.Sprintf("%.0f%%", ipsInBlocklistPercent)}); err != nil {
635-
return err
636-
}
611+
csvRows = append(csvRows, []string{"Number of known IPs", fmt.Sprintf("%d", stats.NbIPs-stats.NbUnknownIPs), fmt.Sprintf("%.0f%%", knownIPPercent)})
612+
csvRows = append(csvRows, []string{"Number of IPs in Blocklist", fmt.Sprintf("%d", stats.IPsBlockedByBlocklist), fmt.Sprintf("%.0f%%", ipsInBlocklistPercent)})
637613

638614
// Empty line before Stats section
639-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
640-
return err
641-
}
615+
csvRows = append(csvRows, []string{"", "", ""})
642616

643617
// Stats section
644-
if err := reportWriter.Write([]string{"Stats", "", ""}); err != nil {
645-
return err
646-
}
647-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
648-
return err
649-
}
618+
csvRows = append(csvRows, []string{"Stats", "", ""})
619+
csvRows = append(csvRows, []string{"", "", ""})
650620

651621
// Top Reputation
652622
TopReputation := getTopN(stats.TopReputation, maxTopDisplayReport)
653623
if len(TopReputation) > 0 {
654-
if err := reportWriter.Write([]string{"🌟 Top Reputation", "", ""}); err != nil {
655-
return err
656-
}
624+
csvRows = append(csvRows, []string{"🌟 Top Reputation", "", ""})
657625
for _, stat := range TopReputation {
658626
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
659-
if err := reportWriter.Write([]string{cases.Title(language.Und).String(stat.Key), fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
660-
return err
661-
}
662-
}
663-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
664-
return err
627+
csvRows = append(csvRows, []string{cases.Title(language.Und).String(stat.Key), fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
665628
}
629+
csvRows = append(csvRows, []string{"", "", ""})
666630
}
667631

668632
// Top Classifications
669633
topClassification := getTopN(stats.TopClassifications, maxTopDisplayReport)
670634
if len(topClassification) > 0 {
671-
if err := reportWriter.Write([]string{"🗂️ Top Classifications", "", ""}); err != nil {
672-
return err
673-
}
635+
csvRows = append(csvRows, []string{"🗂️ Top Classifications", "", ""})
674636
for _, stat := range topClassification {
675637
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
676-
if err := reportWriter.Write([]string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
677-
return err
678-
}
679-
}
680-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
681-
return err
638+
csvRows = append(csvRows, []string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
682639
}
640+
csvRows = append(csvRows, []string{"", "", ""})
683641
}
684642

685643
// Top Behaviors
686644
topBehaviors := getTopN(stats.TopBehaviors, maxTopDisplayReport)
687645
if len(topBehaviors) > 0 {
688-
if err := reportWriter.Write([]string{"🤖 Top Behaviors", "", ""}); err != nil {
689-
return err
690-
}
646+
csvRows = append(csvRows, []string{"🤖 Top Behaviors", "", ""})
691647
for _, stat := range topBehaviors {
692648
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
693-
if err := reportWriter.Write([]string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
694-
return err
695-
}
696-
}
697-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
698-
return err
649+
csvRows = append(csvRows, []string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
699650
}
651+
csvRows = append(csvRows, []string{"", "", ""})
700652
}
701653

702654
// Top Blocklists
703655
topBlocklists := getTopN(stats.TopBlocklists, maxTopDisplayReport)
704656
if len(topBlocklists) > 0 {
705-
if err := reportWriter.Write([]string{"⛔ Top Blocklists", "", ""}); err != nil {
706-
return err
707-
}
657+
csvRows = append(csvRows, []string{"⛔ Top Blocklists", "", ""})
708658
for _, stat := range topBlocklists {
709659
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
710-
if err := reportWriter.Write([]string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
711-
return err
712-
}
713-
}
714-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
715-
return err
660+
csvRows = append(csvRows, []string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
716661
}
662+
csvRows = append(csvRows, []string{"", "", ""})
717663
}
718664

719665
// Top CVEs
720666
topCVEs := getTopN(stats.TopCVEs, maxTopDisplayReport)
721667
if len(topCVEs) > 0 {
722-
if err := reportWriter.Write([]string{"💥 Top CVEs", "", ""}); err != nil {
723-
return err
724-
}
668+
csvRows = append(csvRows, []string{"💥 Top CVEs", "", ""})
725669
for _, stat := range topCVEs {
726670
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
727-
if err := reportWriter.Write([]string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
728-
return err
729-
}
730-
}
731-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
732-
return err
671+
csvRows = append(csvRows, []string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
733672
}
673+
csvRows = append(csvRows, []string{"", "", ""})
734674
}
735675

736676
// Top IP Ranges
737677
TopIPRange := getTopN(stats.TopIPRange, maxTopDisplayReport)
738678
if len(TopIPRange) > 0 {
739-
if err := reportWriter.Write([]string{"🌐 Top IP Ranges", "", ""}); err != nil {
740-
return err
741-
}
679+
csvRows = append(csvRows, []string{"🌐 Top IP Ranges", "", ""})
742680
for _, stat := range TopIPRange {
743681
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
744-
if err := reportWriter.Write([]string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
745-
return err
746-
}
747-
}
748-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
749-
return err
682+
csvRows = append(csvRows, []string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
750683
}
684+
csvRows = append(csvRows, []string{"", "", ""})
751685
}
752686

753687
// Top Autonomous Systems
754688
topAS := getTopN(stats.TopAS, maxTopDisplayReport)
755689
if len(topAS) > 0 {
756-
if err := reportWriter.Write([]string{"🛰️ Top Autonomous Systems", "", ""}); err != nil {
757-
return err
758-
}
690+
csvRows = append(csvRows, []string{"🛰️ Top Autonomous Systems", "", ""})
759691
for _, stat := range topAS {
760692
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
761-
if err := reportWriter.Write([]string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
762-
return err
763-
}
764-
}
765-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
766-
return err
693+
csvRows = append(csvRows, []string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
767694
}
695+
csvRows = append(csvRows, []string{"", "", ""})
768696
}
769697

770698
// Top Countries
771699
topCountry := getTopN(stats.TopCountries, maxTopDisplayReport)
772700
if len(topCountry) > 0 {
773-
if err := reportWriter.Write([]string{"🌎 Top Countries", "", ""}); err != nil {
774-
return err
775-
}
701+
csvRows = append(csvRows, []string{"🌎 Top Countries", "", ""})
776702
for _, stat := range topCountry {
777703
percent := float64(stat.Value) / float64(stats.NbIPs) * 100
778-
if err := reportWriter.Write([]string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)}); err != nil {
779-
return err
780-
}
704+
csvRows = append(csvRows, []string{stat.Key, fmt.Sprintf("%d", stat.Value), fmt.Sprintf("%.0f%%", percent)})
781705
}
782-
if err := reportWriter.Write([]string{"", "", ""}); err != nil {
783-
return err
706+
csvRows = append(csvRows, []string{"", "", ""})
707+
}
708+
709+
// Write all rows at once
710+
for _, row := range csvRows {
711+
if err := reportWriter.Write(row); err != nil {
712+
return fmt.Errorf("failed to write CSV row: %v", err)
784713
}
785714
}
786715

@@ -798,24 +727,16 @@ func saveReportCSV(item *models.Report, stats *models.ReportStats, withIPs bool)
798727
detailsWriter := csv.NewWriter(detailsFile)
799728
defer detailsWriter.Flush()
800729

801-
// Write the header for IP details
802-
if err := detailsWriter.Write([]string{
803-
"IP",
804-
"Country",
805-
"AS Name",
806-
"Reputation",
807-
"Confidence",
808-
"Reverse DNS",
809-
"Profile",
810-
"Behaviors",
811-
"Range",
812-
"First Seen",
813-
"Last Seen",
814-
}); err != nil {
815-
return err
816-
}
730+
// Collect all IP detail rows
731+
var detailRows [][]string
732+
733+
// Header
734+
detailRows = append(detailRows, []string{
735+
"IP", "Country", "AS Name", "Reputation", "Confidence",
736+
"Reverse DNS", "Profile", "Behaviors", "Range", "First Seen", "Last Seen",
737+
})
817738

818-
// Write IP data
739+
// IP data
819740
for _, ipItem := range item.IPs {
820741
country := "N/A"
821742
ipRange := "N/A"
@@ -877,20 +798,16 @@ func saveReportCSV(item *models.Report, stats *models.ReportStats, withIPs bool)
877798
confidence = "N/A"
878799
}
879800

880-
if err := detailsWriter.Write([]string{
881-
ipItem.Ip,
882-
country,
883-
asName,
884-
reputation,
885-
confidence,
886-
reverseDNS,
887-
classif,
888-
behaviors,
889-
ipRange,
890-
firstSeen,
891-
lastSeen,
892-
}); err != nil {
893-
return err
801+
detailRows = append(detailRows, []string{
802+
ipItem.Ip, country, asName, reputation, confidence,
803+
reverseDNS, classif, behaviors, ipRange, firstSeen, lastSeen,
804+
})
805+
}
806+
807+
// Write all detail rows at once
808+
for _, row := range detailRows {
809+
if err := detailsWriter.Write(row); err != nil {
810+
return fmt.Errorf("failed to write detail CSV row: %v", err)
894811
}
895812
}
896813

0 commit comments

Comments
 (0)