Skip to content

Commit d0efc07

Browse files
authored
Fix reports with too many IPs (#20)
* Fix reports with too many IPs
1 parent cc85492 commit d0efc07

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

pkg/database/report.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,19 @@ func (r *ReportClient) GetExpiredIPsFromReport(reportID uint) ([]*IP, error) {
6666

6767
func (r *ReportClient) FindById(reportID uint) (*Report, error) {
6868
var report Report
69-
result := r.db.Preload("IPs").Preload("StatsReport").First(&report, reportID)
69+
result := r.db.Preload("StatsReport").First(&report, reportID)
7070
if result.Error != nil {
7171
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
7272
return nil, nil
7373
}
7474
return nil, result.Error
7575
}
7676

77+
err := r.db.Model(&report).Association("IPs").Find(&report.IPs)
78+
if err != nil {
79+
return nil, err
80+
}
81+
7782
return &report, nil
7883
}
7984

@@ -85,13 +90,19 @@ func (r *ReportClient) FindByHash(filepath string) (*Report, error) {
8590
return nil, err
8691
}
8792

88-
result := r.db.Preload("IPs").Preload("StatsReport").Where("file_hash = ?", hash).First(&report)
93+
result := r.db.Preload("StatsReport").Where("file_hash = ?", hash).First(&report)
8994
if result.Error != nil {
9095
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
9196
return nil, nil
9297
}
9398
return nil, result.Error
9499
}
100+
101+
err = r.db.Model(&report).Association("IPs").Find(&report.IPs)
102+
if err != nil {
103+
return nil, err
104+
}
105+
95106
return &report, nil
96107
}
97108

@@ -131,10 +142,21 @@ func (r *ReportClient) Find(reportID string) (*Report, error) {
131142

132143
func (r *ReportClient) FindAll() ([]*Report, error) {
133144
reports := []*Report{}
134-
result := r.db.Preload("IPs").Preload("StatsReport").Find(&reports)
145+
146+
// First, get all reports with StatsReport only
147+
result := r.db.Preload("StatsReport").Find(&reports)
135148
if result.Error != nil {
136149
return nil, result.Error
137150
}
151+
152+
// Then load IPs using Association API for each report (GORM handles batching internally)
153+
for _, report := range reports {
154+
err := r.db.Model(report).Association("IPs").Find(&report.IPs)
155+
if err != nil {
156+
return nil, err
157+
}
158+
}
159+
138160
return reports, nil
139161
}
140162

@@ -158,7 +180,7 @@ func (r *ReportClient) DeleteExpiredSince(expirationDate time.Time) error {
158180

159181
func (r *ReportClient) FilePathExist(filePath string) (*Report, bool, error) {
160182
var reports []Report
161-
result := r.db.Model(&Report{}).Preload("IPs").Where("file_path = ?", filePath).Find(&reports)
183+
result := r.db.Model(&Report{}).Where("file_path = ?", filePath).Find(&reports)
162184
if result.Error != nil {
163185
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
164186
return nil, false, nil
@@ -168,5 +190,11 @@ func (r *ReportClient) FilePathExist(filePath string) (*Report, bool, error) {
168190
if len(reports) == 0 {
169191
return nil, false, nil
170192
}
193+
194+
err := r.db.Model(&reports[0]).Association("IPs").Find(&reports[0].IPs)
195+
if err != nil {
196+
return nil, false, err
197+
}
198+
171199
return &reports[0], true, nil
172200
}

0 commit comments

Comments
 (0)