log_list.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package nginx_log
  2. import (
  3. "slices"
  4. )
  5. // typeToInt converts log type string to a sortable integer
  6. // "access" = 0, "error" = 1
  7. func typeToInt(t string) int {
  8. if t == "access" {
  9. return 0
  10. }
  11. return 1
  12. }
  13. // sortCompare compares two log entries based on the specified key and order
  14. // Returns true if i should come after j in the sorted list
  15. func sortCompare(i, j *NginxLogCache, key string, order string) bool {
  16. flag := false
  17. switch key {
  18. case "type":
  19. flag = typeToInt(i.Type) > typeToInt(j.Type)
  20. default:
  21. fallthrough
  22. case "name":
  23. flag = i.Name > j.Name
  24. }
  25. if order == "asc" {
  26. flag = !flag
  27. }
  28. return flag
  29. }
  30. // Sort sorts a list of NginxLogCache entries by the specified key and order
  31. // Supported keys: "type", "name"
  32. // Supported orders: "asc", "desc"
  33. func Sort(key string, order string, configs []*NginxLogCache) []*NginxLogCache {
  34. slices.SortStableFunc(configs, func(i, j *NginxLogCache) int {
  35. if sortCompare(i, j, key, order) {
  36. return 1
  37. }
  38. return -1
  39. })
  40. return configs
  41. }
  42. // sortCompareWithIndex compares two log entries with index status based on the specified key and order
  43. // Returns true if i should come after j in the sorted list
  44. func sortCompareWithIndex(i, j *NginxLogWithIndex, key string, order string) bool {
  45. flag := false
  46. switch key {
  47. case "type":
  48. flag = typeToInt(i.Type) > typeToInt(j.Type)
  49. case "index_status":
  50. // Sort order: indexed > indexing > not_indexed
  51. statusOrder := map[string]int{
  52. IndexStatusIndexed: 3,
  53. IndexStatusIndexing: 2,
  54. IndexStatusNotIndexed: 1,
  55. }
  56. iOrder := statusOrder[i.IndexStatus]
  57. jOrder := statusOrder[j.IndexStatus]
  58. flag = iOrder < jOrder
  59. case "last_indexed":
  60. // Sort by last indexed time (more recent first)
  61. if i.LastIndexed != 0 && j.LastIndexed != 0 {
  62. flag = i.LastIndexed > j.LastIndexed
  63. } else if i.LastIndexed == 0 && j.LastIndexed != 0 {
  64. flag = true // 0 comes after non-zero
  65. } else if i.LastIndexed != 0 && j.LastIndexed == 0 {
  66. flag = false // non-zero comes before 0
  67. }
  68. case "last_size":
  69. // Sort by file size
  70. if i.LastSize != 0 && j.LastSize != 0 {
  71. flag = i.LastSize > j.LastSize
  72. } else if i.LastSize == 0 && j.LastSize != 0 {
  73. flag = true
  74. } else if i.LastSize != 0 && j.LastSize == 0 {
  75. flag = false
  76. }
  77. case "document_count":
  78. // Sort by document count
  79. if i.DocumentCount != 0 && j.DocumentCount != 0 {
  80. flag = i.DocumentCount > j.DocumentCount
  81. } else if i.DocumentCount == 0 && j.DocumentCount != 0 {
  82. flag = true
  83. } else if i.DocumentCount != 0 && j.DocumentCount == 0 {
  84. flag = false
  85. }
  86. default:
  87. fallthrough
  88. case "name":
  89. flag = i.Name > j.Name
  90. }
  91. if order == "asc" {
  92. flag = !flag
  93. }
  94. return flag
  95. }
  96. // SortWithIndex sorts a list of NginxLogWithIndex entries by the specified key and order
  97. // Supported keys: "type", "name", "is_indexed", "last_indexed", "last_size", "document_count"
  98. // Supported orders: "asc", "desc"
  99. func SortWithIndex(key string, order string, configs []*NginxLogWithIndex) []*NginxLogWithIndex {
  100. slices.SortStableFunc(configs, func(i, j *NginxLogWithIndex) int {
  101. if sortCompareWithIndex(i, j, key, order) {
  102. return 1
  103. }
  104. return -1
  105. })
  106. return configs
  107. }