nginx_log_index.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package model
  2. import (
  3. "time"
  4. )
  5. // NginxLogIndex represents the incremental index position and metadata for a log file
  6. type NginxLogIndex struct {
  7. BaseModelUUID
  8. Path string `gorm:"uniqueIndex;size:500;not null" json:"path"` // Log file path
  9. MainLogPath string `gorm:"index;size:500" json:"main_log_path"` // Main log path for grouping related files (access.log for access.log.1, access.log.1.gz, etc.)
  10. LastModified time.Time `json:"last_modified"` // File last modified time when indexed
  11. LastSize int64 `gorm:"default:0" json:"last_size"` // Total index size of all related log files when last indexed
  12. LastPosition int64 `gorm:"default:0" json:"last_position"` // Last byte position indexed in file
  13. LastIndexed time.Time `json:"last_indexed"` // When file was last indexed
  14. IndexStartTime *time.Time `json:"index_start_time"` // When the last indexing operation started
  15. IndexDuration *int64 `json:"index_duration"` // Duration of last indexing operation in milliseconds
  16. TimeRangeStart *time.Time `json:"timerange_start"` // Earliest log entry time
  17. TimeRangeEnd *time.Time `json:"timerange_end"` // Latest log entry time
  18. DocumentCount uint64 `gorm:"default:0" json:"document_count"` // Total documents indexed from this file
  19. Enabled bool `gorm:"default:true" json:"enabled"` // Whether indexing is enabled for this file
  20. HasTimeRange bool `gorm:"-" json:"has_timerange"` // Whether a time range is available (not persisted)
  21. }
  22. // NeedsIndexing checks if the file needs incremental indexing
  23. func (nli *NginxLogIndex) NeedsIndexing(fileModTime time.Time, fileSize int64) bool {
  24. // If never indexed, needs full indexing
  25. if nli.LastIndexed.IsZero() {
  26. return true
  27. }
  28. // If file was modified after last index and size increased, needs incremental indexing
  29. if fileModTime.After(nli.LastModified) && fileSize > nli.LastSize {
  30. return true
  31. }
  32. // If file size decreased, file might have been rotated, needs full re-indexing
  33. if fileSize < nli.LastSize {
  34. return true
  35. }
  36. return false
  37. }
  38. // ShouldFullReindex checks if a full re-index is needed (file rotation detected)
  39. func (nli *NginxLogIndex) ShouldFullReindex(fileModTime time.Time, fileSize int64) bool {
  40. // File size decreased - likely file rotation
  41. if fileSize < nli.LastSize {
  42. return true
  43. }
  44. // File significantly older than last index - might be a replaced file
  45. if fileModTime.Before(nli.LastModified.Add(-time.Hour)) {
  46. return true
  47. }
  48. return false
  49. }
  50. // UpdateProgress updates the indexing progress
  51. func (nli *NginxLogIndex) UpdateProgress(modTime time.Time, size int64, position int64, docCount uint64, timeStart, timeEnd *time.Time) {
  52. nli.LastModified = modTime
  53. nli.LastSize = size
  54. nli.LastPosition = position
  55. nli.LastIndexed = time.Now()
  56. nli.DocumentCount = docCount
  57. if timeStart != nil {
  58. nli.TimeRangeStart = timeStart
  59. }
  60. if timeEnd != nil {
  61. nli.TimeRangeEnd = timeEnd
  62. }
  63. }
  64. // SetIndexStartTime records when indexing operation started
  65. func (nli *NginxLogIndex) SetIndexStartTime(startTime time.Time) {
  66. nli.IndexStartTime = &startTime
  67. }
  68. // SetIndexDuration records how long the indexing operation took
  69. func (nli *NginxLogIndex) SetIndexDuration(startTime time.Time) {
  70. // If IndexStartTime is not set, set it to the provided startTime
  71. if nli.IndexStartTime == nil {
  72. nli.IndexStartTime = &startTime
  73. }
  74. duration := time.Since(startTime).Milliseconds()
  75. nli.IndexDuration = &duration
  76. }
  77. // Reset clears all index position data for full re-indexing
  78. func (nli *NginxLogIndex) Reset() {
  79. nli.LastModified = time.Time{} // Clear last modified time
  80. nli.LastSize = 0 // Clear index size
  81. nli.LastPosition = 0
  82. nli.LastIndexed = time.Time{} // Clear last indexed time
  83. nli.IndexStartTime = nil // Clear index start time
  84. nli.IndexDuration = nil // Clear index duration
  85. nli.DocumentCount = 0
  86. nli.TimeRangeStart = nil
  87. nli.TimeRangeEnd = nil
  88. }