Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Panels filter check for where clause #253

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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