1
0

log_list.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package nginx_log
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx_log"
  6. "github.com/gin-gonic/gin"
  7. "github.com/uozi-tech/cosy/logger"
  8. )
  9. // GetLogList returns a list of Nginx log files with their index status
  10. func GetLogList(c *gin.Context) {
  11. var filters []func(*nginx_log.NginxLogWithIndex) bool
  12. if logType := c.Query("type"); logType != "" {
  13. filters = append(filters, func(entry *nginx_log.NginxLogWithIndex) bool {
  14. return entry.Type == logType
  15. })
  16. }
  17. if name := c.Query("name"); name != "" {
  18. filters = append(filters, func(entry *nginx_log.NginxLogWithIndex) bool {
  19. return strings.Contains(entry.Name, name)
  20. })
  21. }
  22. if path := c.Query("path"); path != "" {
  23. filters = append(filters, func(entry *nginx_log.NginxLogWithIndex) bool {
  24. return strings.Contains(entry.Path, path)
  25. })
  26. }
  27. // Add filter for indexed status if requested
  28. if indexed := c.Query("indexed"); indexed != "" {
  29. filters = append(filters, func(entry *nginx_log.NginxLogWithIndex) bool {
  30. switch indexed {
  31. case "true":
  32. return entry.IndexStatus == nginx_log.IndexStatusIndexed
  33. case "false":
  34. return entry.IndexStatus == nginx_log.IndexStatusNotIndexed
  35. case "indexing":
  36. return entry.IndexStatus == nginx_log.IndexStatusIndexing
  37. default:
  38. return true
  39. }
  40. })
  41. }
  42. data := nginx_log.GetAllLogsWithIndexGrouped(filters...)
  43. orderBy := c.DefaultQuery("sort_by", "name")
  44. sort := c.DefaultQuery("order", "desc")
  45. data = nginx_log.SortWithIndex(orderBy, sort, data)
  46. // Calculate summary statistics
  47. totalCount := len(data)
  48. indexedCount := 0
  49. indexingCount := 0
  50. var totalDocuments uint64 = 0
  51. // Try to get total document count from modern indexer if available
  52. // The indexer is the source of truth for document counts.
  53. indexer := nginx_log.GetIndexer()
  54. if indexer != nil {
  55. stats := indexer.GetStats()
  56. if stats != nil {
  57. totalDocuments = stats.TotalDocuments
  58. logger.Debugf("Retrieved document count from indexer stats: %d", totalDocuments)
  59. }
  60. }
  61. for _, log := range data {
  62. switch log.IndexStatus {
  63. case nginx_log.IndexStatusIndexed:
  64. indexedCount++
  65. if totalDocuments == 0 {
  66. // Fallback to summing up document counts from the database records
  67. // This handles the case where the searcher might not be immediately up-to-date
  68. var dbCount uint64
  69. for _, l := range data {
  70. dbCount += l.DocumentCount
  71. }
  72. totalDocuments = dbCount
  73. }
  74. case nginx_log.IndexStatusIndexing:
  75. indexingCount++
  76. }
  77. }
  78. c.JSON(http.StatusOK, LogListResponse{
  79. Data: data,
  80. Summary: LogListSummary{
  81. TotalFiles: totalCount,
  82. IndexedFiles: indexedCount,
  83. IndexingFiles: indexingCount,
  84. DocumentCount: int(totalDocuments),
  85. },
  86. })
  87. }