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] Allow to store the configured ES|QL visualization v3 #175227

Merged
merged 165 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
165 commits
Select commit Hold shift + click to select a range
38d4298
[Discover] Initial implementation for allowing to store ES|QL customi…
jughosta Jan 5, 2024
d3aaeeb
[Discover] Some refactoring
jughosta Jan 8, 2024
6f6f78b
[Discover] More refactoring
jughosta Jan 9, 2024
cc0ca64
Merge branch 'main' into 167887-esql-saved-search
jughosta Jan 9, 2024
631b174
[CI] Auto-commit changed files from 'node scripts/jest_integration -u…
kibanamachine Jan 9, 2024
599653c
[Discover] Fix comparison for customVisualizationJSON
jughosta Jan 10, 2024
d982b33
[Discover] Support customization for histogram too. Reset when vis in…
jughosta Jan 11, 2024
97656ac
[Discover] Fix reverting saved search changes
jughosta Jan 11, 2024
ddb6b15
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Jan 12, 2024
6347d35
[Discover] Some fixes to make customVisualization work
jughosta Jan 12, 2024
aa9946d
[Discover] Refactor what is being saved. Persist all Lens attributes …
jughosta Jan 12, 2024
d0b3d85
[Discover] Update suggestion selector
jughosta Jan 12, 2024
96dc0b8
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Jan 12, 2024
1caa803
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Jan 15, 2024
538e422
Merge remote-tracking branch 'origin/167887-esql-saved-search-v2' int…
jughosta Jan 15, 2024
6796374
[Discover] Fix suggestion change
jughosta Jan 15, 2024
f323642
[Discover] Fix suggestion type selector
jughosta Jan 15, 2024
266f99c
[Discover] Improve the resetting of suggestion
jughosta Jan 15, 2024
772a701
[Discover] Reduce the state size
jughosta Jan 15, 2024
482121d
Merge branch 'main' into 167887-esql-saved-search-v2
jughosta Jan 17, 2024
cb8e18e
[Discover] Update tests
jughosta Jan 17, 2024
e49a423
[Discover] Clean up
jughosta Jan 17, 2024
08f30be
[Discover] Update tests
jughosta Jan 17, 2024
a1643b4
[Discover] Update tests
jughosta Jan 17, 2024
2033a6d
[Discover] Fix typescript errors
jughosta Jan 17, 2024
c9dbaba
[Discover] Remove some logging
jughosta Jan 17, 2024
c6ee408
[Discover] Some fixes
jughosta Jan 17, 2024
55e3641
[Discover] Don't reset the vis on save
jughosta Jan 17, 2024
98b3c9d
[Discover] Fix histogram case
jughosta Jan 17, 2024
70455f0
[Discover] Fix other case
jughosta Jan 17, 2024
cf130cf
[Discover] Refactor to a service
jughosta Jan 18, 2024
34d01f4
[Discover] Add fixes
jughosta Jan 18, 2024
494fde5
[Discover] Refactor to props
jughosta Jan 22, 2024
c4eb455
[Discover] Clean up
jughosta Jan 22, 2024
b882203
[Discover] Fix tests
jughosta Jan 22, 2024
14c224d
[Discover] Fix tests
jughosta Jan 22, 2024
f835ad3
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Jan 22, 2024
a5b59aa
[Discover] Clean up
jughosta Jan 23, 2024
56ca482
Merge remote-tracking branch 'origin/167887-esql-saved-search-v3' int…
jughosta Jan 23, 2024
9996c5d
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Jan 23, 2024
1dc7474
[Discover] Simplify state management
jughosta Jan 23, 2024
5de6c05
[Discover] Update tests
jughosta Jan 24, 2024
73d5819
[Discover] Don't sync with URL
jughosta Jan 24, 2024
1871a4e
[Discover] Improve resetting of lens attributes
jughosta Jan 24, 2024
f28d4bd
[Discover] Add logging
jughosta Jan 24, 2024
ac9d3fe
[Discover] Remove suggestion from histogram main state
jughosta Jan 24, 2024
9ece3ce
[Discover] Return suggestion to histogram main state as it triggers r…
jughosta Jan 25, 2024
fc2ec95
[Discover] Disable logging
jughosta Jan 25, 2024
38c69af
[Discover] Fix breakdown field
jughosta Jan 26, 2024
4181550
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Jan 31, 2024
0328095
[Discover] Delay calculation of table until search is complete
jughosta Jan 31, 2024
6d23632
Migrate Lens Vis State to InternalStateContainer
kertal Jan 30, 2024
1b2fb11
[Discover] Skip syncing to internalStateContainer
jughosta Jan 31, 2024
db9a48d
[Discover] Change props format
jughosta Jan 31, 2024
c0eda01
[Discover] Back to the problem
jughosta Jan 31, 2024
ee25770
[Discover] Improve reverting logic
jughosta Jan 31, 2024
525e7c3
[Discover] Comment logs out
jughosta Feb 1, 2024
bad165d
[Discover] Comment logs out
jughosta Feb 1, 2024
7e53323
[Discover] Fix checks
jughosta Feb 1, 2024
c6b124c
[Discover] Fix sample size schema
jughosta Feb 1, 2024
ae17bcf
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Feb 1, 2024
3a211b2
[Discover] Check tests
jughosta Feb 2, 2024
5b382b9
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Feb 2, 2024
031343e
[Discover] Merge
jughosta Feb 2, 2024
895a08e
[Discover] Fix checks
jughosta Feb 2, 2024
c8f837c
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Feb 2, 2024
30f9df0
[Discover] Fix for ft
jughosta Feb 2, 2024
60e217d
[Discover] Clean up
jughosta Feb 2, 2024
1e5c502
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Feb 5, 2024
7e9dd06
[Discover] Fix based on tests
jughosta Feb 5, 2024
825aa71
[Discover] Update mocks
jughosta Feb 5, 2024
04a3430
[Discover] Restore tests for lens attributes
jughosta Feb 5, 2024
09960ce
[Discover] Fix tests
jughosta Feb 5, 2024
189a182
[Discover] Simplify tests
jughosta Feb 5, 2024
98923df
[Discover] Restore tests for vis suggestions
jughosta Feb 5, 2024
d7be568
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Feb 5, 2024
1fce17f
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Feb 6, 2024
9940078
[Discover] Add new functional tests
jughosta Feb 6, 2024
a5c1281
[Discover] Update tests
jughosta Feb 6, 2024
ab9593b
[Discover] Switch to local utils
jughosta Feb 6, 2024
9bd096c
[Discover] Add more tests
jughosta Feb 7, 2024
7ff9e21
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Feb 7, 2024
6c11f14
[Discover] Remove redundant chart title
jughosta Feb 7, 2024
f2fc2ce
[Discover] Use the shared util for lens attributes
jughosta Feb 7, 2024
b555cd5
[Discover] Update on revert
jughosta Feb 7, 2024
7f8cb6b
[Discover] Add a test
jughosta Feb 7, 2024
cf5666c
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Feb 7, 2024
c2c15b2
[Discover] Clean up i18n
jughosta Feb 7, 2024
9e49be3
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Feb 7, 2024
f25029a
Merge remote-tracking branch 'origin/167887-esql-saved-search-v3' int…
jughosta Feb 7, 2024
9898d6c
[Discover] Avoid circular deps
jughosta Feb 8, 2024
f1d3994
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Feb 8, 2024
96c3f13
[Discover] Revert type changes
jughosta Feb 8, 2024
7f85d86
[Discover] Update types
jughosta Feb 8, 2024
fbc0181
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Feb 8, 2024
9602ee2
Merge branch 'main' into 167887-esql-saved-search-v3
stratoula Feb 9, 2024
2035802
[Discover] Return previous logic
jughosta Feb 9, 2024
e24d977
Merge branch 'main' into 167887-esql-saved-search-v3
stratoula Feb 12, 2024
d41009a
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Mar 4, 2024
de8347a
[CI] Auto-commit changed files from 'node scripts/jest_integration -u…
kibanamachine Mar 4, 2024
b2de107
[Discover] Fix after the merge
jughosta Mar 4, 2024
e0b14a8
Merge remote-tracking branch 'origin/167887-esql-saved-search-v3' int…
jughosta Mar 4, 2024
ad4aabd
[Discover] Split saved search plugin into 2 plugins and a new package…
jughosta Jan 16, 2024
d66451b
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Mar 4, 2024
1f9e86b
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Mar 4, 2024
48ebc37
[CI] Auto-commit changed files from 'node scripts/build_plugin_list_d…
kibanamachine Mar 4, 2024
db28b3d
[CI] Auto-commit changed files from 'node scripts/jest_integration -u…
kibanamachine Mar 4, 2024
998ba35
[Discover] Make lens migrations optional
jughosta Feb 9, 2024
2707333
Merge remote-tracking branch 'origin/167887-esql-saved-search-v3' int…
jughosta Mar 5, 2024
133afb3
Revert "[Discover] Make lens migrations optional"
jughosta Mar 18, 2024
bc5b78c
Revert [Discover] Split saved search plugin into 2 plugins and a new …
jughosta Mar 18, 2024
35d193a
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Mar 18, 2024
1c2fd84
[Discover] Convert visContextJSON string into visContext object insid…
jughosta Mar 18, 2024
14e4f6f
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Mar 18, 2024
2ac30f8
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Mar 18, 2024
6c03da3
[CI] Auto-commit changed files from 'node scripts/build_plugin_list_d…
kibanamachine Mar 18, 2024
3931132
[CI] Auto-commit changed files from 'node scripts/jest_integration -u…
kibanamachine Mar 18, 2024
b604434
[DataView] Update code style
jughosta Mar 20, 2024
5f68c09
[Discover] Fix types
jughosta Mar 20, 2024
01be3f9
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Mar 20, 2024
d4b53ee
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Mar 20, 2024
9b64a66
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Mar 22, 2024
8e0ee34
[Discover] Rename suggestion types
jughosta Mar 22, 2024
a599413
[Discover] Rename lensAttributesContext into visContext
jughosta Mar 22, 2024
03eb966
[Discover] Remove console logs
jughosta Mar 22, 2024
a0bc190
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Mar 22, 2024
a9229a1
[Discover] Fix types
jughosta Mar 22, 2024
d1ebe77
Merge remote-tracking branch 'origin/167887-esql-saved-search-v3' int…
jughosta Mar 22, 2024
ef97fd7
[Discover] Fix lens title
jughosta Mar 22, 2024
3150a5e
[Discover] Revert redundant changes
jughosta Mar 22, 2024
f356277
[Discover] Add tests
jughosta Mar 22, 2024
ac87b1a
[Discover] Fix tests
jughosta Mar 22, 2024
1a74c67
Merge remote-tracking branch 'upstream/main' into 167887-esql-saved-s…
jughosta Mar 25, 2024
b6b5ac5
[Discover] Change how visContext updates are managed in Discover
jughosta Mar 25, 2024
bd3adb3
[Discover] Update tests
jughosta Mar 25, 2024
d0daf77
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Mar 25, 2024
990c623
[Discover] Some fixes for this version
jughosta Mar 26, 2024
8fb8e8e
Merge remote-tracking branch 'origin/167887-esql-saved-search-v3' int…
jughosta Mar 26, 2024
d97c3f6
[Discover] Update implementation
jughosta Mar 26, 2024
a8f6e5b
[Discover] Simplify the implementation
jughosta Mar 26, 2024
d064930
[Discover] Simplify the implementation even more
jughosta Mar 26, 2024
47b28db
[Discover] New updates
jughosta Mar 26, 2024
55b5cf7
[Discover] Better clearing implementation
jughosta Mar 26, 2024
64a0056
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Mar 26, 2024
d5c1b5d
[Discover] Update tests after the merge
jughosta Mar 27, 2024
a4755a6
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Mar 27, 2024
e658447
[Discover] Fix when suggestion type is changed via flyout
jughosta Mar 27, 2024
f3d92d2
[Discover] Fix when suggestion type is changed to an unfamiliar one v…
jughosta Mar 27, 2024
a9e0d9a
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Mar 27, 2024
3400b7e
[Discover] Add more checks
jughosta Mar 27, 2024
6151f7d
[Discover] Update invalidation logic
jughosta Mar 28, 2024
46330ea
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Mar 28, 2024
53139df
[Discover] Fix invalidation logic
jughosta Mar 28, 2024
12b4b1c
Merge remote-tracking branch 'origin/167887-esql-saved-search-v3' int…
jughosta Mar 28, 2024
90448a0
Merge branch 'main' into 167887-esql-saved-search-v3
stratoula Apr 2, 2024
051bd82
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Apr 3, 2024
4406b0e
[Discover] Mark other lnsXY charts as Customized too
jughosta Apr 3, 2024
531c7a7
Merge branch 'main' into 167887-esql-saved-search-v3
stratoula Apr 4, 2024
b6637ba
[Discover] Address PR comments
jughosta Apr 5, 2024
e52a57b
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Apr 5, 2024
7088f98
[Discover] Fix flyout Cancel action and improve the icon
jughosta Apr 5, 2024
280323d
[Discover] Update tests
jughosta Apr 5, 2024
d49f1e3
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Apr 5, 2024
3f1330e
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Apr 8, 2024
0d9235f
Merge branch 'main' into 167887-esql-saved-search-v3
jughosta Apr 8, 2024
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 @@ -20,6 +20,7 @@ describe('getLensAttributesFromSuggestion', () => {
timeFieldName: '@timestamp',
isPersisted: () => false,
toSpec: () => ({}),
toMinimalSpec: () => ({}),
} as unknown as DataView;
const query: AggregateQuery = { esql: 'from foo | limit 10' };

Expand Down
24 changes: 17 additions & 7 deletions packages/kbn-visualization-utils/src/get_lens_attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ export const getLensAttributesFromSuggestion = ({
query: Query | AggregateQuery;
suggestion: Suggestion | undefined;
dataView?: DataView;
}) => {
}): {
references: Array<{ name: string; id: string; type: string }>;
visualizationType: string;
state: {
visualization: {};
datasourceStates: Record<string, unknown>;
query: Query | AggregateQuery;
filters: Filter[];
};
title: string;
} => {
const suggestionDatasourceState = Object.assign({}, suggestion?.datasourceState);
const suggestionVisualizationState = Object.assign({}, suggestion?.visualizationState);
const datasourceStates =
Expand All @@ -35,11 +45,11 @@ export const getLensAttributesFromSuggestion = ({
};
const visualization = suggestionVisualizationState;
const attributes = {
title: suggestion
? suggestion.title
: i18n.translate('visualizationUtils.config.suggestion.title', {
defaultMessage: 'New suggestion',
}),
title:
suggestion?.title ??
i18n.translate('visualizationUtils.config.suggestion.title', {
defaultMessage: 'New suggestion',
}),
references: [
{
id: dataView?.id ?? '',
Expand All @@ -55,7 +65,7 @@ export const getLensAttributesFromSuggestion = ({
...(dataView &&
dataView.id &&
!dataView.isPersisted() && {
adHocDataViews: { [dataView.id]: dataView.toSpec(false) },
adHocDataViews: { [dataView.id]: dataView.toMinimalSpec() },
}),
},
visualizationType: suggestion ? suggestion.visualizationId : 'lnsXY',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"risk-engine-configuration": "aea0c371a462e6d07c3ceb3aff11891b47feb09d",
"rules-settings": "892a2918ebaeba809a612b8d97cec0b07c800b5f",
"sample-data-telemetry": "37441b12f5b0159c2d6d5138a494c9f440e950b5",
"search": "cf69e2bf8ae25c10af21887cd6effc4a9ea73064",
"search": "7598e4a701ddcaa5e3f44f22e797618a48595e6f",
"search-session": "b2fcd840e12a45039ada50b1355faeafa39876d1",
"search-telemetry": "b568601618744720b5662946d3103e3fb75fe8ee",
"security-rule": "07abb4d7e707d91675ec0495c73816394c7b521f",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, { useCallback, useMemo } from 'react';
import { UnifiedHistogramContainer } from '@kbn/unified-histogram-plugin/public';
import { css } from '@emotion/react';
import useObservable from 'react-use/lib/useObservable';
import { Datatable } from '@kbn/expressions-plugin/common';
import type { Datatable } from '@kbn/expressions-plugin/common';
import { useDiscoverHistogram } from './use_discover_histogram';
import { type DiscoverMainContentProps, DiscoverMainContent } from './discover_main_content';
import { useAppStateSelector } from '../../services/discover_app_state_container';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

import { useQuerySubscriber } from '@kbn/unified-field-list/src/hooks/use_query_subscriber';
import {
canImportVisContext,
UnifiedHistogramApi,
UnifiedHistogramExternalVisContextStatus,
UnifiedHistogramFetchStatus,
UnifiedHistogramState,
UnifiedHistogramVisContext,
} from '@kbn/unified-histogram-plugin/public';
import { isEqual } from 'lodash';
import { useCallback, useEffect, useRef, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import {
debounceTime,
distinctUntilChanged,
Expand All @@ -26,6 +29,9 @@ import {
} from 'rxjs';
import useObservable from 'react-use/lib/useObservable';
import type { RequestAdapter } from '@kbn/inspector-plugin/common';
import type { DatatableColumn } from '@kbn/expressions-plugin/common';
import type { SavedSearch } from '@kbn/saved-search-plugin/common';
import type { Filter } from '@kbn/es-query';
import { useDiscoverCustomization } from '../../../../customizations';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { FetchStatus } from '../../../types';
Expand All @@ -35,7 +41,11 @@ import type { DiscoverStateContainer } from '../../services/discover_state';
import { addLog } from '../../../../utils/add_log';
import { useInternalStateSelector } from '../../services/discover_internal_state_container';
import type { DiscoverAppState } from '../../services/discover_app_state_container';
import { RecordRawType } from '../../services/discover_data_state_container';
import { DataDocumentsMsg, RecordRawType } from '../../services/discover_data_state_container';
import { useSavedSearch } from '../../services/discover_state_provider';

const EMPTY_TEXT_BASED_COLUMNS: DatatableColumn[] = [];
const EMPTY_FILTERS: Filter[] = [];

export interface UseDiscoverHistogramProps {
stateContainer: DiscoverStateContainer;
Expand All @@ -52,6 +62,7 @@ export const useDiscoverHistogram = ({
}: UseDiscoverHistogramProps) => {
const services = useDiscoverServices();
const savedSearchData$ = stateContainer.dataState.data$;
const savedSearchState = useSavedSearch();

/**
* API initialization
Expand Down Expand Up @@ -219,15 +230,18 @@ export const useDiscoverHistogram = ({
[stateContainer]
);

const [initialTextBasedProps] = useState(() =>
getUnifiedHistogramPropsForTextBased({
documentsValue: savedSearchData$.documents$.getValue(),
savedSearch: stateContainer.savedSearchState.getState(),
})
);

const {
dataView: textBasedDataView,
query: textBasedQuery,
columns,
} = useObservable(textBasedFetchComplete$, {
dataView: stateContainer.internalState.getState().dataView!,
query: stateContainer.appState.getState().query,
columns: savedSearchData$.documents$.getValue().textBasedQueryColumns ?? [],
});
columns: textBasedColumns,
} = useObservable(textBasedFetchComplete$, initialTextBasedProps);

useEffect(() => {
if (!isPlainRecord) {
Expand Down Expand Up @@ -316,14 +330,53 @@ export const useDiscoverHistogram = ({

const histogramCustomization = useDiscoverCustomization('unified_histogram');

const filtersMemoized = useMemo(
() => [...(filters ?? []), ...customFilters],
[filters, customFilters]
);
const filtersMemoized = useMemo(() => {
const allFilters = [...(filters ?? []), ...customFilters];
return allFilters.length ? allFilters : EMPTY_FILTERS;
}, [filters, customFilters]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const timeRangeMemoized = useMemo(() => timeRange, [timeRange?.from, timeRange?.to]);

const onVisContextChanged = useCallback(
(
nextVisContext: UnifiedHistogramVisContext | undefined,
externalVisContextStatus: UnifiedHistogramExternalVisContextStatus
) => {
switch (externalVisContextStatus) {
case UnifiedHistogramExternalVisContextStatus.manuallyCustomized:
// if user customized the visualization manually
// (only this action should trigger Unsaved changes badge)
stateContainer.savedSearchState.updateVisContext({
nextVisContext,
});
stateContainer.internalState.transitions.setOverriddenVisContextAfterInvalidation(
undefined
);
break;
case UnifiedHistogramExternalVisContextStatus.automaticallyOverridden:
// if the visualization was invalidated as incompatible and rebuilt
// (it will be used later for saving the visualization via Save button)
stateContainer.internalState.transitions.setOverriddenVisContextAfterInvalidation(
nextVisContext
);
break;
case UnifiedHistogramExternalVisContextStatus.automaticallyCreated:
case UnifiedHistogramExternalVisContextStatus.applied:
// clearing the value in the internal state so we don't use it during saved search saving
stateContainer.internalState.transitions.setOverriddenVisContextAfterInvalidation(
undefined
);
break;
case UnifiedHistogramExternalVisContextStatus.unknown:
// using `{}` to overwrite the value inside the saved search SO during saving
stateContainer.internalState.transitions.setOverriddenVisContextAfterInvalidation({});
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if null could be used for clearing the saved search value instead? Just thinking it might be clearer than an empty object, but not a big concern really.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@davismcphee I tried with null initially but it was skipped completely during saving the SO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh that's surprising, I was under the impression it was only undefined that gets skipped since I used null for clearing values recently in saved query SOs. In any case it's a minor concern and doesn't impact the functionality, so no big deal.

break;
}
},
[stateContainer]
);

return {
ref,
getCreationOptions,
Expand All @@ -333,12 +386,18 @@ export const useDiscoverHistogram = ({
filters: filtersMemoized,
timeRange: timeRangeMemoized,
relativeTimeRange,
columns,
columns: isPlainRecord ? textBasedColumns : undefined,
onFilter: histogramCustomization?.onFilter,
onBrushEnd: histogramCustomization?.onBrushEnd,
withDefaultActions: histogramCustomization?.withDefaultActions,
disabledActions: histogramCustomization?.disabledActions,
isChartLoading: isSuggestionLoading,
// visContext should be in sync with current query
externalVisContext:
isPlainRecord && canImportVisContext(savedSearchState?.visContext)
? savedSearchState?.visContext
: undefined,
onVisContextChanged: isPlainRecord ? onVisContextChanged : undefined,
};
};

Expand Down Expand Up @@ -412,12 +471,13 @@ const createAppStateObservable = (state$: Observable<DiscoverAppState>) => {
const createFetchCompleteObservable = (stateContainer: DiscoverStateContainer) => {
return stateContainer.dataState.data$.documents$.pipe(
distinctUntilChanged((prev, curr) => prev.fetchStatus === curr.fetchStatus),
filter(({ fetchStatus }) => fetchStatus === FetchStatus.COMPLETE),
map(({ textBasedQueryColumns }) => ({
dataView: stateContainer.internalState.getState().dataView!,
query: stateContainer.appState.getState().query!,
columns: textBasedQueryColumns ?? [],
}))
filter(({ fetchStatus }) => [FetchStatus.COMPLETE, FetchStatus.ERROR].includes(fetchStatus)),
map((documentsValue) => {
return getUnifiedHistogramPropsForTextBased({
documentsValue,
savedSearch: stateContainer.savedSearchState.getState(),
});
})
);
};

Expand All @@ -430,7 +490,27 @@ const createTotalHitsObservable = (state$?: Observable<UnifiedHistogramState>) =

const createCurrentSuggestionObservable = (state$: Observable<UnifiedHistogramState>) => {
return state$.pipe(
map((state) => state.currentSuggestion),
map((state) => state.currentSuggestionContext),
distinctUntilChanged(isEqual)
);
};

function getUnifiedHistogramPropsForTextBased({
documentsValue,
savedSearch,
}: {
documentsValue: DataDocumentsMsg | undefined;
savedSearch: SavedSearch;
}) {
const columns = documentsValue?.textBasedQueryColumns || EMPTY_TEXT_BASED_COLUMNS;

const nextProps = {
dataView: savedSearch.searchSource.getField('index')!,
query: savedSearch.searchSource.getField('query'),
columns,
};

addLog('[UnifiedHistogram] delayed next props for text-based', nextProps);

return nextProps;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ export const getTopNavBadges = ({
if (hasUnsavedChanges && !defaultBadges?.unsavedChangesBadge?.disabled) {
entries.push({
data: getTopNavUnsavedChangesBadge({
onRevert: stateContainer.actions.undoSavedSearchChanges,
onRevert: async () => {
const lensEditFlyoutCancelButton = document.getElementById('lnsCancelEditOnFlyFlyout');
if (lensEditFlyoutCancelButton) {
lensEditFlyoutCancelButton.click?.();
}
Comment on lines +49 to +52
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be great to clean this up and avoid interacting with the DOM like this, but I get it would be tricky. If we don't address it in this PR, I think it would be good to add as a cleanup item in a followup issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have ideas yet how to implement it cleaner.

Copy link
Contributor

Choose a reason for hiding this comment

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

Neither do I 😅 But I think it's worth tracking as a cleanup item since I'm sure we can find a nicer way to approach it later, but it's good for now.

await stateContainer.actions.undoSavedSearchChanges();
},
onSave:
services.capabilities.discover.save && !isManaged
? async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export async function onSaveSearch({
}) {
const { uiSettings, savedObjectsTagging } = services;
const dataView = state.internalState.getState().dataView;
const overriddenVisContextAfterInvalidation =
state.internalState.getState().overriddenVisContextAfterInvalidation;

const onSave = async ({
newTitle,
newCopyOnSave,
Expand All @@ -116,6 +119,7 @@ export async function onSaveSearch({
const currentSampleSize = savedSearch.sampleSize;
const currentDescription = savedSearch.description;
const currentTags = savedSearch.tags;
const currentVisContext = savedSearch.visContext;
savedSearch.title = newTitle;
savedSearch.description = newDescription;
savedSearch.timeRestore = newTimeRestore;
Expand All @@ -134,6 +138,11 @@ export async function onSaveSearch({
if (savedObjectsTagging) {
savedSearch.tags = newTags;
}

if (overriddenVisContextAfterInvalidation) {
savedSearch.visContext = overriddenVisContextAfterInvalidation;
}

const saveOptions: SaveSavedSearchOptions = {
onTitleDuplicate,
copyOnSave: newCopyOnSave,
Expand All @@ -159,10 +168,12 @@ export async function onSaveSearch({
savedSearch.rowsPerPage = currentRowsPerPage;
savedSearch.sampleSize = currentSampleSize;
savedSearch.description = currentDescription;
savedSearch.visContext = currentVisContext;
if (savedObjectsTagging) {
savedSearch.tags = currentTags;
}
} else {
state.internalState.transitions.resetOnSavedSearchChange();
state.appState.resetInitialState();
}
onSaveCb?.();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
createStateContainerReactHelpers,
ReduxLikeStateContainer,
} from '@kbn/kibana-utils-plugin/common';
import { DataView, DataViewListItem } from '@kbn/data-views-plugin/common';
import { Filter } from '@kbn/es-query';
import type { DataView, DataViewListItem } from '@kbn/data-views-plugin/common';
import type { Filter } from '@kbn/es-query';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import type { UnifiedHistogramVisContext } from '@kbn/unified-histogram-plugin/public';

export interface InternalState {
dataView: DataView | undefined;
Expand All @@ -22,6 +23,7 @@ export interface InternalState {
adHocDataViews: DataView[];
expandedDoc: DataTableRecord | undefined;
customFilters: Filter[];
overriddenVisContextAfterInvalidation: UnifiedHistogramVisContext | {} | undefined; // it will be used during saved search saving
}

export interface InternalStateTransitions {
Expand All @@ -40,6 +42,12 @@ export interface InternalStateTransitions {
state: InternalState
) => (dataView: DataTableRecord | undefined) => InternalState;
setCustomFilters: (state: InternalState) => (customFilters: Filter[]) => InternalState;
setOverriddenVisContextAfterInvalidation: (
state: InternalState
) => (
overriddenVisContextAfterInvalidation: UnifiedHistogramVisContext | {} | undefined
) => InternalState;
resetOnSavedSearchChange: (state: InternalState) => () => InternalState;
}

export type DiscoverInternalStateContainer = ReduxLikeStateContainer<
Expand All @@ -59,6 +67,7 @@ export function getInternalStateContainer() {
savedDataViews: [],
expandedDoc: undefined,
customFilters: [],
overriddenVisContextAfterInvalidation: undefined,
},
{
setDataView: (prevState: InternalState) => (nextDataView: DataView) => ({
Expand Down Expand Up @@ -112,6 +121,16 @@ export function getInternalStateContainer() {
...prevState,
customFilters,
}),
setOverriddenVisContextAfterInvalidation:
(prevState: InternalState) =>
(overriddenVisContextAfterInvalidation: UnifiedHistogramVisContext | {} | undefined) => ({
...prevState,
overriddenVisContextAfterInvalidation,
}),
resetOnSavedSearchChange: (prevState: InternalState) => () => ({
...prevState,
overriddenVisContextAfterInvalidation: undefined,
}),
},
{},
{ freeze: (state) => state }
Expand Down
Loading