Skip to content

Commit

Permalink
Disable field stats
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Sep 20, 2024
1 parent bddeac0 commit 545a116
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ import {
import type { DataView } from '@kbn/data-views-plugin/public';
import { dynamic } from '@kbn/shared-ux-utility';
import { isDefined } from '@kbn/ml-is-defined';
import { EuiFlexItem } from '@elastic/eui';
import { EuiCallOut, EuiEmptyPrompt, EuiFlexItem } from '@elastic/eui';
import { css } from '@emotion/react';
import type { ActionExecutionContext } from '@kbn/ui-actions-plugin/public';
import type { Filter } from '@kbn/es-query';
import { FilterStateStore } from '@kbn/es-query';
import { getESQLAdHocDataview } from '@kbn/esql-utils';
import { ENABLE_ESQL, getESQLAdHocDataview } from '@kbn/esql-utils';
import { ACTION_GLOBAL_APPLY_FILTER } from '@kbn/unified-search-plugin/public';
import { FormattedMessage } from '@kbn/i18n-react';
import type { DataVisualizerTableState } from '../../../../../common/types';
import type { DataVisualizerPluginStart } from '../../../../plugin';
import type { FieldStatisticsTableEmbeddableState } from '../grid_embeddable/types';
Expand Down Expand Up @@ -153,39 +154,40 @@ export const getFieldStatsChartEmbeddableFactory = (
serializeFieldStatsChartState,
onFieldStatsTableDestroy,
resetData$,
} = initializeFieldStatsControls(state);
} = initializeFieldStatsControls(state, deps.uiSettings);
const { onError, dataLoading, blockingError } = dataLoadingApi;

const defaultDataViewId = await deps.data.dataViews.getDefaultId();
const validDataViewId: string =
isDefined(state.dataViewId) && state.dataViewId !== ''
? state.dataViewId
: defaultDataViewId ?? '';
let initialDataView: DataView[] | undefined;
const validDataViewId: string | undefined =
isDefined(state.dataViewId) && state.dataViewId !== '' ? state.dataViewId : undefined;
let initialDataView: DataView | undefined;
try {
const dataView = isESQLQuery(state.query)
? await getESQLAdHocDataview(state.query.esql, deps.data.dataViews)
: await deps.data.dataViews.get(validDataViewId);
initialDataView = [dataView];
: validDataViewId
? await deps.data.dataViews.get(validDataViewId)
: undefined;
initialDataView = dataView;
} catch (error) {
// Only need to publish blocking error if viewtype is data view, and no data view found
if (state.viewType === FieldStatsInitializerViewType.DATA_VIEW) {
onError(error);
}
}

const dataViews$ = new BehaviorSubject<DataView[] | undefined>(initialDataView);
const dataViews$ = new BehaviorSubject<DataView[] | undefined>(
initialDataView ? [initialDataView] : undefined
);

const subscriptions = new Subscription();
if (fieldStatsControlsApi.dataViewId$) {
subscriptions.add(
fieldStatsControlsApi.dataViewId$
.pipe(
skip(1),
skipWhile((dataViewId) => !dataViewId && !defaultDataViewId),
skipWhile((dataViewId) => !dataViewId),
switchMap(async (dataViewId) => {
try {
return await deps.data.dataViews.get(dataViewId ?? defaultDataViewId);
return await deps.data.dataViews.get(dataViewId);
} catch (error) {
return undefined;
}
Expand Down Expand Up @@ -324,6 +326,8 @@ export const getFieldStatsChartEmbeddableFactory = (
api.viewType$,
api.showDistributions$
);
const isEsqlEnabled = deps.uiSettings.get(ENABLE_ESQL);

const lastReloadRequestTime = useObservable(reload$, Date.now());

const isEsqlMode = viewType === FieldStatsInitializerViewType.ESQL;
Expand Down Expand Up @@ -362,6 +366,47 @@ export const getFieldStatsChartEmbeddableFactory = (
};
}, []);

if (viewType === FieldStatsInitializerViewType.DATA_VIEW && !dataViews) {
return (
<EuiEmptyPrompt
color="primary"
title={
<h3>
<FormattedMessage
id="xpack.dashboard.fieldStats.noDataViewSelected"
defaultMessage="No data view selected"
/>
</h3>
}
body={
<p>
<FormattedMessage
id="xpack.dashboard.fieldStats.noDataViewSelectedDescription"
defaultMessage="Pick a data view to view field statistics."
/>
</p>
}
/>
);
}

if (isEsqlMode && !isEsqlEnabled) {
return (
<EuiCallOut
title={
<h3>
<FormattedMessage
id="xpack.dashboard.fieldStats.noDataViewSelected"
defaultMessage="ES|QL is disabled"
/>
</h3>
}
color="warning"
iconType="alert"
/>
);
}

return (
<EuiFlexItem css={statsTableCss} data-test-subj="dashboardFieldStatsEmbeddedContent">
<FieldStatisticsWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export const FieldStatisticsInitializer: FC<FieldStatsInitializerProps> = ({
defaultMessage: 'Data view',
}
)}
css={css({ padding: euiThemeVars.euiSizeM })}
>
<IndexPatternSelect
autoFocus={!dataViewId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ import type { AggregateQuery } from '@kbn/es-query';
import type { StateComparators } from '@kbn/presentation-publishing';
import { BehaviorSubject } from 'rxjs';
import fastIsEqual from 'fast-deep-equal';
import type { IUiSettingsClient } from '@kbn/core-ui-settings-browser';
import { ENABLE_ESQL } from '@kbn/esql-utils';
import { FieldStatsInitializerViewType } from '../grid_embeddable/types';
import type { FieldStatsInitialState } from '../grid_embeddable/types';
import type { FieldStatsControlsApi } from './types';

export const initializeFieldStatsControls = (rawState: FieldStatsInitialState) => {
export const initializeFieldStatsControls = (
rawState: FieldStatsInitialState,
uiSettings: IUiSettingsClient
) => {
const isEsqlEnabled = uiSettings.get(ENABLE_ESQL);
const defaultType = isEsqlEnabled
? FieldStatsInitializerViewType.ESQL
: FieldStatsInitializerViewType.DATA_VIEW;
const viewType$ = new BehaviorSubject<FieldStatsInitializerViewType | undefined>(
rawState.viewType ?? FieldStatsInitializerViewType.ESQL
rawState.viewType ?? defaultType
);
const dataViewId$ = new BehaviorSubject<string | undefined>(rawState.dataViewId);
const query$ = new BehaviorSubject<AggregateQuery | undefined>(rawState.query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import React from 'react';
import { isDefined } from '@kbn/ml-is-defined';
import { COMMON_VISUALIZATION_GROUPING } from '@kbn/visualizations-plugin/public';
import { ENABLE_ESQL } from '@kbn/esql-utils';
import { FIELD_STATS_EMBEDDABLE_TYPE } from '../embeddables/field_stats/constants';
import type { DataVisualizerStartDependencies } from '../../common/types/data_visualizer_plugin';
import type {
Expand Down Expand Up @@ -139,22 +140,31 @@ export function createAddFieldStatsTableAction(
i18n.translate('xpack.dataVisualizer.fieldStatistics.displayName', {
defaultMessage: 'Field statistics',
}),
disabled: !coreStart.uiSettings.get(ENABLE_ESQL),
async isCompatible(context: EmbeddableApiContext) {
return Boolean(await parentApiIsCompatible(context.embeddable));
return (
Boolean(await parentApiIsCompatible(context.embeddable)) &&
coreStart.uiSettings.get(ENABLE_ESQL)
);
},
async execute(context) {
const presentationContainerParent = await parentApiIsCompatible(context.embeddable);
if (!presentationContainerParent) throw new IncompatibleActionError();

const isEsqlEnabled = coreStart.uiSettings.get(ENABLE_ESQL);
try {
const defaultIndexPattern = await pluginStart.data.dataViews.getDefault();
const defaultInitialState: FieldStatsInitialState = {
viewType: FieldStatsInitializerViewType.ESQL,
query: {
// Initial default query
esql: `from ${defaultIndexPattern?.getIndexPattern()} | limit 10`,
},
};
const defaultInitialState: FieldStatsInitialState = isEsqlEnabled
? {
viewType: FieldStatsInitializerViewType.ESQL,
query: {
// Initial default query
esql: `from ${defaultIndexPattern?.getIndexPattern()} | limit 10`,
},
}
: {
viewType: FieldStatsInitializerViewType.DATA_VIEW,
};
const embeddable = await presentationContainerParent.addNewPanel<
object,
FieldStatisticsTableEmbeddableApi
Expand Down

0 comments on commit 545a116

Please sign in to comment.