From d9793e15e7463cf2840f25ad0a98f5fb015f1aa0 Mon Sep 17 00:00:00 2001 From: Shenoy Pratik Date: Mon, 6 Feb 2023 15:50:18 -0800 Subject: [PATCH] Panels filter check for where clause (#251) * where cluase filter Signed-off-by: Shenoy Pratik * remove eslintcache Signed-off-by: Shenoy Pratik * changing const to let Signed-off-by: Shenoy Pratik --------- Signed-off-by: Shenoy Pratik --- common/constants/shared.ts | 1 + .../custom_panels/helpers/utils.tsx | 28 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/common/constants/shared.ts b/common/constants/shared.ts index f5e193038..f87dbf8c1 100644 --- a/common/constants/shared.ts +++ b/common/constants/shared.ts @@ -40,6 +40,7 @@ export const PPL_SPAN_REGEX = /by\s*span/i; export const PPL_STATS_REGEX = /\|\s*stats/i; export const PPL_INDEX_INSERT_POINT_REGEX = /(search source|source|index)\s*=\s*([^|\s]+)(.*)/i; export const PPL_INDEX_REGEX = /(search source|source|index)\s*=\s*([^|\s]+)/i; +export const PPL_WHERE_CLAUSE_REGEX = /\s*where\s+/i; export const PPL_NEWLINE_REGEX = /[\n\r]+/g; // Observability plugin URI diff --git a/public/components/custom_panels/helpers/utils.tsx b/public/components/custom_panels/helpers/utils.tsx index 71bfa22c5..6cf49cc1d 100644 --- a/public/components/custom_panels/helpers/utils.tsx +++ b/public/components/custom_panels/helpers/utils.tsx @@ -11,7 +11,11 @@ import _, { isEmpty } from 'lodash'; import { Moment } from 'moment-timezone'; import React from 'react'; import { Layout } from 'react-grid-layout'; -import { PPL_DATE_FORMAT, PPL_INDEX_REGEX } from '../../../../common/constants/shared'; +import { + PPL_DATE_FORMAT, + PPL_INDEX_REGEX, + PPL_WHERE_CLAUSE_REGEX, +} from '../../../../common/constants/shared'; import PPLService from '../../../services/requests/ppl'; import { CoreStart } from '../../../../../../src/core/public'; import { CUSTOM_PANELS_API_PREFIX } from '../../../../common/constants/custom_panels'; @@ -64,7 +68,7 @@ export const mergeLayoutAndVisualizations = ( for (let i = 0; i < newVisualizationList.length; i++) { for (let j = 0; j < layout.length; j++) { - if (newVisualizationList[i].id == layout[j].i) { + if (newVisualizationList[i].id === layout[j].i) { newPanelVisualizations.push({ ...newVisualizationList[i], x: layout[j].x, @@ -293,7 +297,7 @@ const createCatalogVisualizationMetaData = ( }; }; -//Creates a catalogVisualization for a runtime catalog based PPL query and runs getQueryResponse +// Creates a catalogVisualization for a runtime catalog based PPL query and runs getQueryResponse export const renderCatalogVisualization = async ( http: CoreStart['http'], pplService: PPLService, @@ -359,14 +363,15 @@ export const onTimeChange = ( setStart: React.Dispatch>, setEnd: React.Dispatch> ) => { - const recentlyUsedRange = recentlyUsedRanges.filter((recentlyUsedRange) => { + let recentlyUsedRangeObject = recentlyUsedRanges.filter((recentlyUsedRange) => { const isDuplicate = recentlyUsedRange.start === start && recentlyUsedRange.end === end; return !isDuplicate; }); - recentlyUsedRange.unshift({ start, end }); + + recentlyUsedRangeObject.unshift({ start, end }); setStart(start); setEnd(end); - setRecentlyUsedRanges(recentlyUsedRange.slice(0, 9)); + setRecentlyUsedRanges(recentlyUsedRangeObject.slice(0, 9)); }; // Function to check date validity @@ -392,6 +397,11 @@ const checkIndexExists = (query: string) => { return PPL_INDEX_REGEX.test(query); }; +// Check if the filter query starts with a where clause +const checkWhereClauseExists = (query: string) => { + return PPL_WHERE_CLAUSE_REGEX.test(query); +}; + // Check PPL Query in Panel UI // Validate if the query doesn't contain any Index export const isPPLFilterValid = ( @@ -407,6 +417,10 @@ export const isPPLFilterValid = ( setToast('Please remove index from PPL Filter', 'danger', undefined); return false; } + if (!checkWhereClauseExists(query)) { + setToast('PPL filters should start with a where clause', 'danger', undefined); + return false; + } return true; }; @@ -421,7 +435,7 @@ export const displayVisualization = (metaData: any, data: any, type: string) => ...getDefaultVisConfig(new QueryManager().queryParser().parse(metaData.query).getStats()), }; let finalDimensions = [...(realTimeParsedStats.dimensions || [])]; - let breakdowns = [...(dataConfig.breakdowns || [])]; + const breakdowns = [...(dataConfig.breakdowns || [])]; // filter out breakdowns from dimnesions if (hasBreakdowns) {