1
0

log_cache_index.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package nginx_log
  2. import (
  3. "github.com/0xJacky/Nginx-UI/model"
  4. "github.com/uozi-tech/cosy/logger"
  5. )
  6. // GetAllLogsWithIndex returns all cached log paths with their index status
  7. func GetAllLogsWithIndex(filters ...func(*NginxLogWithIndex) bool) []*NginxLogWithIndex {
  8. cacheMutex.RLock()
  9. defer cacheMutex.RUnlock()
  10. result := make([]*NginxLogWithIndex, 0, len(logCache))
  11. // Get persistence manager for database index records
  12. persistence := NewPersistenceManager()
  13. persistenceIndexes, err := persistence.GetAllLogIndexes()
  14. if err != nil {
  15. logger.Warnf("Failed to get persistence indexes: %v", err)
  16. persistenceIndexes = []*model.NginxLogIndex{}
  17. }
  18. // Create a map of persistence indexes for quick lookup
  19. persistenceMap := make(map[string]*model.NginxLogIndex)
  20. for _, idx := range persistenceIndexes {
  21. persistenceMap[idx.Path] = idx
  22. }
  23. // Get analytics service for index status
  24. service := GetAnalyticsService()
  25. var indexStatus *IndexStatus
  26. if service != nil {
  27. status, err := service.GetIndexStatus()
  28. if err == nil {
  29. indexStatus = status
  30. }
  31. }
  32. // Create a map of indexed files for quick lookup
  33. indexedFiles := make(map[string]*FileStatus)
  34. if indexStatus != nil && indexStatus.Files != nil {
  35. for i := range indexStatus.Files {
  36. file := &indexStatus.Files[i]
  37. indexedFiles[file.Path] = file
  38. }
  39. }
  40. // Convert each log cache entry to log with index
  41. for _, cache := range logCache {
  42. logWithIndex := &NginxLogWithIndex{
  43. Path: cache.Path,
  44. Type: cache.Type,
  45. Name: cache.Name,
  46. ConfigFile: cache.ConfigFile,
  47. IndexStatus: IndexStatusNotIndexed,
  48. IsCompressed: false,
  49. HasTimeRange: false,
  50. }
  51. // Check if this file is currently being indexed
  52. if IsFileIndexing(cache.Path) {
  53. logWithIndex.IndexStatus = IndexStatusIndexing
  54. }
  55. // Check persistence data first (more accurate)
  56. if persistenceIndex, ok := persistenceMap[cache.Path]; ok {
  57. // Set status based on persistence and current indexing state
  58. if logWithIndex.IndexStatus != IndexStatusIndexing {
  59. if !persistenceIndex.LastIndexed.IsZero() {
  60. logWithIndex.IndexStatus = IndexStatusIndexed
  61. }
  62. }
  63. // Use persistence data
  64. if !persistenceIndex.LastModified.IsZero() {
  65. logWithIndex.LastModified = persistenceIndex.LastModified.Unix()
  66. }
  67. logWithIndex.LastSize = persistenceIndex.LastSize
  68. if !persistenceIndex.LastIndexed.IsZero() {
  69. logWithIndex.LastIndexed = persistenceIndex.LastIndexed.Unix()
  70. }
  71. if persistenceIndex.IndexStartTime != nil {
  72. logWithIndex.IndexStartTime = persistenceIndex.IndexStartTime.Unix()
  73. }
  74. if persistenceIndex.IndexDuration != nil {
  75. logWithIndex.IndexDuration = *persistenceIndex.IndexDuration
  76. }
  77. if persistenceIndex.TimeRangeStart != nil {
  78. logWithIndex.TimeRangeStart = persistenceIndex.TimeRangeStart.Unix()
  79. logWithIndex.HasTimeRange = true
  80. }
  81. if persistenceIndex.TimeRangeEnd != nil {
  82. logWithIndex.TimeRangeEnd = persistenceIndex.TimeRangeEnd.Unix()
  83. logWithIndex.HasTimeRange = true
  84. }
  85. logWithIndex.DocumentCount = persistenceIndex.DocumentCount
  86. } else if fileStatus, ok := indexedFiles[cache.Path]; ok {
  87. // Fallback to old index status system
  88. if logWithIndex.IndexStatus != IndexStatusIndexing {
  89. logWithIndex.IndexStatus = IndexStatusIndexed
  90. }
  91. if fileStatus.LastModified != 0 {
  92. logWithIndex.LastModified = fileStatus.LastModified
  93. }
  94. logWithIndex.LastSize = fileStatus.LastSize
  95. if fileStatus.LastIndexed != 0 {
  96. logWithIndex.LastIndexed = fileStatus.LastIndexed
  97. }
  98. logWithIndex.IsCompressed = fileStatus.IsCompressed
  99. logWithIndex.HasTimeRange = fileStatus.HasTimeRange
  100. if fileStatus.TimeRangeStart != 0 {
  101. logWithIndex.TimeRangeStart = fileStatus.TimeRangeStart
  102. }
  103. if fileStatus.TimeRangeEnd != 0 {
  104. logWithIndex.TimeRangeEnd = fileStatus.TimeRangeEnd
  105. }
  106. }
  107. // Apply filters
  108. flag := true
  109. if len(filters) > 0 {
  110. for _, filter := range filters {
  111. if !filter(logWithIndex) {
  112. flag = false
  113. break
  114. }
  115. }
  116. }
  117. if flag {
  118. result = append(result, logWithIndex)
  119. }
  120. }
  121. return result
  122. }