From a231fb407db6b922acb2e8dc52dda61be3379787 Mon Sep 17 00:00:00 2001 From: Kevin KADOSH Date: Thu, 4 Dec 2025 17:23:26 +0100 Subject: [PATCH 1/2] Fix reports with too many IPs --- pkg/database/database.go | 2 +- pkg/database/report.go | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/database/database.go b/pkg/database/database.go index 88bb754..2c5030d 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -13,7 +13,7 @@ type Client struct { func NewClient(sqliteDBPath string) (*Client, error) { client := &Client{} - db, err := gorm.Open(sqlite.Open(sqliteDBPath), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) + db, err := gorm.Open(sqlite.Open(sqliteDBPath), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)}) if err != nil { return client, err } diff --git a/pkg/database/report.go b/pkg/database/report.go index a18fa2b..177f91a 100644 --- a/pkg/database/report.go +++ b/pkg/database/report.go @@ -66,7 +66,7 @@ func (r *ReportClient) GetExpiredIPsFromReport(reportID uint) ([]*IP, error) { func (r *ReportClient) FindById(reportID uint) (*Report, error) { var report Report - result := r.db.Preload("IPs").Preload("StatsReport").First(&report, reportID) + result := r.db.Preload("StatsReport").First(&report, reportID) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, nil @@ -74,6 +74,12 @@ func (r *ReportClient) FindById(reportID uint) (*Report, error) { return nil, result.Error } + // Load IPs using Association API + err := r.db.Model(&report).Association("IPs").Find(&report.IPs) + if err != nil { + return nil, err + } + return &report, nil } @@ -85,13 +91,20 @@ func (r *ReportClient) FindByHash(filepath string) (*Report, error) { return nil, err } - result := r.db.Preload("IPs").Preload("StatsReport").Where("file_hash = ?", hash).First(&report) + result := r.db.Preload("StatsReport").Where("file_hash = ?", hash).First(&report) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, nil } return nil, result.Error } + + // Load IPs using Association API + err = r.db.Model(&report).Association("IPs").Find(&report.IPs) + if err != nil { + return nil, err + } + return &report, nil } @@ -131,10 +144,21 @@ func (r *ReportClient) Find(reportID string) (*Report, error) { func (r *ReportClient) FindAll() ([]*Report, error) { reports := []*Report{} - result := r.db.Preload("IPs").Preload("StatsReport").Find(&reports) + + // First, get all reports with StatsReport only + result := r.db.Preload("StatsReport").Find(&reports) if result.Error != nil { return nil, result.Error } + + // Then load IPs using Association API for each report (GORM handles batching internally) + for _, report := range reports { + err := r.db.Model(report).Association("IPs").Find(&report.IPs) + if err != nil { + return nil, err + } + } + return reports, nil } @@ -158,7 +182,7 @@ func (r *ReportClient) DeleteExpiredSince(expirationDate time.Time) error { func (r *ReportClient) FilePathExist(filePath string) (*Report, bool, error) { var reports []Report - result := r.db.Model(&Report{}).Preload("IPs").Where("file_path = ?", filePath).Find(&reports) + result := r.db.Model(&Report{}).Where("file_path = ?", filePath).Find(&reports) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, false, nil @@ -168,5 +192,12 @@ func (r *ReportClient) FilePathExist(filePath string) (*Report, bool, error) { if len(reports) == 0 { return nil, false, nil } + + // Load IPs using Association API + err := r.db.Model(&reports[0]).Association("IPs").Find(&reports[0].IPs) + if err != nil { + return nil, false, err + } + return &reports[0], true, nil } From 3ea844d18bbcc3407a5b5f87a2e593794814a13d Mon Sep 17 00:00:00 2001 From: Kevin KADOSH Date: Thu, 4 Dec 2025 17:24:31 +0100 Subject: [PATCH 2/2] remove log debug --- pkg/database/database.go | 2 +- pkg/database/report.go | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/database/database.go b/pkg/database/database.go index 2c5030d..88bb754 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -13,7 +13,7 @@ type Client struct { func NewClient(sqliteDBPath string) (*Client, error) { client := &Client{} - db, err := gorm.Open(sqlite.Open(sqliteDBPath), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)}) + db, err := gorm.Open(sqlite.Open(sqliteDBPath), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) if err != nil { return client, err } diff --git a/pkg/database/report.go b/pkg/database/report.go index 177f91a..4b882ba 100644 --- a/pkg/database/report.go +++ b/pkg/database/report.go @@ -74,7 +74,6 @@ func (r *ReportClient) FindById(reportID uint) (*Report, error) { return nil, result.Error } - // Load IPs using Association API err := r.db.Model(&report).Association("IPs").Find(&report.IPs) if err != nil { return nil, err @@ -99,7 +98,6 @@ func (r *ReportClient) FindByHash(filepath string) (*Report, error) { return nil, result.Error } - // Load IPs using Association API err = r.db.Model(&report).Association("IPs").Find(&report.IPs) if err != nil { return nil, err @@ -193,7 +191,6 @@ func (r *ReportClient) FilePathExist(filePath string) (*Report, bool, error) { return nil, false, nil } - // Load IPs using Association API err := r.db.Model(&reports[0]).Association("IPs").Find(&reports[0].IPs) if err != nil { return nil, false, err