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

[Discover] UI Updates to Dataset Configurator #8166

Merged
merged 10 commits into from
Oct 8, 2024
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
2 changes: 2 additions & 0 deletions changelogs/fragments/8166.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Fixes UI issues in Discover and data configurator ([#8166](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8166))

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ const createStartContract = (isEnhancementsEnabled: boolean = false): Start => {
})
),
clearCache: jest.fn(),
create: jest.fn().mockResolvedValue({
sejli marked this conversation as resolved.
Show resolved Hide resolved
id: 'test-index-pattern',
title: 'Test Index Pattern',
type: 'INDEX_PATTERN',
}),
saveToCache: jest.fn(),
} as unknown) as IndexPatternsContract,
dataSources: dataSourceServiceMock.createStartContract(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@

import { DatasetService } from './dataset_service';
import { coreMock } from '../../../../../../core/public/mocks';
import { DataStorage } from 'src/plugins/data/common';
import { DEFAULT_DATA, DataStorage, Dataset } from 'src/plugins/data/common';
import { DataStructure } from '../../../../common';
import { IDataPluginServices } from '../../../types';
import { indexPatternTypeConfig } from './lib';
import { dataPluginMock } from '../../../mocks';
import { IndexPatternsContract } from '../../..';

describe('DatasetService', () => {
let service: DatasetService;
let uiSettings: ReturnType<typeof coreMock.createSetup>['uiSettings'];
let sessionStorage: DataStorage;
let mockDataPluginServices: jest.Mocked<IDataPluginServices>;
let indexPatterns: IndexPatternsContract;

beforeEach(() => {
uiSettings = coreMock.createSetup().uiSettings;
sessionStorage = new DataStorage(window.sessionStorage, 'opensearchDashboards.');
mockDataPluginServices = {} as jest.Mocked<IDataPluginServices>;

service = new DatasetService(uiSettings, sessionStorage);
indexPatterns = dataPluginMock.createStartContract().indexPatterns;
service.init(indexPatterns);
});

const mockResult = {
Expand Down Expand Up @@ -98,4 +104,29 @@ describe('DatasetService', () => {

expect(service.getLastCacheTime()).toEqual(time);
});
test('calling cacheDataset on dataset caches it', async () => {
const mockDataset = {
id: 'test-dataset',
title: 'Test Dataset',
type: mockType.id,
} as Dataset;
service.registerType(mockType);

await service.cacheDataset(mockDataset);
expect(indexPatterns.create).toHaveBeenCalledTimes(1);
expect(indexPatterns.saveToCache).toHaveBeenCalledTimes(1);
});

test('calling cacheDataset on index pattern does not cache it', async () => {
service.registerType(indexPatternTypeConfig);
const mockDataset = {
id: 'test-index-pattern',
title: 'Test Index Pattern',
type: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN,
} as Dataset;

await service.cacheDataset(mockDataset);
expect(indexPatterns.create).toHaveBeenCalledTimes(0);
expect(indexPatterns.saveToCache).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
DataStructure,
IndexPatternSpec,
DEFAULT_DATA,
IFieldType,
UI_SETTINGS,
DataStorage,
CachedDataStructure,
Expand Down Expand Up @@ -64,14 +63,11 @@ export class DatasetService {

public async cacheDataset(dataset: Dataset): Promise<void> {
const type = this.getType(dataset.type);
if (dataset) {
if (dataset && dataset.type !== DEFAULT_DATA.SET_TYPES.INDEX_PATTERN) {
const spec = {
id: dataset.id,
title: dataset.title,
timeFieldName: {
name: dataset.timeFieldName,
type: 'date',
sejli marked this conversation as resolved.
Show resolved Hide resolved
} as Partial<IFieldType>,
timeFieldName: dataset.timeFieldName,
fields: await type?.fetchFields(dataset),
dataSourceRef: dataset.dataSource
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const indexTypeConfig: DatasetTypeConfig = {
return fields.map((field: any) => ({
name: field.name,
type: field.type,
aggregatable: field?.aggregatable,
}));
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export interface LanguageConfig {
showDocLinks?: boolean;
editorSupportedAppNames?: string[];
supportedAppNames?: string[];
hideDatePicker?: boolean;
}
92 changes: 62 additions & 30 deletions src/plugins/data/public/ui/dataset_selector/configurator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { FormattedMessage } from '@osd/i18n/react';
import React, { useEffect, useState } from 'react';
import { BaseDataset, Dataset, DatasetField } from '../../../common';
import React, { useEffect, useMemo, useState } from 'react';
import { BaseDataset, DEFAULT_DATA, Dataset, DatasetField } from '../../../common';
import { getIndexPatterns, getQueryService } from '../../services';

export const Configurator = ({
Expand All @@ -41,8 +41,14 @@
const languages = type?.supportedLanguages(baseDataset) || [];

const [dataset, setDataset] = useState<Dataset>(baseDataset);
const [timeFields, setTimeFields] = useState<DatasetField[]>();
const [timeFields, setTimeFields] = useState<DatasetField[]>([]);

Check warning on line 44 in src/plugins/data/public/ui/dataset_selector/configurator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_selector/configurator.tsx#L44

Added line #L44 was not covered by tests
const [timeFieldName, setTimeFieldName] = useState<string | undefined>(dataset.timeFieldName);
sejli marked this conversation as resolved.
Show resolved Hide resolved
const noTimeFilter = i18n.translate(

Check warning on line 46 in src/plugins/data/public/ui/dataset_selector/configurator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_selector/configurator.tsx#L46

Added line #L46 was not covered by tests
'data.explorer.datasetSelector.advancedSelector.configurator.timeField.noTimeFieldOptionLabel',
{
defaultMessage: "I don't want to use the time filter",
}
);
const [language, setLanguage] = useState<string>(() => {
const currentLanguage = queryString.getQuery().language;
if (languages.includes(currentLanguage)) {
Expand All @@ -51,6 +57,18 @@
return languages[0];
});

const submitDisabled = useMemo(() => {
return (

Check warning on line 61 in src/plugins/data/public/ui/dataset_selector/configurator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_selector/configurator.tsx#L60-L61

Added lines #L60 - L61 were not covered by tests
timeFieldName === undefined &&
!(
languageService.getLanguage(language)?.hideDatePicker ||
dataset.type === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN
) &&
timeFields &&
timeFields.length > 0
);
}, [dataset, language, timeFieldName, timeFields, languageService]);

useEffect(() => {
const fetchFields = async () => {
const datasetFields = await queryString
Copy link
Member

Choose a reason for hiding this comment

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

we should add a follow up or a spinner to prevent the date field from loading in afterwards

Expand Down Expand Up @@ -97,33 +115,6 @@
>
<EuiFieldText disabled value={dataset.title} />
</EuiFormRow>
{timeFields && timeFields.length > 0 && (
<EuiFormRow
label={i18n.translate(
'data.explorer.datasetSelector.advancedSelector.configurator.timeFieldLabel',
{
defaultMessage: 'Time field',
}
)}
>
<EuiSelect
options={[
...timeFields.map((field) => ({
text: field.displayName || field.name,
value: field.name,
})),
{ text: '-----', value: '', disabled: true },
{ text: 'No time field', value: undefined },
]}
value={timeFieldName}
onChange={(e) => {
const value = e.target.value === 'undefined' ? undefined : e.target.value;
setTimeFieldName(value);
setDataset({ ...dataset, timeFieldName: value });
}}
/>
</EuiFormRow>
)}
<EuiFormRow
label={i18n.translate(
'data.explorer.datasetSelector.advancedSelector.configurator.languageLabel',
Expand All @@ -144,6 +135,46 @@
}}
/>
</EuiFormRow>
{!languageService.getLanguage(language)?.hideDatePicker &&
(dataset.type === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN ? (
<EuiFormRow
label={i18n.translate(
'data.explorer.datasetSelector.advancedSelector.configurator.indexPatternTimeFieldLabel',
{
defaultMessage: 'Time field',
}
)}
>
<EuiFieldText disabled value={dataset.timeFieldName ?? 'No time field'} />
</EuiFormRow>
) : (
<EuiFormRow
label={i18n.translate(
'data.explorer.datasetSelector.advancedSelector.configurator.timeFieldLabel',
{
defaultMessage: 'Time field',
}
)}
>
<EuiSelect
options={[
...timeFields.map((field) => ({

Check warning on line 161 in src/plugins/data/public/ui/dataset_selector/configurator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_selector/configurator.tsx#L161

Added line #L161 was not covered by tests
text: field.displayName || field.name,
value: field.name,
})),
{ text: '-----', value: '-----', disabled: true },
{ text: noTimeFilter, value: noTimeFilter },
Copy link
Member

Choose a reason for hiding this comment

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

i think this is fine but it's better to separate them, since text can change (e.g. different translation, future UX wording update), and value should not change

Copy link
Member

Choose a reason for hiding this comment

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

+1

]}
value={timeFieldName}
onChange={(e) => {
const value = e.target.value === noTimeFilter ? undefined : e.target.value;
setTimeFieldName(e.target.value);
setDataset({ ...dataset, timeFieldName: value });

Check warning on line 172 in src/plugins/data/public/ui/dataset_selector/configurator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_selector/configurator.tsx#L171-L172

Added lines #L171 - L172 were not covered by tests
}}
hasNoInitialSelection
/>
</EuiFormRow>
))}
</EuiForm>
</EuiModalBody>
<EuiModalFooter>
Expand All @@ -165,6 +196,7 @@
onConfirm(dataset);
}}
fill
disabled={submitDisabled}
>
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.confirm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export const AppContainer = React.memo(
)}

<EuiPage
className={classNames('deLayout', isEnhancementsEnabled && 'dsc--next')}
className={classNames(
'deLayout',
isEnhancementsEnabled && !showActionsInGroup ? 'dsc--next' : undefined
)}
paddingSize="none"
grow={false}
>
Expand Down
1 change: 1 addition & 0 deletions src/plugins/query_enhancements/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class QueryEnhancementsPlugin
editor: enhancedSQLQueryEditor,
editorSupportedAppNames: ['discover'],
supportedAppNames: ['discover', 'data-explorer'],
hideDatePicker: true,
};
queryString.getLanguageService().registerLanguage(sqlLanguageConfig);

Expand Down
Loading