2020-05-26 15:37:37 +03:00
|
|
|
package querylog
|
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log/slog"
|
|
|
|
"time"
|
|
|
|
)
|
2020-05-26 15:37:37 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// searchParams represent the search query sent by the client.
|
2020-05-26 15:37:37 +03:00
|
|
|
type searchParams struct {
|
2023-05-24 16:33:15 +03:00
|
|
|
// olderThen represents a parameter for entries that are older than this
|
|
|
|
// parameter value. If not set, disregard it and return any value.
|
|
|
|
olderThan time.Time
|
|
|
|
|
|
|
|
// searchCriteria is a list of search criteria that we use to get filter
|
|
|
|
// results.
|
2021-04-12 18:22:11 +03:00
|
|
|
searchCriteria []searchCriterion
|
2020-05-26 15:37:37 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// offset for the search.
|
|
|
|
offset int
|
|
|
|
|
|
|
|
// limit the number of records returned.
|
|
|
|
limit int
|
2020-05-26 15:37:37 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// maxFileScanEntries is a maximum of log entries to scan in query log
|
|
|
|
// files. If not set, then no limit.
|
|
|
|
maxFileScanEntries int
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newSearchParams - creates an empty instance of searchParams
|
|
|
|
func newSearchParams() *searchParams {
|
|
|
|
return &searchParams{
|
|
|
|
// default max log entries to return
|
|
|
|
limit: 500,
|
|
|
|
|
|
|
|
// by default, we scan up to 50k entries at once
|
|
|
|
maxFileScanEntries: 50000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:22:11 +03:00
|
|
|
// quickMatchClientFunc is a simplified client finder for quick matches.
|
2024-11-21 20:19:39 +03:00
|
|
|
type quickMatchClientFunc = func(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *slog.Logger,
|
|
|
|
clientID, ip string,
|
|
|
|
) (c *Client)
|
2021-04-12 18:22:11 +03:00
|
|
|
|
|
|
|
// quickMatch quickly checks if the line matches the given search parameters.
|
|
|
|
// It returns false if the line doesn't match. This method is only here for
|
2021-12-27 19:40:39 +03:00
|
|
|
// optimization purposes.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (s *searchParams) quickMatch(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *slog.Logger,
|
|
|
|
line string,
|
|
|
|
findClient quickMatchClientFunc,
|
|
|
|
) (ok bool) {
|
2021-04-12 18:22:11 +03:00
|
|
|
for _, c := range s.searchCriteria {
|
2024-11-21 20:19:39 +03:00
|
|
|
if !c.quickMatch(ctx, logger, line, findClient) {
|
2021-04-12 18:22:11 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
// match - checks if the logEntry matches the searchParams
|
|
|
|
func (s *searchParams) match(entry *logEntry) bool {
|
2021-05-17 17:48:35 +03:00
|
|
|
if !s.olderThan.IsZero() && !entry.Time.Before(s.olderThan) {
|
2020-05-26 15:37:37 +03:00
|
|
|
// Ignore entries newer than what was requested
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range s.searchCriteria {
|
|
|
|
if !c.match(entry) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|