Skip to content

Commit

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

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

* remove eslintcache

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

* changing const to let

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

---------

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
  • Loading branch information
ps48 committed Feb 6, 2023
1 parent 5532041 commit d9793e1
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 _, { 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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -359,14 +363,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 @@ -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 = (
Expand All @@ -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;
};

Expand All @@ -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) {
Expand Down

0 comments on commit d9793e1

Please sign in to comment.