1
0

log_list.go 707 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package nginx_log
  2. import (
  3. "slices"
  4. "github.com/0xJacky/Nginx-UI/internal/cache"
  5. )
  6. func typeToInt(t string) int {
  7. if t == "access" {
  8. return 0
  9. }
  10. return 1
  11. }
  12. func sortCompare(i, j *cache.NginxLogCache, key string, order string) bool {
  13. flag := false
  14. switch key {
  15. case "type":
  16. flag = typeToInt(i.Type) > typeToInt(j.Type)
  17. default:
  18. fallthrough
  19. case "name":
  20. flag = i.Name > j.Name
  21. }
  22. if order == "asc" {
  23. flag = !flag
  24. }
  25. return flag
  26. }
  27. func Sort(key string, order string, configs []*cache.NginxLogCache) []*cache.NginxLogCache {
  28. slices.SortStableFunc(configs, func(i, j *cache.NginxLogCache) int {
  29. if sortCompare(i, j, key, order) {
  30. return 1
  31. }
  32. return -1
  33. })
  34. return configs
  35. }