@@ -7,10 +7,16 @@ import (
77
88"github.com/0xJacky/Nginx-UI/internal/nginx_log"
99"github.com/0xJacky/Nginx-UI/internal/nginx_log/indexer"
10+ "github.com/0xJacky/Nginx-UI/model"
1011"github.com/go-co-op/gocron/v2"
1112"github.com/uozi-tech/cosy/logger"
1213)
1314
15+ // logIndexProvider provides access to stored per-file index metadata.
16+ type logIndexProvider interface {
17+ GetLogIndex (path string ) (* model.NginxLogIndex , error )
18+ }
19+
1420// setupIncrementalIndexingJob sets up the periodic incremental log indexing job
1521func setupIncrementalIndexingJob (s gocron.Scheduler ) (gocron.Job , error ) {
1622logger .Info ("Setting up incremental log indexing job" )
@@ -42,6 +48,12 @@ func performIncrementalIndexing() {
4248return
4349}
4450
51+ persistence := logFileManager .GetPersistence ()
52+ if persistence == nil {
53+ logger .Warn ("Persistence manager not available for incremental indexing" )
54+ return
55+ }
56+
4557// Get modern indexer
4658modernIndexer := nginx_log .GetIndexer ()
4759if modernIndexer == nil {
@@ -64,7 +76,7 @@ func performIncrementalIndexing() {
6476changedCount := 0
6577for _ , log := range allLogs {
6678// Check if file needs incremental indexing
67- if needsIncrementalIndexing (log ) {
79+ if needsIncrementalIndexing (log , persistence ) {
6880if err := queueIncrementalIndexing (log .Path , modernIndexer , logFileManager ); err != nil {
6981logger .Errorf ("Failed to queue incremental indexing for %s: %v" , log .Path , err )
7082} else {
@@ -81,7 +93,7 @@ func performIncrementalIndexing() {
8193}
8294
8395// needsIncrementalIndexing checks if a log file needs incremental indexing
84- func needsIncrementalIndexing (log * nginx_log.NginxLogWithIndex ) bool {
96+ func needsIncrementalIndexing (log * nginx_log.NginxLogWithIndex , persistence logIndexProvider ) bool {
8597// Skip if already indexing or queued
8698if log .IndexStatus == string (indexer .IndexStatusIndexing ) ||
8799log .IndexStatus == string (indexer .IndexStatusQueued ) {
@@ -99,22 +111,43 @@ func needsIncrementalIndexing(log *nginx_log.NginxLogWithIndex) bool {
99111return false
100112}
101113
102- // Check if file has been modified since last index
103114fileModTime := fileInfo .ModTime ()
104115fileSize := fileInfo .Size ()
116+
117+ if persistence != nil {
118+ if logIndex , err := persistence .GetLogIndex (log .Path ); err == nil {
119+ if logIndex .NeedsIndexing (fileModTime , fileSize ) {
120+ logger .Debugf ("File %s needs incremental indexing based on persisted metadata" , log .Path )
121+ return true
122+ }
123+ return false
124+ } else {
125+ logger .Debugf ("Could not load persisted metadata for %s: %v" , log .Path , err )
126+ }
127+ }
128+
129+ // Fallback: use aggregated data cautiously by clamping the stored size so grouped entries
130+ // do not trigger false positives when rotation files are aggregated together.
105131lastModified := time .Unix (log .LastModified , 0 )
132+ lastSize := log .LastSize
133+ if lastSize == 0 || lastSize > fileSize {
134+ lastSize = fileSize
135+ }
136+
137+ // If the file was never indexed, queue it once.
138+ if log .LastIndexed == 0 {
139+ return true
140+ }
106141
107- // File was modified after last index and size increased
108- if fileModTime .After (lastModified ) && fileSize > log .LastSize {
109- logger .Debugf ("File %s needs incremental indexing: mod_time=%s, size=%d" ,
142+ if fileModTime .After (lastModified ) && fileSize > lastSize {
143+ logger .Debugf ("File %s needs incremental indexing (fallback path): mod_time=%s, size=%d" ,
110144log .Path , fileModTime .Format ("2006-01-02 15:04:05" ), fileSize )
111145return true
112146}
113147
114- // File size decreased - might be file rotation
115- if fileSize < log .LastSize {
116- logger .Debugf ("File %s needs full re-indexing due to size decrease: old_size=%d, new_size=%d" ,
117- log .Path , log .LastSize , fileSize )
148+ if fileSize < lastSize {
149+ logger .Debugf ("File %s needs full re-indexing (fallback path) due to size decrease: old_size=%d, new_size=%d" ,
150+ log .Path , lastSize , fileSize )
118151return true
119152}
120153
0 commit comments