From b8a8d3d6f16624e9b41c67aad06636d852ac7d0c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 10 Jul 2024 00:39:16 +0200 Subject: [PATCH] lib/logstorage: drop all the pipes from the query when calculating the number of matching logs at /select/logsql/hits API --- app/vlselect/logsql/logsql.go | 5 +++-- docs/VictoriaLogs/CHANGELOG.md | 2 ++ lib/logstorage/parser.go | 5 +++++ lib/logstorage/parser_test.go | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/vlselect/logsql/logsql.go b/app/vlselect/logsql/logsql.go index b4920e3b32..03e886f70a 100644 --- a/app/vlselect/logsql/logsql.go +++ b/app/vlselect/logsql/logsql.go @@ -70,9 +70,10 @@ func ProcessHitsRequest(ctx context.Context, w http.ResponseWriter, r *http.Requ fieldsLimit = 0 } - // Prepare the query - q.AddCountByTimePipe(int64(step), int64(offset), fields) + // Prepare the query for hits count. q.Optimize() + q.DropAllPipes() + q.AddCountByTimePipe(int64(step), int64(offset), fields) var mLock sync.Mutex m := make(map[string]*hitsSeries) diff --git a/docs/VictoriaLogs/CHANGELOG.md b/docs/VictoriaLogs/CHANGELOG.md index c50a36bd51..9f793fed22 100644 --- a/docs/VictoriaLogs/CHANGELOG.md +++ b/docs/VictoriaLogs/CHANGELOG.md @@ -20,6 +20,8 @@ according to [these docs](https://docs.victoriametrics.com/victorialogs/quicksta ## tip * FEATURE: [web UI](https://docs.victoriametrics.com/victorialogs/querying/#web-ui): show a spinner on top of bar chart until user's request is finished. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6558). +* FEATURE: [web UI](https://docs.victoriametrics.com/victorialogs/querying/#web-ui): use compact representation of JSON lines at `JSON` tab if only a single [log field](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model) is queried. +* FEATURE: [web UI](https://docs.victoriametrics.com/victorialogs/querying/#web-ui): properly show the number of matching logs on the selected time range at bar chart for queries with arbitrary [pipes](https://docs.victoriametrics.com/victorialogs/logsql/#pipes), including [`stats` pipe](https://docs.victoriametrics.com/victorialogs/logsql/#stats-pipe) and [`top` pipe](https://docs.victoriametrics.com/victorialogs/logsql/#top-pipe). ## [v0.27.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v0.27.1-victorialogs) diff --git a/lib/logstorage/parser.go b/lib/logstorage/parser.go index 7fffea3363..45f59768f7 100644 --- a/lib/logstorage/parser.go +++ b/lib/logstorage/parser.go @@ -281,6 +281,11 @@ func getStreamIDsFromFilterOr(f filter) ([]streamID, bool) { } } +// DropAllPipes drops all the pipes from q. +func (q *Query) DropAllPipes() { + q.pipes = nil +} + // AddCountByTimePipe adds '| stats by (_time:step offset off, field1, ..., fieldN) count() hits' to the end of q. func (q *Query) AddCountByTimePipe(step, off int64, fields []string) { { diff --git a/lib/logstorage/parser_test.go b/lib/logstorage/parser_test.go index 1d3abf15b6..6956ba6e7d 100644 --- a/lib/logstorage/parser_test.go +++ b/lib/logstorage/parser_test.go @@ -2078,3 +2078,25 @@ func TestQueryCanLiveTail(t *testing.T) { f("* | unpack_syslog", true) f("* | unroll by (a)", true) } + +func TestQueryDropAllPipes(t *testing.T) { + f := func(qStr, resultExpected string) { + t.Helper() + + q, err := ParseQuery(qStr) + if err != nil { + t.Fatalf("cannot parse [%s]: %s", qStr, err) + } + q.Optimize() + q.DropAllPipes() + result := q.String() + if result != resultExpected { + t.Fatalf("unexpected result\ngot\n%s\nwant\n%s", result, resultExpected) + } + } + + f(`*`, `*`) + f(`foo | stats count()`, `foo`) + f(`foo or bar and baz | top 5 by (x)`, `foo or bar baz`) + f(`foo | filter bar:baz | stats by (x) min(y)`, `foo bar:baz`) +}