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

refactor: Update alerting DSL verify mechanism #359

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- feat: take index pattern and query assistant input to text2viz app([#349](https://github.com/opensearch-project/dashboards-assistant/pull/349))
- feat: Hide incompatible index patterns ([#354] (https://github.com/opensearch-project/dashboards-assistant/pull/354))
- feat: edit visualization with natural language in a dialog([#351](https://github.com/opensearch-project/dashboards-assistant/pull/351))
- fix: Update alerting DSL verify mechanism([#359](https://github.com/opensearch-project/dashboards-assistant/pull/359))


### 📈 Features/Enhancements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks';

jest.mock('../../services');

jest.mock('../../utils', () => ({
createIndexPatterns: jest.fn().mockResolvedValue('index pattern'),
buildUrlQuery: jest.fn().mockResolvedValue('query'),
jest.mock('../../utils', () => {
const originUtils = jest.requireActual('../../utils');
return {
...originUtils,
createIndexPatterns: jest.fn().mockResolvedValue('index pattern'),
buildUrlQuery: jest.fn().mockResolvedValue('query'),
};
});

jest.spyOn(window, 'open').mockImplementation(() => null);

jest.mock('../../../../../src/core/public/utils', () => ({
...jest.requireActual('../../../../../src/core/public/utils'),
formatUrlWithWorkspaceId: jest.fn().mockReturnValue('formattedUrl'),
}));

const mockToasts = {
Expand Down Expand Up @@ -48,7 +59,7 @@ const mockDSL = `{
"range": {
"timestamp": {
"from": "2024-09-06T04:02:52||-1h",
"to": "2024-09-06T04:02:52",
"to": "2024-10-09T17:40:47+00:00",
"include_lower": true,
"include_upper": true,
"boost": 1
Expand Down Expand Up @@ -370,9 +381,7 @@ describe('GeneratePopoverBody', () => {
const button = getByText('Discover details');
expect(button).toBeInTheDocument();
fireEvent.click(button);
expect(coreStart.application.navigateToUrl).toHaveBeenCalledWith(
'data-explorer/discover#?query'
);
});
expect(window.open).toHaveBeenCalledWith('formattedUrl', '_blank');
});
});
40 changes: 28 additions & 12 deletions public/components/incontext_insight/generate_popover_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import { SUMMARY_ASSISTANT_API } from '../../../common/constants/llm';
import shiny_sparkle from '../../assets/shiny_sparkle.svg';
import { UsageCollectionSetup } from '../../../../../src/plugins/usage_collection/public';
import { reportMetric } from '../../utils/report_metric';
import { buildUrlQuery, createIndexPatterns } from '../../utils';
import { buildUrlQuery, createIndexPatterns, extractTimeRangeDSL } from '../../utils';
import { AssistantPluginStartDependencies } from '../../types';
import { UI_SETTINGS } from '../../../../../src/plugins/data/public';
import { formatUrlWithWorkspaceId } from '../../../../../src/core/public/utils';

export const GeneratePopoverBody: React.FC<{
incontextInsight: IncontextInsightInput;
Expand All @@ -55,10 +56,19 @@ export const GeneratePopoverBody: React.FC<{
const getMonitorType = async () => {
const context = await incontextInsight.contextProvider?.();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, could you store this context in a variable so we don't need to invoke this provider multiple times?(It got 3 invoking for now.) contextProvider for alerting is a heavy operation, it will execute a dsl and a ppl each time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let me see how to redesign this and do it in a separate PR in order not to have any influence on upcoming release.

const monitorType = context?.additionalInfo?.monitorType;
const dsl = context?.additionalInfo?.dsl;
// Only this two types from alerting contain DSL and index.
const shoudDisplayDiscoverButton =
const isSupportedMonitorType =
monitorType === 'query_level_monitor' || monitorType === 'bucket_level_monitor';
setDisplayDiscoverButton(shoudDisplayDiscoverButton);
let hasTimeRangeFilter = false;
if (dsl) {
const dslObject = JSON.parse(dsl);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add try catch for JSON.parse?

const filters = dslObject?.query?.bool?.filter;
// Filters contains time range filter,if no filters, return.
if (!filters?.length) return;
hasTimeRangeFilter = !!extractTimeRangeDSL(filters).timeRangeDSL;
}
setDisplayDiscoverButton(isSupportedMonitorType && hasTimeRangeFilter);
};
getMonitorType();
}, [incontextInsight, setDisplayDiscoverButton]);
Expand Down Expand Up @@ -195,12 +205,11 @@ export const GeneratePopoverBody: React.FC<{
if (!dsl || !indexName) return;
const dslObject = JSON.parse(dsl);
const filters = dslObject?.query?.bool?.filter;
if (!filters) return;
const timeDslIndex = filters?.findIndex((filter: Record<string, string>) => filter?.range);
const timeDsl = filters[timeDslIndex]?.range;
const timeFieldName = Object.keys(timeDsl)[0];
if (!timeFieldName) return;
filters?.splice(timeDslIndex, 1);
if (!filters?.length) return;
const { timeRangeDSL, newFilters, timeFieldName } = extractTimeRangeDSL(filters);
// Filter out time range DSL and use this result to build filter query.
if (!timeFieldName || !timeRangeDSL) return;
dslObject.query.bool.filter = newFilters;

if (getStartServices) {
const [coreStart, startDeps] = await getStartServices();
Expand All @@ -222,11 +231,18 @@ export const GeneratePopoverBody: React.FC<{
coreStart.savedObjects,
indexPattern,
dslObject,
timeDsl[timeFieldName],
timeRangeDSL,
context?.dataSourceId
);
// Navigate to new discover with query built to populate
coreStart.application.navigateToUrl(`data-explorer/discover#?${query}`);
// Navigate to new discover with query built to populate, use new window to avoid discover search failed.
const discoverUrl = `data-explorer/discover#?${query}`;
const currentWorkspace = coreStart.workspaces.currentWorkspace$.getValue();
const url = formatUrlWithWorkspaceId(
discoverUrl,
currentWorkspace?.id ?? '',
coreStart.http.basePath
);
window.open(url, '_blank');
}
} finally {
setDiscoverLoading(false);
Expand Down
12 changes: 12 additions & 0 deletions public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,15 @@ export type IncontextInsightType =
| 'error';

export type TabId = 'chat' | 'compose' | 'insights' | 'history' | 'trace';

export interface NestedRecord<T = string> {
[key: string]: T | NestedRecord<T>;
}

export interface DSL {
query?: {
bool?: {
filter?: unknown[];
};
};
}
54 changes: 46 additions & 8 deletions public/utils/alerting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

import rison from 'rison-node';
import { stringify } from 'query-string';
import moment from 'moment';
import { buildCustomFilter } from '../../../../src/plugins/data/common';
import { url } from '../../../../src/plugins/opensearch_dashboards_utils/public';
import {
DataPublicPluginStart,
opensearchFilters,
IndexPattern,
Filter,
} from '../../../../src/plugins/data/public';
import { CoreStart } from '../../../../src/core/public';
import { NestedRecord, DSL } from '../types';

export const buildFilter = (indexPatternId: string, dsl: Record<string, unknown>) => {
export const buildFilter = (indexPatternId: string, dsl: DSL) => {
const filterAlias = 'Alerting-filters';
return buildCustomFilter(
indexPatternId,
Expand Down Expand Up @@ -69,16 +72,19 @@ export const buildUrlQuery = async (
dataStart: DataPublicPluginStart,
savedObjects: CoreStart['savedObjects'],
indexPattern: IndexPattern,
dsl: Record<string, unknown>,
dsl: DSL,
timeDsl: Record<'from' | 'to', string>,
dataSourceId?: string
) => {
const filter = buildFilter(indexPattern.id!, dsl);

const filterManager = dataStart.query.filterManager;
// There are some map and flatten operations to filters in filterManager, use this to keep aligned with discover.
filterManager.setAppFilters([filter]);
const filters = filterManager.getAppFilters();
let filters: Filter[] = [];
// If there is none filter after filtering timeRange filter, skip to build filter query.
if ((dsl?.query?.bool?.filter?.length ?? 0) > 0) {
const filter = buildFilter(indexPattern.id!, dsl);
const filterManager = dataStart.query.filterManager;
// There are some map and flatten operations to filters in filterManager, use this to keep aligned with discover.
filterManager.setAppFilters([filter]);
filters = filterManager.getAppFilters();
}

const refreshInterval = {
pause: true,
Expand Down Expand Up @@ -142,3 +148,35 @@ export const buildUrlQuery = async (
);
return hash;
};

export const validateToTimeRange = (time: string) => {
// Alerting uses this format in to field of time range filter.
const TO_TIME_FORMAT = 'YYYY-MM-DDTHH:mm:ssZ';
return moment.utc(time, TO_TIME_FORMAT, true).isValid();
};

export const extractTimeRangeDSL = (filters: NestedRecord[]) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a unit test for these two functions?

let timeRangeDSL;
let timeFieldName;
const newFilters = filters.filter((filter) => {
if (filter?.range && typeof filter.range === 'object') {
for (const key of Object.keys(filter.range)) {
const rangeValue = filter.range[key];
if (typeof rangeValue === 'object' && 'to' in rangeValue) {
const toValue = rangeValue.to;
if (typeof toValue === 'string' && validateToTimeRange(toValue)) {
timeRangeDSL = filter.range[key];
timeFieldName = key;
return false;
}
}
}
}
return true;
});
return {
newFilters,
timeRangeDSL,
timeFieldName,
};
};
Loading