Skip to content

Commit

Permalink
Panels filter check for where clause (#251) (#253)
Browse files Browse the repository at this point in the history
* where cluase filter

Signed-off-by: Shenoy Pratik <[email protected]>

* remove eslintcache

Signed-off-by: Shenoy Pratik <[email protected]>

* changing const to let

Signed-off-by: Shenoy Pratik <[email protected]>

---------

Signed-off-by: Shenoy Pratik <[email protected]>
(cherry picked from commit d9793e1)

Co-authored-by: Shenoy Pratik <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and ps48 authored Feb 7, 2023
1 parent 7205a79 commit a008f71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions common/constants/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions public/components/custom_panels/helpers/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import _ 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';
Expand Down Expand Up @@ -68,7 +72,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,
Expand Down Expand Up @@ -301,7 +305,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,
Expand Down Expand Up @@ -367,14 +371,15 @@ export const onTimeChange = (
setStart: React.Dispatch<React.SetStateAction<string>>,
setEnd: React.Dispatch<React.SetStateAction<string>>
) => {
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
Expand All @@ -400,6 +405,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 = (
Expand All @@ -415,6 +425,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;
};

Expand All @@ -429,7 +443,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) {
Expand Down

0 comments on commit a008f71

Please sign in to comment.