refactor: cache index

This commit is contained in:
Jacky 2025-04-04 02:00:18 +00:00
parent 5d8d96fd4f
commit 269397e114
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
20 changed files with 532 additions and 364 deletions

View file

@ -2,10 +2,10 @@ package nginx_log
import (
"slices"
"github.com/0xJacky/Nginx-UI/internal/cache"
)
// typeToInt converts log type string to a sortable integer
// "access" = 0, "error" = 1
func typeToInt(t string) int {
if t == "access" {
return 0
@ -13,7 +13,9 @@ func typeToInt(t string) int {
return 1
}
func sortCompare(i, j *cache.NginxLogCache, key string, order string) bool {
// sortCompare compares two log entries based on the specified key and order
// Returns true if i should come after j in the sorted list
func sortCompare(i, j *NginxLogCache, key string, order string) bool {
flag := false
switch key {
@ -32,8 +34,11 @@ func sortCompare(i, j *cache.NginxLogCache, key string, order string) bool {
return flag
}
func Sort(key string, order string, configs []*cache.NginxLogCache) []*cache.NginxLogCache {
slices.SortStableFunc(configs, func(i, j *cache.NginxLogCache) int {
// Sort sorts a list of NginxLogCache entries by the specified key and order
// Supported keys: "type", "name"
// Supported orders: "asc", "desc"
func Sort(key string, order string, configs []*NginxLogCache) []*NginxLogCache {
slices.SortStableFunc(configs, func(i, j *NginxLogCache) int {
if sortCompare(i, j, key, order) {
return 1
}