log_file_manager.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. package indexer
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "sort"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/0xJacky/Nginx-UI/model"
  12. "github.com/uozi-tech/cosy/logger"
  13. )
  14. // Legacy constants for backward compatibility - use IndexStatus enum in types.go instead
  15. // NginxLogCache represents a cached log entry from nginx configuration
  16. type NginxLogCache struct {
  17. Path string `json:"path"` // Path to the log file
  18. Type string `json:"type"` // Type of log: "access" or "error"
  19. Name string `json:"name"` // Name of the log file
  20. ConfigFile string `json:"config_file"` // Path to the configuration file that contains this log directive
  21. }
  22. // NginxLogWithIndex represents a log file with its index status information
  23. type NginxLogWithIndex struct {
  24. Path string `json:"path"` // Path to the log file
  25. Type string `json:"type"` // Type of log: "access" or "error"
  26. Name string `json:"name"` // Name of the log file
  27. ConfigFile string `json:"config_file"` // Path to the configuration file
  28. IndexStatus string `json:"index_status"` // Index status: indexed, indexing, not_indexed, queued, error
  29. LastModified int64 `json:"last_modified,omitempty"` // Unix timestamp of last modification time
  30. LastSize int64 `json:"last_size,omitempty"` // Last known size of the file
  31. LastIndexed int64 `json:"last_indexed,omitempty"` // Unix timestamp when the file was last indexed
  32. IndexStartTime int64 `json:"index_start_time,omitempty"` // Unix timestamp when the last indexing operation started
  33. IndexDuration int64 `json:"index_duration,omitempty"` // Duration of last indexing operation in milliseconds
  34. IsCompressed bool `json:"is_compressed"` // Whether the file is compressed
  35. HasTimeRange bool `json:"has_timerange"` // Whether time range is available
  36. TimeRangeStart int64 `json:"timerange_start,omitempty"` // Unix timestamp of start of time range in the log
  37. TimeRangeEnd int64 `json:"timerange_end,omitempty"` // Unix timestamp of end of time range in the log
  38. DocumentCount uint64 `json:"document_count,omitempty"` // Number of indexed documents from this file
  39. // Enhanced status tracking fields
  40. ErrorMessage string `json:"error_message,omitempty"` // Error message if indexing failed
  41. ErrorTime int64 `json:"error_time,omitempty"` // Unix timestamp when error occurred
  42. RetryCount int `json:"retry_count,omitempty"` // Number of retry attempts
  43. QueuePosition int `json:"queue_position,omitempty"` // Position in indexing queue
  44. }
  45. // LogFileManager manages nginx log file discovery and index status
  46. type LogFileManager struct {
  47. logCache map[string]*NginxLogCache
  48. cacheMutex sync.RWMutex
  49. persistence *PersistenceManager
  50. indexingStatus map[string]bool
  51. indexingMutex sync.RWMutex
  52. indexer *ParallelIndexer
  53. }
  54. // NewLogFileManager creates a new log file manager
  55. func NewLogFileManager() *LogFileManager {
  56. return &LogFileManager{
  57. logCache: make(map[string]*NginxLogCache),
  58. persistence: NewPersistenceManager(DefaultIncrementalConfig()),
  59. indexingStatus: make(map[string]bool),
  60. }
  61. }
  62. // SetIndexer injects the running ParallelIndexer so we can query exact doc counts before persisting
  63. func (lm *LogFileManager) SetIndexer(pi *ParallelIndexer) {
  64. lm.indexer = pi
  65. }
  66. // AddLogPath adds a log path to the log cache with the source config file
  67. func (lm *LogFileManager) AddLogPath(path, logType, name, configFile string) {
  68. lm.cacheMutex.Lock()
  69. defer lm.cacheMutex.Unlock()
  70. lm.logCache[path] = &NginxLogCache{
  71. Path: path,
  72. Type: logType,
  73. Name: name,
  74. ConfigFile: configFile,
  75. }
  76. }
  77. // RemoveLogPathsFromConfig removes all log paths associated with a specific config file
  78. func (lm *LogFileManager) RemoveLogPathsFromConfig(configFile string) {
  79. lm.cacheMutex.Lock()
  80. defer lm.cacheMutex.Unlock()
  81. for path, logEntry := range lm.logCache {
  82. if logEntry.ConfigFile == configFile {
  83. delete(lm.logCache, path)
  84. }
  85. }
  86. }
  87. // GetAllLogPaths returns all cached log paths, optionally filtered
  88. func (lm *LogFileManager) GetAllLogPaths(filters ...func(*NginxLogCache) bool) []*NginxLogCache {
  89. lm.cacheMutex.RLock()
  90. defer lm.cacheMutex.RUnlock()
  91. var logs []*NginxLogCache
  92. for _, logEntry := range lm.logCache {
  93. // Apply all filters
  94. include := true
  95. for _, filter := range filters {
  96. if !filter(logEntry) {
  97. include = false
  98. break
  99. }
  100. }
  101. if include {
  102. // Create a copy to avoid race conditions
  103. logCopy := *logEntry
  104. logs = append(logs, &logCopy)
  105. }
  106. }
  107. return logs
  108. }
  109. // SetIndexingStatus sets the indexing status for a specific file path
  110. func (lm *LogFileManager) SetIndexingStatus(path string, isIndexing bool) {
  111. lm.indexingMutex.Lock()
  112. defer lm.indexingMutex.Unlock()
  113. if isIndexing {
  114. lm.indexingStatus[path] = true
  115. } else {
  116. delete(lm.indexingStatus, path)
  117. }
  118. }
  119. // GetIndexingFiles returns a list of files currently being indexed
  120. func (lm *LogFileManager) GetIndexingFiles() []string {
  121. lm.indexingMutex.RLock()
  122. defer lm.indexingMutex.RUnlock()
  123. var files []string
  124. for path := range lm.indexingStatus {
  125. files = append(files, path)
  126. }
  127. return files
  128. }
  129. // getBaseLogName determines the base log file name for grouping rotated files
  130. func getBaseLogName(filePath string) string {
  131. dir := filepath.Dir(filePath)
  132. filename := filepath.Base(filePath)
  133. // Remove compression extensions first
  134. filename = strings.TrimSuffix(filename, ".gz")
  135. filename = strings.TrimSuffix(filename, ".bz2")
  136. // Handle numbered rotation (access.log.1, access.log.2, etc.)
  137. if match := regexp.MustCompile(`^(.+)\.(\d+)$`).FindStringSubmatch(filename); len(match) > 1 {
  138. baseFilename := match[1]
  139. return filepath.Join(dir, baseFilename)
  140. }
  141. // Handle date rotation suffixes
  142. parts := strings.Split(filename, ".")
  143. if len(parts) >= 2 {
  144. lastPart := parts[len(parts)-1]
  145. if isDatePattern(lastPart) {
  146. baseFilename := strings.Join(parts[:len(parts)-1], ".")
  147. // If the base doesn't end with .log, add it
  148. if !strings.HasSuffix(baseFilename, ".log") {
  149. baseFilename += ".log"
  150. }
  151. return filepath.Join(dir, baseFilename)
  152. }
  153. }
  154. // If it already looks like a base log file, return as-is
  155. return filePath
  156. }
  157. // GetAllLogsWithIndexGrouped returns logs grouped by their base name (e.g., access.log includes access.log.1, access.log.2.gz etc.)
  158. func (lm *LogFileManager) GetAllLogsWithIndexGrouped(filters ...func(*NginxLogWithIndex) bool) []*NginxLogWithIndex {
  159. lm.cacheMutex.RLock()
  160. defer lm.cacheMutex.RUnlock()
  161. // Get all logs from both cache (config files) and persistence (indexed files)
  162. allLogsMap := make(map[string]*NginxLogWithIndex)
  163. // First, get logs from the cache (these are from nginx config)
  164. for _, cache := range lm.logCache {
  165. logWithIndex := &NginxLogWithIndex{
  166. Path: cache.Path,
  167. Type: cache.Type,
  168. Name: cache.Name,
  169. ConfigFile: cache.ConfigFile,
  170. IndexStatus: string(IndexStatusNotIndexed),
  171. IsCompressed: false,
  172. HasTimeRange: false,
  173. }
  174. allLogsMap[cache.Path] = logWithIndex
  175. }
  176. // Get persistence indexes and update status
  177. persistenceIndexes, err := lm.persistence.GetAllLogIndexes()
  178. if err != nil {
  179. logger.Warnf("Failed to get persistence indexes: %v", err)
  180. persistenceIndexes = []*model.NginxLogIndex{}
  181. }
  182. // Add all indexed files from persistence (including rotated files)
  183. for _, idx := range persistenceIndexes {
  184. if _, exists := allLogsMap[idx.Path]; !exists {
  185. // This is a rotated file not in config cache, create entry for it
  186. logType := "access"
  187. if strings.Contains(idx.Path, "error") {
  188. logType = "error"
  189. }
  190. logWithIndex := &NginxLogWithIndex{
  191. Path: idx.Path,
  192. Type: logType,
  193. Name: filepath.Base(idx.Path),
  194. ConfigFile: "",
  195. IndexStatus: string(IndexStatusNotIndexed),
  196. }
  197. allLogsMap[idx.Path] = logWithIndex
  198. }
  199. // Update index status from persistence data
  200. logWithIndex := allLogsMap[idx.Path]
  201. logWithIndex.LastModified = idx.LastModified.Unix()
  202. logWithIndex.LastSize = idx.LastSize
  203. logWithIndex.LastIndexed = idx.LastIndexed.Unix()
  204. if idx.IndexStartTime != nil {
  205. logWithIndex.IndexStartTime = idx.IndexStartTime.Unix()
  206. }
  207. if idx.IndexDuration != nil {
  208. logWithIndex.IndexDuration = *idx.IndexDuration
  209. }
  210. logWithIndex.DocumentCount = idx.DocumentCount
  211. // Set queue position if available
  212. logWithIndex.QueuePosition = idx.QueuePosition
  213. // Set error message if available
  214. logWithIndex.ErrorMessage = idx.ErrorMessage
  215. if idx.ErrorTime != nil {
  216. logWithIndex.ErrorTime = idx.ErrorTime.Unix()
  217. }
  218. logWithIndex.RetryCount = idx.RetryCount
  219. // Use the index status from the database if it's set
  220. if idx.IndexStatus != "" {
  221. logWithIndex.IndexStatus = idx.IndexStatus
  222. } else {
  223. // Fallback to determining status if not set in DB
  224. lm.indexingMutex.RLock()
  225. isIndexing := lm.indexingStatus[idx.Path]
  226. lm.indexingMutex.RUnlock()
  227. if isIndexing {
  228. logWithIndex.IndexStatus = string(IndexStatusIndexing)
  229. } else if !idx.LastIndexed.IsZero() {
  230. // If file has been indexed (regardless of document count), it's indexed
  231. logWithIndex.IndexStatus = string(IndexStatusIndexed)
  232. }
  233. }
  234. // Set time range if available
  235. if idx.TimeRangeStart != nil && idx.TimeRangeEnd != nil && !idx.TimeRangeStart.IsZero() && !idx.TimeRangeEnd.IsZero() {
  236. logWithIndex.HasTimeRange = true
  237. logWithIndex.TimeRangeStart = idx.TimeRangeStart.Unix()
  238. logWithIndex.TimeRangeEnd = idx.TimeRangeEnd.Unix()
  239. }
  240. logWithIndex.IsCompressed = strings.HasSuffix(idx.Path, ".gz") || strings.HasSuffix(idx.Path, ".bz2")
  241. }
  242. // Convert to slice and apply filters
  243. var logs []*NginxLogWithIndex
  244. for _, log := range allLogsMap {
  245. // Apply all filters
  246. include := true
  247. for _, filter := range filters {
  248. if !filter(log) {
  249. include = false
  250. break
  251. }
  252. }
  253. if include {
  254. logs = append(logs, log)
  255. }
  256. }
  257. // Group by base log name with stable aggregation
  258. groupedMap := make(map[string]*NginxLogWithIndex)
  259. // Sort logs by path first to ensure consistent processing order
  260. sort.Slice(logs, func(i, j int) bool {
  261. return logs[i].Path < logs[j].Path
  262. })
  263. for _, log := range logs {
  264. baseLogName := getBaseLogName(log.Path)
  265. if existing, exists := groupedMap[baseLogName]; exists {
  266. // Check if current log is a main log path record (already aggregated)
  267. // or if existing record is a main log path record
  268. logIsMainPath := (log.Path == baseLogName)
  269. existingIsMainPath := (existing.Path == baseLogName)
  270. if logIsMainPath && !existingIsMainPath {
  271. // Current log is the main aggregated record, replace existing
  272. groupedLog := *log
  273. groupedLog.Path = baseLogName
  274. groupedLog.Name = filepath.Base(baseLogName)
  275. groupedMap[baseLogName] = &groupedLog
  276. } else if !logIsMainPath && existingIsMainPath {
  277. // Existing is main record, keep it, don't accumulate
  278. // Only update status if needed
  279. if log.IndexStatus == string(IndexStatusIndexing) {
  280. existing.IndexStatus = string(IndexStatusIndexing)
  281. }
  282. } else if !logIsMainPath && !existingIsMainPath {
  283. // Both are individual files, accumulate normally
  284. if log.LastIndexed > existing.LastIndexed {
  285. existing.LastModified = log.LastModified
  286. existing.LastIndexed = log.LastIndexed
  287. existing.IndexStartTime = log.IndexStartTime
  288. existing.IndexDuration = log.IndexDuration
  289. }
  290. existing.DocumentCount += log.DocumentCount
  291. existing.LastSize += log.LastSize
  292. // Update status with priority: indexing > queued > indexed > error > not_indexed
  293. if log.IndexStatus == string(IndexStatusIndexing) {
  294. existing.IndexStatus = string(IndexStatusIndexing)
  295. } else if log.IndexStatus == string(IndexStatusQueued) &&
  296. existing.IndexStatus != string(IndexStatusIndexing) {
  297. existing.IndexStatus = string(IndexStatusQueued)
  298. // Keep the queue position from the queued log
  299. if log.QueuePosition > 0 {
  300. existing.QueuePosition = log.QueuePosition
  301. }
  302. } else if log.IndexStatus == string(IndexStatusIndexed) &&
  303. existing.IndexStatus != string(IndexStatusIndexing) &&
  304. existing.IndexStatus != string(IndexStatusQueued) {
  305. existing.IndexStatus = string(IndexStatusIndexed)
  306. } else if log.IndexStatus == string(IndexStatusError) &&
  307. existing.IndexStatus != string(IndexStatusIndexing) &&
  308. existing.IndexStatus != string(IndexStatusQueued) &&
  309. existing.IndexStatus != string(IndexStatusIndexed) {
  310. existing.IndexStatus = string(IndexStatusError)
  311. existing.ErrorMessage = log.ErrorMessage
  312. existing.ErrorTime = log.ErrorTime
  313. }
  314. if log.HasTimeRange {
  315. if !existing.HasTimeRange {
  316. existing.HasTimeRange = true
  317. existing.TimeRangeStart = log.TimeRangeStart
  318. existing.TimeRangeEnd = log.TimeRangeEnd
  319. } else {
  320. if log.TimeRangeStart > 0 && (existing.TimeRangeStart == 0 || log.TimeRangeStart < existing.TimeRangeStart) {
  321. existing.TimeRangeStart = log.TimeRangeStart
  322. }
  323. if log.TimeRangeEnd > existing.TimeRangeEnd {
  324. existing.TimeRangeEnd = log.TimeRangeEnd
  325. }
  326. }
  327. }
  328. } else if logIsMainPath && existingIsMainPath {
  329. // If both are main paths, use the one with more recent LastIndexed
  330. if log.LastIndexed > existing.LastIndexed {
  331. groupedLog := *log
  332. groupedLog.Path = baseLogName
  333. groupedLog.Name = filepath.Base(baseLogName)
  334. groupedMap[baseLogName] = &groupedLog
  335. }
  336. }
  337. } else {
  338. // Create new entry with base log name as path for grouping
  339. groupedLog := *log
  340. groupedLog.Path = baseLogName
  341. groupedLog.Name = filepath.Base(baseLogName)
  342. // Preserve queue position and error info for the grouped log
  343. groupedLog.QueuePosition = log.QueuePosition
  344. groupedLog.ErrorMessage = log.ErrorMessage
  345. groupedLog.ErrorTime = log.ErrorTime
  346. groupedLog.RetryCount = log.RetryCount
  347. groupedMap[baseLogName] = &groupedLog
  348. }
  349. }
  350. // Convert map to slice with consistent ordering
  351. var result []*NginxLogWithIndex
  352. // Create a sorted list of keys to ensure consistent order
  353. var keys []string
  354. for key := range groupedMap {
  355. keys = append(keys, key)
  356. }
  357. sort.Strings(keys)
  358. // Build result in consistent order
  359. for _, key := range keys {
  360. result = append(result, groupedMap[key])
  361. }
  362. // --- START DIAGNOSTIC LOGGING ---
  363. logger.Debugf("===== FINAL GROUPED LIST =====")
  364. for _, fLog := range result {
  365. logger.Debugf("Final Group: Path=%s, DocCount=%d, Status=%s", fLog.Path, fLog.DocumentCount, fLog.IndexStatus)
  366. }
  367. logger.Debugf("===============================")
  368. // --- END DIAGNOSTIC LOGGING ---
  369. return result
  370. }
  371. // SaveIndexMetadata saves the metadata for a log group after an indexing operation.
  372. // It creates a new record for the base log path.
  373. func (lm *LogFileManager) SaveIndexMetadata(basePath string, documentCount uint64, startTime time.Time, duration time.Duration, minTime *time.Time, maxTime *time.Time) error {
  374. // We want to save the metadata against the base path (the "log group").
  375. // We get or create a record for this specific path.
  376. logIndex, err := lm.persistence.GetLogIndex(basePath)
  377. if err != nil {
  378. // If the error is anything other than "not found", it's a real problem.
  379. // GetLogIndex is designed to return a new object if not found, so this should be rare.
  380. return fmt.Errorf("could not get or create log index for '%s': %w", basePath, err)
  381. }
  382. // Get file stats to update LastModified and LastSize
  383. if fileInfo, err := os.Stat(basePath); err == nil {
  384. logIndex.LastModified = fileInfo.ModTime()
  385. logIndex.LastSize = fileInfo.Size()
  386. }
  387. // If indexer is available and healthy, query Bleve for exact document count
  388. if lm.indexer != nil && lm.indexer.IsHealthy() {
  389. // Decide whether this path is a main log path (group) or a specific file
  390. mainPath := getMainLogPathFromFile(basePath)
  391. if mainPath == basePath {
  392. if exact, err := lm.indexer.CountDocsByMainLogPath(basePath); err == nil {
  393. documentCount = exact
  394. } else {
  395. logger.Warnf("Falling back to provided documentCount for group %s due to count error: %v", basePath, err)
  396. }
  397. } else {
  398. if exact, err := lm.indexer.CountDocsByFilePath(basePath); err == nil {
  399. documentCount = exact
  400. } else {
  401. logger.Warnf("Falling back to provided documentCount for file %s due to count error: %v", basePath, err)
  402. }
  403. }
  404. }
  405. // Update the record with the (possibly corrected) metadata
  406. logIndex.DocumentCount = documentCount
  407. logIndex.LastIndexed = time.Now()
  408. logIndex.IndexStartTime = &startTime
  409. durationMs := duration.Milliseconds()
  410. logIndex.IndexDuration = &durationMs
  411. // Merge time ranges: preserve existing historical range and expand if necessary
  412. // This prevents incremental indexing from losing historical time range data
  413. if minTime != nil {
  414. if logIndex.TimeRangeStart == nil || minTime.Before(*logIndex.TimeRangeStart) {
  415. logIndex.TimeRangeStart = minTime
  416. }
  417. }
  418. if maxTime != nil {
  419. if logIndex.TimeRangeEnd == nil || maxTime.After(*logIndex.TimeRangeEnd) {
  420. logIndex.TimeRangeEnd = maxTime
  421. }
  422. }
  423. // Save the updated record to the database
  424. return lm.persistence.SaveLogIndex(logIndex)
  425. }
  426. // DeleteIndexMetadataByGroup deletes all database records for a given log group.
  427. func (lm *LogFileManager) DeleteIndexMetadataByGroup(basePath string) error {
  428. // The basePath is the main log path for the group.
  429. return lm.persistence.DeleteLogIndexesByGroup(basePath)
  430. }
  431. // DeleteAllIndexMetadata deletes all index metadata from the database.
  432. func (lm *LogFileManager) DeleteAllIndexMetadata() error {
  433. return lm.persistence.DeleteAllLogIndexes()
  434. }
  435. // GetLogByPath returns the full NginxLogWithIndex struct for a given base path.
  436. func (lm *LogFileManager) GetLogByPath(basePath string) (*NginxLogWithIndex, error) {
  437. // This is not the most efficient way, but it's reliable.
  438. // It ensures we get the same grouped and aggregated data the UI sees.
  439. allLogs := lm.GetAllLogsWithIndexGrouped()
  440. for _, log := range allLogs {
  441. if log.Path == basePath {
  442. return log, nil
  443. }
  444. }
  445. return nil, fmt.Errorf("log group with base path not found: %s", basePath)
  446. }
  447. // GetFilePathsForGroup returns all physical file paths for a given log group base path.
  448. func (lm *LogFileManager) GetFilePathsForGroup(basePath string) ([]string, error) {
  449. // Query the database for all log indexes with matching main_log_path
  450. logIndexes, err := lm.persistence.GetLogIndexesByGroup(basePath)
  451. if err != nil {
  452. return nil, fmt.Errorf("failed to get log indexes for group %s: %w", basePath, err)
  453. }
  454. // Extract file paths from the database records
  455. filePaths := make([]string, 0, len(logIndexes))
  456. for _, logIndex := range logIndexes {
  457. filePaths = append(filePaths, logIndex.Path)
  458. }
  459. return filePaths, nil
  460. }
  461. // GetPersistence returns the persistence manager for advanced operations
  462. func (lm *LogFileManager) GetPersistence() *PersistenceManager {
  463. return lm.persistence
  464. }
  465. // GetAllLogsWithIndex returns all cached log paths with their index status (non-grouped)
  466. func (lm *LogFileManager) GetAllLogsWithIndex(filters ...func(*NginxLogWithIndex) bool) []*NginxLogWithIndex {
  467. lm.cacheMutex.RLock()
  468. defer lm.cacheMutex.RUnlock()
  469. result := make([]*NginxLogWithIndex, 0, len(lm.logCache))
  470. // Get persistence indexes
  471. persistenceIndexes, err := lm.persistence.GetAllLogIndexes()
  472. if err != nil {
  473. logger.Warnf("Failed to get persistence indexes: %v", err)
  474. persistenceIndexes = []*model.NginxLogIndex{}
  475. }
  476. // Create a map of persistence indexes for quick lookup
  477. persistenceMap := make(map[string]*model.NginxLogIndex)
  478. for _, idx := range persistenceIndexes {
  479. persistenceMap[idx.Path] = idx
  480. }
  481. // Process cached logs (from nginx config)
  482. for _, cache := range lm.logCache {
  483. logWithIndex := &NginxLogWithIndex{
  484. Path: cache.Path,
  485. Type: cache.Type,
  486. Name: cache.Name,
  487. ConfigFile: cache.ConfigFile,
  488. IndexStatus: string(IndexStatusNotIndexed),
  489. IsCompressed: strings.HasSuffix(cache.Path, ".gz") || strings.HasSuffix(cache.Path, ".bz2"),
  490. }
  491. // Update with persistence data if available
  492. if idx, exists := persistenceMap[cache.Path]; exists {
  493. logWithIndex.LastModified = idx.LastModified.Unix()
  494. logWithIndex.LastSize = idx.LastSize
  495. logWithIndex.LastIndexed = idx.LastIndexed.Unix()
  496. if idx.IndexStartTime != nil {
  497. logWithIndex.IndexStartTime = idx.IndexStartTime.Unix()
  498. }
  499. if idx.IndexDuration != nil {
  500. logWithIndex.IndexDuration = *idx.IndexDuration
  501. }
  502. logWithIndex.DocumentCount = idx.DocumentCount
  503. // Determine status
  504. lm.indexingMutex.RLock()
  505. isIndexing := lm.indexingStatus[cache.Path]
  506. lm.indexingMutex.RUnlock()
  507. if isIndexing {
  508. logWithIndex.IndexStatus = string(IndexStatusIndexing)
  509. } else if !idx.LastIndexed.IsZero() {
  510. // If file has been indexed (regardless of document count), it's indexed
  511. logWithIndex.IndexStatus = string(IndexStatusIndexed)
  512. }
  513. // Set time range if available
  514. if idx.TimeRangeStart != nil && idx.TimeRangeEnd != nil && !idx.TimeRangeStart.IsZero() && !idx.TimeRangeEnd.IsZero() {
  515. logWithIndex.HasTimeRange = true
  516. logWithIndex.TimeRangeStart = idx.TimeRangeStart.Unix()
  517. logWithIndex.TimeRangeEnd = idx.TimeRangeEnd.Unix()
  518. }
  519. }
  520. // Apply filters
  521. include := true
  522. for _, filter := range filters {
  523. if !filter(logWithIndex) {
  524. include = false
  525. break
  526. }
  527. }
  528. if include {
  529. result = append(result, logWithIndex)
  530. }
  531. }
  532. return result
  533. }