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

feat: take index pattern and user input to t2viz from discover #349

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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- fix: Fix error time field and add filter for same name index patterns([#345](https://github.com/opensearch-project/dashboards-assistant/pull/345))
- refactor: Add data source info in discover url when navigating([#347](https://github.com/opensearch-project/dashboards-assistant/pull/347))
- feat: only display ai actions that compatible with the datasource([#350](https://github.com/opensearch-project/dashboards-assistant/pull/350))
- feat: take index pattern and query assistant input to text2viz app([#349](https://github.com/opensearch-project/dashboards-assistant/pull/349))


### 📈 Features/Enhancements
Expand Down
13 changes: 10 additions & 3 deletions public/components/visualization/text2viz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { v4 as uuidv4 } from 'uuid';

import { useCallback } from 'react';
import { useObservable } from 'react-use';
import { useParams } from 'react-router-dom';
import { useLocation, useParams } from 'react-router-dom';
import { SourceSelector } from './source_selector';
import type { IndexPattern } from '../../../../../src/plugins/data/public';
import chatIcon from '../../assets/chat.svg';
Expand Down Expand Up @@ -52,9 +52,16 @@ import { HeaderVariant } from '../../../../../src/core/public';
import { TEXT2VEGA_INPUT_SIZE_LIMIT } from '../../../common/constants/llm';
import { FeedbackThumbs } from '../feedback_thumbs';

export const INDEX_PATTERN_URL_SEARCH_KEY = 'indexPatternId';
export const ASSISTANT_INPUT_URL_SEARCH_KEY = 'assistantInput';

export const Text2Viz = () => {
const { savedObjectId } = useParams<{ savedObjectId?: string }>();
const [selectedSource, setSelectedSource] = useState('');
const { search } = useLocation();
const searchParams = useMemo(() => new URLSearchParams(search), [search]);
const [selectedSource, setSelectedSource] = useState(
searchParams.get(INDEX_PATTERN_URL_SEARCH_KEY) ?? ''
);
const [savedObjectLoading, setSavedObjectLoading] = useState(false);
const [submitting, setSubmitting] = useState(false);
const {
Expand Down Expand Up @@ -89,7 +96,7 @@ export const Text2Viz = () => {

const useUpdatedUX = uiSettings.get('home:useNewHomePage');

const [input, setInput] = useState('');
const [input, setInput] = useState(searchParams.get(ASSISTANT_INPUT_URL_SEARCH_KEY) ?? '');
const [editorInput, setEditorInput] = useState('');
const text2vegaRef = useRef(new Text2Vega(http, data.search, savedObjects));

Expand Down
27 changes: 24 additions & 3 deletions public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ import {
} from './vis_nlq/saved_object_loader';
import { NLQVisualizationEmbeddableFactory } from './components/visualization/embeddable/nlq_vis_embeddable_factory';
import { NLQ_VISUALIZATION_EMBEDDABLE_TYPE } from './components/visualization/embeddable/nlq_vis_embeddable';
import {
ASSISTANT_INPUT_URL_SEARCH_KEY,
INDEX_PATTERN_URL_SEARCH_KEY,
} from './components/visualization/text2viz';
import { DEFAULT_DATA } from '../../../src/plugins/data/common';

export const [getCoreStart, setCoreStart] = createGetterSetter<CoreStart>('CoreStart');

Expand Down Expand Up @@ -337,7 +342,7 @@ export class AssistantPlugin
// T2Viz is only compatible with data sources that have certain agents configured
isCompatible: async (context) => {
// t2viz only supports selecting index pattern at the moment
if (context.datasetType === 'INDEX_PATTERN' && context.datasetId) {
if (context.datasetType === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN && context.datasetId) {
const res = await assistantServiceStart.client.agentConfigExists(
[
TEXT2VEGA_RULE_BASED_AGENT_CONFIG_ID,
Expand All @@ -352,8 +357,24 @@ export class AssistantPlugin
}
return false;
},
execute: async () => {
core.application.navigateToApp(TEXT2VIZ_APP_ID);
execute: async (context) => {
const url = new URL(core.application.getUrlForApp(TEXT2VIZ_APP_ID, { absolute: true }));
if (context.datasetId && context.datasetType === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN) {
url.searchParams.set(INDEX_PATTERN_URL_SEARCH_KEY, context.datasetId);
}
/**
* TODO: the current implementation of getting query assistant input needs to be refactored
* once query assistant is moved to dashboard-assistant repo. Currently, there is no better
* way to get the input value as the query assistant is currently implemented in OSD core.
*/
const queryAssistInputEle = document.getElementsByClassName('queryAssist__input')[0];
if (queryAssistInputEle instanceof HTMLInputElement) {
const input = queryAssistInputEle.value;
if (input) {
url.searchParams.set(ASSISTANT_INPUT_URL_SEARCH_KEY, input);
}
}
Comment on lines +370 to +376
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we get the value from the place triggered this action? If so, we can use context to pass the value.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bit tricky, the context menu is updated based on data.query.queryString.getUpdates$(), but the query assistant input is not part of it. Didn't find a way to easily update the context when the input changes. I have a todo to refactor this when we moved query assistant to dashboards-assistant plugin.

core.application.navigateToUrl(url.toString());
},
});
const savedVisNLQLoader = createVisNLQSavedObjectLoader({
Expand Down
Loading