From 023c65968fa29130cd6b101ce270e3bb9f41daf7 Mon Sep 17 00:00:00 2001 From: Yury Molodov Date: Thu, 23 Mar 2023 13:10:59 +0100 Subject: [PATCH] vmui: display errors for each query individually (#3987) (#3994) --- .../Chart/Legend/LegendItem/LegendItem.tsx | 9 +++++--- .../src/components/Main/TextField/style.scss | 2 ++ .../packages/vmui/src/hooks/useFetchQuery.ts | 23 ++++++++----------- .../QueryConfigurator/QueryConfigurator.tsx | 6 ++--- .../vmui/src/pages/CustomPanel/index.tsx | 6 +++-- docs/CHANGELOG.md | 1 + 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app/vmui/packages/vmui/src/components/Chart/Legend/LegendItem/LegendItem.tsx b/app/vmui/packages/vmui/src/components/Chart/Legend/LegendItem/LegendItem.tsx index ca5d45a1c3..813f5933c3 100644 --- a/app/vmui/packages/vmui/src/components/Chart/Legend/LegendItem/LegendItem.tsx +++ b/app/vmui/packages/vmui/src/components/Chart/Legend/LegendItem/LegendItem.tsx @@ -15,6 +15,7 @@ const LegendItem: FC = ({ legend, onChange }) => { const [copiedValue, setCopiedValue] = useState(""); const freeFormFields = useMemo(() => getFreeFields(legend), [legend]); const calculations = legend.calculations; + const showCalculations = Object.values(calculations).some(v => v); const handleClickFreeField = async (val: string, id: string) => { await navigator.clipboard.writeText(val); @@ -68,9 +69,11 @@ const LegendItem: FC = ({ legend, onChange }) => { } -
- median:{calculations.median}, min:{calculations.min}, max:{calculations.max}, last:{calculations.last} -
+ {showCalculations && ( +
+ median:{calculations.median}, min:{calculations.min}, max:{calculations.max}, last:{calculations.last} +
+ )} ); }; diff --git a/app/vmui/packages/vmui/src/components/Main/TextField/style.scss b/app/vmui/packages/vmui/src/components/Main/TextField/style.scss index 9e70bcc7ce..ec6942caad 100644 --- a/app/vmui/packages/vmui/src/components/Main/TextField/style.scss +++ b/app/vmui/packages/vmui/src/components/Main/TextField/style.scss @@ -58,6 +58,8 @@ &__error { top: calc((100% - ($font-size-small/2)) - 2px); color: $color-error; + pointer-events: auto; + user-select: text; } &__helper-text { diff --git a/app/vmui/packages/vmui/src/hooks/useFetchQuery.ts b/app/vmui/packages/vmui/src/hooks/useFetchQuery.ts index 6384fed848..72e3cad6c4 100644 --- a/app/vmui/packages/vmui/src/hooks/useFetchQuery.ts +++ b/app/vmui/packages/vmui/src/hooks/useFetchQuery.ts @@ -26,6 +26,7 @@ interface FetchQueryReturn { graphData?: MetricResult[], liveData?: InstantMetricResult[], error?: ErrorTypes | string, + queryErrors: (ErrorTypes | string)[], warning?: string, traces?: Trace[], } @@ -58,17 +59,10 @@ export const useFetchQuery = ({ const [liveData, setLiveData] = useState(); const [traces, setTraces] = useState(); const [error, setError] = useState(); + const [queryErrors, setQueryErrors] = useState<(ErrorTypes | string)[]>([]); const [warning, setWarning] = useState(); const [fetchQueue, setFetchQueue] = useState([]); - useEffect(() => { - if (error) { - setGraphData(undefined); - setLiveData(undefined); - setTraces(undefined); - } - }, [error]); - const fetchData = async ({ fetchUrl, fetchQueue, @@ -100,7 +94,7 @@ export const useFetchQuery = ({ const resp = await response.json(); if (response.ok) { - setError(undefined); + setQueryErrors(prev => [...prev, ""]); if (resp.trace) { const trace = new Trace(resp.trace, query[counter - 1]); @@ -114,10 +108,11 @@ export const useFetchQuery = ({ }); totalLength += resp.data.result.length; - counter++; } else { - setError(`${resp.errorType}\r\n${resp?.error}`); + tempData.push({ metric: {}, values: [], group: counter } as MetricBase); + setQueryErrors(prev => [...prev, `${resp.errorType}\r\n${resp?.error}`]); } + counter++; } const limitText = `Showing ${seriesLimit} series out of ${totalLength} series due to performance reasons. Please narrow down the query, so it returns less series`; @@ -136,13 +131,15 @@ export const useFetchQuery = ({ const throttledFetchData = useCallback(debounce(fetchData, 300), []); const fetchUrl = useMemo(() => { + setError(""); + setQueryErrors([]); const expr = predefinedQuery ?? query; const displayChart = (display || displayType) === "chart"; if (!period) return; if (!serverUrl) { setError(ErrorTypes.emptyServer); } else if (expr.every(q => !q.trim())) { - setError(ErrorTypes.validQuery); + setQueryErrors(expr.map(() => ErrorTypes.validQuery)); } else if (isValidHttpUrl(serverUrl)) { const updatedPeriod = { ...period }; updatedPeriod.step = customStep; @@ -181,5 +178,5 @@ export const useFetchQuery = ({ setFetchQueue(fetchQueue.filter(f => !f.signal.aborted)); }, [fetchQueue]); - return { fetchUrl, isLoading, graphData, liveData, error, warning, traces }; + return { fetchUrl, isLoading, graphData, liveData, error, queryErrors, warning, traces }; }; diff --git a/app/vmui/packages/vmui/src/pages/CustomPanel/QueryConfigurator/QueryConfigurator.tsx b/app/vmui/packages/vmui/src/pages/CustomPanel/QueryConfigurator/QueryConfigurator.tsx index cf8ce0e6c6..57f16cd63e 100644 --- a/app/vmui/packages/vmui/src/pages/CustomPanel/QueryConfigurator/QueryConfigurator.tsx +++ b/app/vmui/packages/vmui/src/pages/CustomPanel/QueryConfigurator/QueryConfigurator.tsx @@ -16,14 +16,14 @@ import { arrayEquals } from "../../../utils/array"; import useDeviceDetect from "../../../hooks/useDeviceDetect"; export interface QueryConfiguratorProps { - error?: ErrorTypes | string; + errors: (ErrorTypes | string)[]; queryOptions: string[] onHideQuery: (queries: number[]) => void onRunQuery: () => void } const QueryConfigurator: FC = ({ - error, + errors, queryOptions, onHideQuery, onRunQuery @@ -141,7 +141,7 @@ const QueryConfigurator: FC = ({ value={stateQuery[i]} autocomplete={autocomplete} options={queryOptions} - error={error} + error={errors[i]} onArrowUp={createHandlerArrow(-1, i)} onArrowDown={createHandlerArrow(1, i)} onEnter={handleRunQuery} diff --git a/app/vmui/packages/vmui/src/pages/CustomPanel/index.tsx b/app/vmui/packages/vmui/src/pages/CustomPanel/index.tsx index 32a94b28c9..efaf1a9886 100644 --- a/app/vmui/packages/vmui/src/pages/CustomPanel/index.tsx +++ b/app/vmui/packages/vmui/src/pages/CustomPanel/index.tsx @@ -41,7 +41,9 @@ const CustomPanel: FC = () => { const graphDispatch = useGraphDispatch(); const { queryOptions } = useFetchQueryOptions(); - const { isLoading, liveData, graphData, error, warning, traces } = useFetchQuery({ + const { + isLoading, liveData, graphData, error, queryErrors, warning, traces + } = useFetchQuery({ visible: true, customStep, hideQuery, @@ -99,7 +101,7 @@ const CustomPanel: FC = () => { })} >