Skip to content

Commit c60ea6d

Browse files
authored
Fix error when analysing big files (#12)
1 parent 9e5e20d commit c60ea6d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/database/report.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package database
33
import (
44
"errors"
55
"fmt"
6+
"slices"
67
"time"
78

9+
"github.com/crowdsecurity/ipdex/cmd/ipdex/config"
810
"gorm.io/gorm"
911
)
1012

@@ -102,10 +104,19 @@ func (r *ReportClient) Create(report *Report) error {
102104
report.FileHash = hash
103105
}
104106

105-
result := r.db.Create(report)
107+
result := r.db.Omit("IPs.*").Create(report)
106108
if result.Error != nil {
107109
return result.Error
108110
}
111+
112+
allIPs := report.IPs
113+
for batch := range slices.Chunk(allIPs, config.BatchSize) {
114+
report.IPs = batch
115+
if err := r.db.Model(report).Association("IPs").Append(batch); err != nil {
116+
return fmt.Errorf("failed to associate IPs with report: %w", err)
117+
}
118+
}
119+
109120
return nil
110121
}
111122

pkg/display/display.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ func displayIP(item *cticlient.SmokeItem, ipLastRefresh time.Time, detailed bool
118118
reputationStr += fmt.Sprintf(" (%s)", fps)
119119
}
120120

121+
if item.History.FirstSeen == nil {
122+
item.History.FirstSeen = new(string)
123+
*item.History.FirstSeen = "N/A"
124+
}
125+
if item.History.LastSeen == nil {
126+
item.History.LastSeen = new(string)
127+
*item.History.LastSeen = "N/A"
128+
}
129+
121130
rd.PrintRow("IP", item.Ip, keyStyle, valueStyle)
122131
rd.PrintRow("Reputation", reputationStr, keyStyle, GetReputationStyle(levelStyle, item.Reputation))
123132
rd.PrintRow("Confidence", item.Confidence, keyStyle, GetLevelStyle(levelStyle, item.Reputation))

0 commit comments

Comments
 (0)