significantly improve log search performance
Signed-off-by: huanggze <loganhuang@yunify.com>
This commit is contained in:
32
pkg/utils/esutil/esutil.go
Normal file
32
pkg/utils/esutil/esutil.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// TODO: refactor
|
||||
package esutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// KubeSphere uses the layout `yyyy.MM.dd`.
|
||||
const layoutISO = "2006.01.02"
|
||||
|
||||
func ResolveIndexNames(prefix string, start, end time.Time) string {
|
||||
if end.IsZero() {
|
||||
end = time.Now()
|
||||
}
|
||||
|
||||
// In case of no start time or a broad query range over 30 days, search all indices.
|
||||
if start.IsZero() || end.Sub(start).Hours() > 24*30 {
|
||||
return fmt.Sprintf("%s*", prefix)
|
||||
}
|
||||
|
||||
var indices []string
|
||||
for i := 0; i <= int(end.Sub(start).Hours()/24); i++ {
|
||||
suffix := end.Add(time.Duration(-i) * 24 * time.Hour).Format(layoutISO)
|
||||
indices = append(indices, fmt.Sprintf("%s-%s", prefix, suffix))
|
||||
}
|
||||
|
||||
// If start is after end, ResolveIndexNames returns "".
|
||||
// However, query parameter validation will prevent it from happening at the very beginning (Bad Request).
|
||||
return strings.Join(indices, ",")
|
||||
}
|
||||
Reference in New Issue
Block a user