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

[ML] Adds support for ad-hoc Data Views for testing models in Trained Models UI #180795

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import { EuiButton } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import React, { useCallback, useEffect, useRef } from 'react';
import { type DataView } from '@kbn/data-plugin/common';
import { useMlKibana } from '../../contexts/kibana';

export const CreateDataViewButton = ({
onDataViewCreated,
allowAdHocDataView = false,
}: {
onDataViewCreated: (id: string, type: string, name?: string) => void;
onDataViewCreated: (dataView: DataView) => void;
allowAdHocDataView?: boolean;
}) => {
const { dataViewEditor } = useMlKibana().services;
Expand All @@ -25,10 +26,9 @@ export const CreateDataViewButton = ({
closeDataViewEditorRef.current = dataViewEditor?.openEditor({
onSave: async (dataView) => {
if (dataView.id && onDataViewCreated) {
onDataViewCreated(dataView.id, 'index-pattern', dataView.name);
onDataViewCreated(dataView);
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
}
},

allowAdHocDataView,
});
}, [onDataViewCreated, dataViewEditor, allowAdHocDataView]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ export const SourceSelection: FC = () => {
uiSettings,
}}
>
<CreateDataViewButton onDataViewCreated={onSearchSelected} allowAdHocDataView={true} />
<CreateDataViewButton
onDataViewCreated={(dataView) => {
onSearchSelected(dataView.id!, 'index-pattern', dataView.getIndexPattern());
}}
allowAdHocDataView={true}
/>
</SavedObjectFinder>
</EuiPanel>
</EuiPageBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export const Page: FC<PageProps> = ({
>
<EuiFlexGroup direction="row" gutterSize="s">
<CreateDataViewButton
onDataViewCreated={onObjectSelection}
onDataViewCreated={(dataView) => {
onObjectSelection(dataView.id!, 'index-pattern', dataView.getIndexPattern());
}}
allowAdHocDataView={true}
/>
{extraButtons ? extraButtons : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import type { FC } from 'react';
import React, { useState, useMemo, useEffect, useCallback } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';

import useObservable from 'react-use/lib/useObservable';
import { firstValueFrom } from 'rxjs';
Expand All @@ -15,14 +16,17 @@ import {
EuiAccordion,
EuiCode,
EuiCodeBlock,
EuiFlexGroup,
EuiFlexItem,
EuiFormRow,
EuiSpacer,
EuiSelect,
EuiSpacer,
EuiText,
} from '@elastic/eui';

import { isPopulatedObject } from '@kbn/ml-is-populated-object';
import { i18n } from '@kbn/i18n';
import { CreateDataViewButton } from '../../../components/create_data_view_button';
import { useMlKibana } from '../../../contexts/kibana';
import { RUNNING_STATE } from './inference_base';
import type { InferrerType } from '.';
Expand All @@ -45,6 +49,7 @@ export const InferenceInputFormIndexControls: FC<Props> = ({
setSelectedDataViewId,
selectedField,
setSelectedField,
setDataViewListItems,
} = data;

const runningState = useObservable(inferrer.getRunningState$(), inferrer.getRunningState());
Expand All @@ -53,27 +58,56 @@ export const InferenceInputFormIndexControls: FC<Props> = ({

return (
<>
<EuiFormRow label="Index" fullWidth>
{disableIndexSelection ? (
<EuiText grow={false}>
<EuiCode>
{dataViewListItems.find((item) => item.value === selectedDataViewId)?.text}
</EuiCode>
</EuiText>
) : (
<EuiSelect
options={dataViewListItems}
value={selectedDataViewId}
onChange={(e) => {
inferrer.setSelectedDataViewId(e.target.value);
setSelectedDataViewId(e.target.value);
}}
hasNoInitialSelection={true}
disabled={runningState === RUNNING_STATE.RUNNING}
<EuiFlexGroup gutterSize={'s'} justifyContent={'spaceBetween'} alignItems={'flexEnd'}>
<EuiFlexItem grow={true}>
<EuiFormRow
label={
<FormattedMessage
id="xpack.ml.trainedModels.testModelsFlyout.dataViewTitle"
defaultMessage="Data view"
/>
}
fullWidth
>
{disableIndexSelection ? (
<EuiText grow={false}>
<EuiCode>
{dataViewListItems.find((item) => item.value === selectedDataViewId)?.text}
</EuiCode>
</EuiText>
) : (
<EuiSelect
options={dataViewListItems}
value={selectedDataViewId}
onChange={(e) => {
inferrer.setSelectedDataViewId(e.target.value);
setSelectedDataViewId(e.target.value);
}}
hasNoInitialSelection={true}
disabled={runningState === RUNNING_STATE.RUNNING}
fullWidth
/>
)}
</EuiFormRow>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<CreateDataViewButton
allowAdHocDataView
onDataViewCreated={(dataView) => {
setDataViewListItems((prev) => {
return [
...prev,
{
text: dataView.getIndexPattern(),
value: dataView.id!,
},
];
});
}}
/>
)}
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>

<EuiSpacer size="m" />
<EuiFormRow
label={i18n.translate('xpack.ml.trainedModels.testModelsFlyout.indexInput.fieldInput', {
Expand Down Expand Up @@ -277,5 +311,6 @@ export function useIndexInput({
setSelectedDataViewId,
selectedField,
setSelectedField,
setDataViewListItems,
};
}