From 26e981ced2d9140195421d54e663a832e51611da Mon Sep 17 00:00:00 2001 From: Yury Molodov Date: Thu, 4 Apr 2024 00:48:37 +0200 Subject: [PATCH] vmui: fix trigger auto-suggestion (#6033) * vmui: fix ui freeze on query paste #5923 * vmui: fix auto-suggestion trigger issue after whitespace char #5866 --------- Co-authored-by: Aliaksandr Valialkin --- .../QueryEditor/QueryEditorAutocomplete.tsx | 22 +++++++++++++++---- app/vmui/packages/vmui/src/utils/regexp.ts | 5 +++++ docs/CHANGELOG.md | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/vmui/packages/vmui/src/components/Configurators/QueryEditor/QueryEditorAutocomplete.tsx b/app/vmui/packages/vmui/src/components/Configurators/QueryEditor/QueryEditorAutocomplete.tsx index a642598ec..edbd85d35 100644 --- a/app/vmui/packages/vmui/src/components/Configurators/QueryEditor/QueryEditorAutocomplete.tsx +++ b/app/vmui/packages/vmui/src/components/Configurators/QueryEditor/QueryEditorAutocomplete.tsx @@ -2,7 +2,7 @@ import React, { FC, Ref, useState, useEffect, useMemo } from "preact/compat"; import Autocomplete, { AutocompleteOptions } from "../../Main/Autocomplete/Autocomplete"; import { useFetchQueryOptions } from "../../../hooks/useFetchQueryOptions"; import { getTextWidth } from "../../../utils/uplot"; -import { escapeRegexp } from "../../../utils/regexp"; +import { escapeRegexp, hasUnclosedQuotes } from "../../../utils/regexp"; import useGetMetricsQL from "../../../hooks/useGetMetricsQL"; import { QueryContextType } from "../../../types"; import { AUTOCOMPLETE_LIMITS } from "../../../constants/queryAutocomplete"; @@ -42,10 +42,24 @@ const QueryEditorAutocomplete: FC = ({ return match ? match[match.length - 1] : ""; }, [exprLastPart]); - const context = useMemo(() => { - if (!value || value.endsWith("}")) return QueryContextType.empty; + const shouldSuppressAutoSuggestion = (value: string) => { + const pattern = /([(),+\-*/^]|\b(?:or|and|unless|default|ifnot|if|group_left|group_right)\b)/; + const parts = value.split(/\s+/); + const partsCount = parts.length; + const lastPart = parts[partsCount - 1]; + const preLastPart = parts[partsCount - 2]; - const labelRegexp = /\{[^}]*?(\w+)*$/gm; + const hasEmptyPartAndQuotes = !lastPart && hasUnclosedQuotes(value); + const suppressPreLast = (!lastPart || parts.length > 1) && !pattern.test(preLastPart); + return hasEmptyPartAndQuotes || suppressPreLast; + }; + + const context = useMemo(() => { + if (!value || value.endsWith("}") || shouldSuppressAutoSuggestion(value)) { + return QueryContextType.empty; + } + + const labelRegexp = /\{[^}]*$/; const labelValueRegexp = new RegExp(`(${escapeRegexp(metric)})?{?.+${escapeRegexp(label)}(=|!=|=~|!~)"?([^"]*)$`, "g"); switch (true) { diff --git a/app/vmui/packages/vmui/src/utils/regexp.ts b/app/vmui/packages/vmui/src/utils/regexp.ts index 19836626e..1322914ab 100644 --- a/app/vmui/packages/vmui/src/utils/regexp.ts +++ b/app/vmui/packages/vmui/src/utils/regexp.ts @@ -6,3 +6,8 @@ export const escapeRegexp = (s: string) => { export const escapeDoubleQuotes = (s: string) => { return JSON.stringify(s).slice(1,-1); }; + +export const hasUnclosedQuotes = (str: string) => { + const matches = str.match(/"/g); + return matches ? matches.length % 2 !== 0 : false; +}; diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 07c0091c2..0d0cadbee 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -72,6 +72,9 @@ See also [LTS releases](https://docs.victoriametrics.com/lts-releases/). * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): return proper responses for [AWS Firehose](https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html#requestformat) requests according to [these docs](https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html#responseformat). See [this pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6016) and [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6037). * BUGFIX: [vmctl](https://docs.victoriametrics.com/vmctl.html): properly parse TLS key and CA files for [InfluxDB](https://docs.victoriametrics.com/vmctl/#migrating-data-from-influxdb-1x) and [OpenTSDB](https://docs.victoriametrics.com/vmctl/#migrating-data-from-opentsdb) migration modes. * BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix VictoriaLogs UI query handling to correctly apply `_time` filter across all queries. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5920). +* BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix freezing when pasting a query with autocomplete enabled. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5923). +* BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix auto-suggestion trigger issue after whitespace char. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5866). +* BUGFIX: [vmselect](https://docs.victoriametrics.com/): make vmselect resilient to absence of cache folder. If cache folder was mistakenly deleted by user or OS, vmselect will try re-creating it first. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5985). * BUGFIX: [Single-node VictoriaMetrics](https://docs.victoriametrics.com/) and `vmselect` in [VictoriaMetrics cluster](https://docs.victoriametrics.com/cluster-victoriametrics/): limit duration of requests to /api/v1/labels, /api/v1/label/.../values or /api/v1/series with `-search.maxLabelsAPIDuration` duration. Before, `-search.maxExportDuration` value was used by mistake. The bug has been introduced in [v1.99.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.99.0). Thanks to @kbweave for the [pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5992). * BUGFIX: properly wait for force merge to be completed during the shutdown. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5944) for the details. * BUGFIX: [vmalert](https://docs.victoriametrics.com/vmalert.html): set correct `endsAt` value in notifications sent to the Alertmanager. Previously, a rule with evaluation intervals lower than 10s could never be triggered. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5995) for details.