progress_tracker.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package nginx_log
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/0xJacky/Nginx-UI/internal/event"
  6. "github.com/uozi-tech/cosy/logger"
  7. )
  8. // ProgressTracker manages progress tracking for log group indexing
  9. type ProgressTracker struct {
  10. mu sync.RWMutex
  11. logGroupPath string
  12. startTime int64 // Unix timestamp
  13. files map[string]*FileProgress
  14. totalEstimate int64 // Total estimated lines across all files
  15. totalActual int64 // Total actual lines processed
  16. isCompleted bool
  17. completionNotified bool // Flag to prevent duplicate completion notifications
  18. lastNotify int64 // Unix timestamp
  19. }
  20. // FileProgress tracks progress for individual files
  21. type FileProgress struct {
  22. FilePath string
  23. State FileState
  24. EstimatedLines int64 // Estimated total lines in this file
  25. ProcessedLines int64 // Actually processed lines
  26. FileSize int64 // Total file size in bytes (compressed size for .gz files)
  27. CurrentPos int64 // Current reading position in bytes (for uncompressed files only)
  28. AvgLineSize int64 // Dynamic average line size in bytes (for compressed files)
  29. SampleCount int64 // Number of lines sampled for average calculation
  30. IsCompressed bool
  31. StartTime int64 // Unix timestamp
  32. CompletedTime int64 // Unix timestamp
  33. }
  34. // FileState represents the current state of file processing
  35. type FileState int
  36. const (
  37. FileStatePending FileState = iota
  38. FileStateProcessing
  39. FileStateCompleted
  40. )
  41. func (fs FileState) String() string {
  42. switch fs {
  43. case FileStatePending:
  44. return "pending"
  45. case FileStateProcessing:
  46. return "processing"
  47. case FileStateCompleted:
  48. return "completed"
  49. default:
  50. return "unknown"
  51. }
  52. }
  53. // NewProgressTracker creates a new progress tracker for a log group
  54. func NewProgressTracker(logGroupPath string) *ProgressTracker {
  55. return &ProgressTracker{
  56. logGroupPath: logGroupPath,
  57. startTime: time.Now().Unix(),
  58. files: make(map[string]*FileProgress),
  59. completionNotified: false,
  60. }
  61. }
  62. // AddFile adds a file to the progress tracker
  63. func (pt *ProgressTracker) AddFile(filePath string, isCompressed bool) {
  64. pt.mu.Lock()
  65. defer pt.mu.Unlock()
  66. pt.files[filePath] = &FileProgress{
  67. FilePath: filePath,
  68. State: FileStatePending,
  69. IsCompressed: isCompressed,
  70. AvgLineSize: 120, // Initial estimate: 120 bytes per line
  71. SampleCount: 0,
  72. }
  73. logger.Debugf("Added file to progress tracker: %s (compressed: %v)", filePath, isCompressed)
  74. }
  75. // SetFileEstimate sets the estimated line count for a file
  76. func (pt *ProgressTracker) SetFileEstimate(filePath string, estimatedLines int64) {
  77. pt.mu.Lock()
  78. defer pt.mu.Unlock()
  79. if progress, exists := pt.files[filePath]; exists {
  80. oldEstimate := progress.EstimatedLines
  81. progress.EstimatedLines = estimatedLines
  82. // Update total estimate
  83. pt.totalEstimate = pt.totalEstimate - oldEstimate + estimatedLines
  84. logger.Debugf("Updated file estimate for %s: %d lines (total estimate: %d)",
  85. filePath, estimatedLines, pt.totalEstimate)
  86. }
  87. }
  88. // SetFileSize sets the file size for a file
  89. func (pt *ProgressTracker) SetFileSize(filePath string, fileSize int64) {
  90. pt.mu.Lock()
  91. defer pt.mu.Unlock()
  92. if progress, exists := pt.files[filePath]; exists {
  93. progress.FileSize = fileSize
  94. logger.Debugf("Set file size for %s: %d bytes", filePath, fileSize)
  95. }
  96. }
  97. // UpdateFilePosition updates the current reading position for files
  98. func (pt *ProgressTracker) UpdateFilePosition(filePath string, currentPos int64, linesProcessed int64) {
  99. pt.mu.Lock()
  100. defer pt.mu.Unlock()
  101. if progress, exists := pt.files[filePath]; exists {
  102. progress.ProcessedLines = linesProcessed
  103. if progress.IsCompressed {
  104. // For compressed files, update average line size dynamically
  105. if linesProcessed > 0 {
  106. // Use the first 1000 lines to establish a good average, then update less frequently
  107. if progress.SampleCount < 1000 || progress.SampleCount%100 == 0 {
  108. // Calculate current average line size based on processed data
  109. // For compressed files, we estimate based on processed lines and compression ratio
  110. estimatedUncompressedBytes := progress.FileSize * 3 // Assume 3:1 compression ratio
  111. newAvgLineSize := estimatedUncompressedBytes / linesProcessed
  112. if newAvgLineSize > 50 && newAvgLineSize < 5000 { // Sanity check: 50-5000 bytes per line
  113. // Smooth the average to avoid sudden jumps
  114. if progress.SampleCount > 0 {
  115. progress.AvgLineSize = (progress.AvgLineSize + newAvgLineSize) / 2
  116. } else {
  117. progress.AvgLineSize = newAvgLineSize
  118. }
  119. }
  120. }
  121. progress.SampleCount = linesProcessed
  122. }
  123. } else {
  124. // For uncompressed files, update current position
  125. progress.CurrentPos = currentPos
  126. }
  127. }
  128. }
  129. // StartFile marks a file as started processing
  130. func (pt *ProgressTracker) StartFile(filePath string) {
  131. pt.mu.Lock()
  132. defer pt.mu.Unlock()
  133. if progress, exists := pt.files[filePath]; exists {
  134. progress.State = FileStateProcessing
  135. progress.StartTime = time.Now().Unix()
  136. logger.Debugf("Started processing file: %s", filePath)
  137. pt.notifyProgressLocked()
  138. }
  139. }
  140. // UpdateFileProgress updates the processed line count for a file
  141. func (pt *ProgressTracker) UpdateFileProgress(filePath string, processedLines int64) {
  142. pt.mu.Lock()
  143. defer pt.mu.Unlock()
  144. if progress, exists := pt.files[filePath]; exists {
  145. oldProcessed := progress.ProcessedLines
  146. progress.ProcessedLines = processedLines
  147. // Update total actual processed
  148. pt.totalActual = pt.totalActual - oldProcessed + processedLines
  149. // Notify progress if enough time has passed
  150. pt.notifyProgressLocked()
  151. }
  152. }
  153. // CompleteFile marks a file as completed
  154. func (pt *ProgressTracker) CompleteFile(filePath string, finalProcessedLines int64) {
  155. pt.mu.Lock()
  156. defer pt.mu.Unlock()
  157. if progress, exists := pt.files[filePath]; exists {
  158. oldProcessed := progress.ProcessedLines
  159. progress.ProcessedLines = finalProcessedLines
  160. progress.State = FileStateCompleted
  161. progress.CompletedTime = time.Now().Unix()
  162. // Update total actual processed
  163. pt.totalActual = pt.totalActual - oldProcessed + finalProcessedLines
  164. logger.Debugf("Completed processing file: %s (%d lines)", filePath, finalProcessedLines)
  165. // Check if all files are completed and we haven't notified yet
  166. if !pt.completionNotified {
  167. allCompleted := true
  168. for _, fp := range pt.files {
  169. if fp.State != FileStateCompleted {
  170. allCompleted = false
  171. break
  172. }
  173. }
  174. if allCompleted {
  175. pt.isCompleted = true
  176. pt.completionNotified = true // Mark as notified to prevent duplicates
  177. pt.notifyCompletionLocked()
  178. } else {
  179. pt.notifyProgressLocked()
  180. }
  181. }
  182. }
  183. }
  184. // GetProgress returns the current progress percentage and stats
  185. func (pt *ProgressTracker) GetProgress() (percentage float64, stats ProgressStats) {
  186. pt.mu.RLock()
  187. defer pt.mu.RUnlock()
  188. stats = ProgressStats{
  189. LogGroupPath: pt.logGroupPath,
  190. TotalFiles: len(pt.files),
  191. ProcessedLines: pt.totalActual,
  192. EstimatedLines: pt.totalEstimate,
  193. StartTime: pt.startTime,
  194. IsCompleted: pt.isCompleted,
  195. }
  196. // Count completed files
  197. for _, fp := range pt.files {
  198. switch fp.State {
  199. case FileStateCompleted:
  200. stats.CompletedFiles++
  201. case FileStateProcessing:
  202. stats.ProcessingFiles++
  203. }
  204. }
  205. // Calculate progress percentage
  206. if pt.totalEstimate > 0 {
  207. percentage = float64(pt.totalActual) / float64(pt.totalEstimate) * 100
  208. } else if stats.TotalFiles > 0 {
  209. // Fallback to file-based progress if no line estimates
  210. percentage = float64(stats.CompletedFiles) / float64(stats.TotalFiles) * 100
  211. }
  212. // Cap at 100%
  213. if percentage > 100 {
  214. percentage = 100
  215. }
  216. return percentage, stats
  217. }
  218. // ProgressStats contains progress statistics
  219. type ProgressStats struct {
  220. LogGroupPath string
  221. TotalFiles int
  222. CompletedFiles int
  223. ProcessingFiles int
  224. ProcessedLines int64
  225. EstimatedLines int64
  226. StartTime int64 // Unix timestamp
  227. IsCompleted bool
  228. }
  229. // notifyProgressLocked sends progress notification (must be called with lock held)
  230. func (pt *ProgressTracker) notifyProgressLocked() {
  231. // Throttle notifications to avoid spam
  232. now := time.Now().Unix()
  233. if now-pt.lastNotify < 2 {
  234. return
  235. }
  236. pt.lastNotify = now
  237. percentage, stats := pt.getProgressLocked()
  238. elapsed := (time.Now().Unix() - pt.startTime) * 1000 // Convert to milliseconds
  239. var estimatedRemain int64
  240. if percentage > 0 && percentage < 100 {
  241. avgTimePerPercent := float64(elapsed) / percentage
  242. remainingPercent := 100.0 - percentage
  243. estimatedRemain = int64(avgTimePerPercent * remainingPercent)
  244. }
  245. eventData := event.NginxLogIndexProgressData{
  246. LogPath: pt.logGroupPath,
  247. Progress: percentage,
  248. Stage: "indexing",
  249. Status: "running",
  250. ElapsedTime: elapsed,
  251. EstimatedRemain: estimatedRemain,
  252. }
  253. logger.Debugf("Progress update for %s: %.1f%% (%d/%d files, %d/%d lines)",
  254. pt.logGroupPath, percentage, stats.CompletedFiles, stats.TotalFiles,
  255. stats.ProcessedLines, stats.EstimatedLines)
  256. event.Publish(event.Event{
  257. Type: event.EventTypeNginxLogIndexProgress,
  258. Data: eventData,
  259. })
  260. }
  261. // notifyCompletionLocked sends completion notification (must be called with lock held)
  262. func (pt *ProgressTracker) notifyCompletionLocked() {
  263. elapsed := (time.Now().Unix() - pt.startTime) * 1000 // Convert to milliseconds
  264. // Calculate total size processed using improved estimation
  265. var totalSize int64
  266. for _, fp := range pt.files {
  267. if fp.IsCompressed {
  268. // For compressed files, use dynamic average line size
  269. totalSize += fp.ProcessedLines * fp.AvgLineSize
  270. } else {
  271. // For uncompressed files, use actual bytes processed if available, otherwise estimate
  272. if fp.CurrentPos > 0 {
  273. totalSize += fp.CurrentPos
  274. } else {
  275. // Fallback to line-based estimation with improved calculation
  276. totalSize += fp.ProcessedLines * 150
  277. }
  278. }
  279. }
  280. completeEventData := event.NginxLogIndexCompleteData{
  281. LogPath: pt.logGroupPath,
  282. Success: true,
  283. Duration: elapsed,
  284. TotalLines: pt.totalActual,
  285. IndexedSize: totalSize,
  286. Error: "",
  287. }
  288. event.Publish(event.Event{
  289. Type: event.EventTypeNginxLogIndexComplete,
  290. Data: completeEventData,
  291. })
  292. // Also publish index ready event for table refresh
  293. event.Publish(event.Event{
  294. Type: event.EventTypeNginxLogIndexReady,
  295. Data: map[string]interface{}{
  296. "log_path": pt.logGroupPath,
  297. "success": true,
  298. },
  299. })
  300. logger.Infof("Log group indexing completed for %s: %d files, %d lines processed in %dms (SINGLE NOTIFICATION)",
  301. pt.logGroupPath, len(pt.files), pt.totalActual, elapsed)
  302. }
  303. // getProgressLocked returns progress without notification (must be called with lock held)
  304. func (pt *ProgressTracker) getProgressLocked() (float64, ProgressStats) {
  305. stats := ProgressStats{
  306. LogGroupPath: pt.logGroupPath,
  307. TotalFiles: len(pt.files),
  308. ProcessedLines: pt.totalActual,
  309. EstimatedLines: pt.totalEstimate,
  310. StartTime: pt.startTime,
  311. IsCompleted: pt.isCompleted,
  312. }
  313. // Count completed files
  314. for _, fp := range pt.files {
  315. switch fp.State {
  316. case FileStateCompleted:
  317. stats.CompletedFiles++
  318. case FileStateProcessing:
  319. stats.ProcessingFiles++
  320. }
  321. }
  322. // Calculate progress percentage
  323. var percentage float64
  324. if pt.totalEstimate > 0 {
  325. percentage = float64(pt.totalActual) / float64(pt.totalEstimate) * 100
  326. } else if stats.TotalFiles > 0 {
  327. // Fallback to file-based progress if no line estimates
  328. percentage = float64(stats.CompletedFiles) / float64(stats.TotalFiles) * 100
  329. }
  330. // Cap at 100%
  331. if percentage > 100 {
  332. percentage = 100
  333. }
  334. return percentage, stats
  335. }
  336. // GlobalProgressManager manages all progress trackers
  337. type GlobalProgressManager struct {
  338. mu sync.RWMutex
  339. trackers map[string]*ProgressTracker
  340. }
  341. var globalProgressManager = &GlobalProgressManager{
  342. trackers: make(map[string]*ProgressTracker),
  343. }
  344. // GetProgressTracker gets or creates a progress tracker for a log group
  345. func GetProgressTracker(logGroupPath string) *ProgressTracker {
  346. globalProgressManager.mu.Lock()
  347. defer globalProgressManager.mu.Unlock()
  348. if tracker, exists := globalProgressManager.trackers[logGroupPath]; exists {
  349. return tracker
  350. }
  351. tracker := NewProgressTracker(logGroupPath)
  352. globalProgressManager.trackers[logGroupPath] = tracker
  353. return tracker
  354. }
  355. // RemoveProgressTracker removes a progress tracker (called when indexing is complete)
  356. func RemoveProgressTracker(logGroupPath string) {
  357. globalProgressManager.mu.Lock()
  358. defer globalProgressManager.mu.Unlock()
  359. delete(globalProgressManager.trackers, logGroupPath)
  360. logger.Debugf("Removed progress tracker for log group: %s", logGroupPath)
  361. }
  362. // EstimateFileLines estimates the number of lines in a file based on sampling
  363. func EstimateFileLines(filePath string, fileSize int64, isCompressed bool) int64 {
  364. if isCompressed {
  365. // For compressed files, estimate based on compression ratio and average line size
  366. // Assume 3:1 compression ratio and 100 bytes average per line
  367. estimatedUncompressedSize := fileSize * 3
  368. return estimatedUncompressedSize / 100
  369. }
  370. // For uncompressed files, assume average 100 bytes per line
  371. if fileSize == 0 {
  372. return 0
  373. }
  374. return fileSize / 100 // Rough estimate
  375. }