From cdec372cf282f1e80b20b0f82f61c9b2fb3dc0ab Mon Sep 17 00:00:00 2001 From: Liza K Date: Tue, 28 Jan 2020 15:25:37 +0200 Subject: [PATCH 01/29] Clean up discover --- .../discover/np_ready/angular/discover.html | 17 +- .../discover/np_ready/angular/discover.js | 164 +++--------------- .../ui/search_bar/create_search_bar.tsx | 96 +++++++++- 3 files changed, 121 insertions(+), 156 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html index 45490ac7adc0f..ed000a2527ad9 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html @@ -5,24 +5,13 @@

{{screenTitle}}

diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js index dd782f97b075d..20ed0c0f9ac53 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js @@ -19,7 +19,8 @@ import _ from 'lodash'; import React from 'react'; -import { Subscription } from 'rxjs'; +import { Subscription, Subject, merge } from 'rxjs'; +import { debounceTime } from 'rxjs/operators'; import moment from 'moment'; import dateMath from '@elastic/datemath'; import { i18n } from '@kbn/i18n'; @@ -62,7 +63,6 @@ import { const { core, chrome, - data, docTitle, filterManager, share, @@ -79,8 +79,6 @@ import { import { getIndexPatternId } from '../helpers/get_index_pattern_id'; import { FilterStateManager } from '../../../../../data/public'; -const { getSavedQuery } = data.query.savedQueries; - const fetchStatuses = { UNINITIALIZED: 'uninitialized', LOADING: 'loading', @@ -205,8 +203,6 @@ function discoverController( const subscriptions = new Subscription(); - timefilter.disableTimeRangeSelector(); - timefilter.disableAutoRefreshSelector(); $scope.timefilterUpdateHandler = ranges => { timefilter.setTime({ from: moment(ranges.from).toISOString(), @@ -218,7 +214,6 @@ function discoverController( $scope.showInterval = false; $scope.minimumVisibleRows = 50; $scope.fetchStatus = fetchStatuses.UNINITIALIZED; - $scope.refreshInterval = timefilter.getRefreshInterval(); $scope.showSaveQuery = uiCapabilities.discover.saveQuery; $scope.$watch( @@ -436,15 +431,10 @@ function discoverController( let stateMonitor; const $state = ($scope.state = new AppState(getStateDefaults())); + const $fetchObservable = new Subject(); - $scope.filters = filterManager.getFilters(); $scope.screenTitle = savedSearch.title; - $scope.onFiltersUpdated = filters => { - // The filters will automatically be set when the filterManager emits an update event (see below) - filterManager.setFilters(filters); - }; - const getFieldCounts = async () => { // the field counts aren't set until we have the data back, // so we wait for the fetch to be done before proceeding @@ -571,17 +561,12 @@ function discoverController( }; const shouldSearchOnPageLoad = () => { - // If a saved query is referenced in the app state, omit the initial load because the saved query will - // be fetched separately and trigger a reload - if ($scope.state.savedQuery) { - return false; - } // A saved search is created on every page load, so we check the ID to see if we're loading a // previously saved search or if it is just transient return ( config.get('discover:searchOnPageLoad') || savedSearch.id !== undefined || - _.get($scope, 'refreshInterval.pause') === false + timefilter.getRefreshInterval().pause === false ); }; @@ -593,25 +578,23 @@ function discoverController( $scope.$on('$destroy', () => stateMonitor.destroy()); $scope.updateDataSource().then(function() { - subscriptions.add( - subscribeWithScope($scope, timefilter.getAutoRefreshFetch$(), { - next: $scope.fetch, - }) - ); + const searchBarChanges = merge( + timefilter.getAutoRefreshFetch$(), + timefilter.getFetch$(), + filterManager.getFetches$(), + $fetchObservable + ).pipe(debounceTime(100)); subscriptions.add( - subscribeWithScope($scope, timefilter.getRefreshIntervalUpdate$(), { - next: $scope.updateRefreshInterval, + subscribeWithScope($scope, searchBarChanges, { + next: $scope.fetch, }) ); subscriptions.add( subscribeWithScope($scope, timefilter.getTimeUpdate$(), { - next: $scope.updateTime, - }) - ); - subscriptions.add( - subscribeWithScope($scope, timefilter.getFetch$(), { - next: $scope.fetch, + next: () => { + $scope.updateTime(); + }, }) ); @@ -625,14 +608,14 @@ function discoverController( ); // if the searchSource doesn't know, tell it so - if (!angular.equals(sort, currentSort)) $scope.fetch(); + if (!angular.equals(sort, currentSort)) $fetchObservable.next(); }); // update data source when filters update + subscriptions.add( subscribeWithScope($scope, filterManager.getUpdates$(), { next: () => { - $scope.filters = filterManager.getFilters(); $scope.updateDataSource().then(function() { $state.save(); }); @@ -640,28 +623,15 @@ function discoverController( }) ); - // fetch data when filters fire fetch event - subscriptions.add( - subscribeWithScope($scope, filterManager.getFetches$(), { - next: $scope.fetch, - }) - ); - // update data source when hitting forward/back and the query changes $scope.$listen($state, 'fetch_with_changes', function(diff) { - if (diff.indexOf('query') >= 0) $scope.fetch(); + if (diff.indexOf('query') >= 0) $fetchObservable.next(); }); $scope.$watch('opts.timefield', function(timefield) { $scope.enableTimeRangeSelector = !!timefield; }); - $scope.$watch('state.interval', function(newInterval, oldInterval) { - if (newInterval !== oldInterval) { - $scope.fetch(); - } - }); - $scope.$watch('vis.aggs', function() { // no timefield, no vis, nothing to update if (!$scope.opts.timefield) return; @@ -677,7 +647,7 @@ function discoverController( if (!_.isEqual(newQuery, oldQuery)) { const query = migrateLegacyQuery(newQuery); if (!_.isEqual(query, newQuery)) { - $scope.updateQueryAndFetch({ query }); + $scope.updateQuery({ query }); } } }); @@ -737,7 +707,7 @@ function discoverController( $state.replace(); if (shouldSearchOnPageLoad()) { - $scope.fetch(); + $fetchObservable.next(); } }); }); @@ -830,14 +800,12 @@ function discoverController( }); }; - $scope.updateQueryAndFetch = function({ query, dateRange }) { - const oldDateRange = timefilter.getTime(); - timefilter.setTime(dateRange); - $state.query = query; + $scope.updateQuery = function({ query }) { // storing the updated timerange in the state will trigger a fetch // call automatically, so only trigger fetch in case this is a refresh call (no changes in parameters). - if (_.isEqual(oldDateRange, dateRange)) { - $scope.fetch(); + if (!_.isEqual($state.query, query)) { + $state.query = query; + $fetchObservable.next(); } }; @@ -899,32 +867,12 @@ function discoverController( from: dateMath.parse(timefilter.getTime().from), to: dateMath.parse(timefilter.getTime().to, { roundUp: true }), }; - $scope.time = timefilter.getTime(); }; $scope.toMoment = function(datetime) { return moment(datetime).format(config.get('dateFormat')); }; - $scope.updateRefreshInterval = function() { - const newInterval = timefilter.getRefreshInterval(); - const shouldFetch = - _.get($scope, 'refreshInterval.pause') === true && newInterval.pause === false; - - $scope.refreshInterval = newInterval; - - if (shouldFetch) { - $scope.fetch(); - } - }; - - $scope.onRefreshChange = function({ isPaused, refreshInterval }) { - timefilter.setRefreshInterval({ - pause: isPaused, - value: refreshInterval ? refreshInterval : $scope.refreshInterval.value, - }); - }; - $scope.resetQuery = function() { kbnUrl.change('/discover/{{id}}', { id: $route.current.params.id }); }; @@ -991,70 +939,6 @@ function discoverController( $scope.minimumVisibleRows = $scope.hits; }; - $scope.onQuerySaved = savedQuery => { - $scope.savedQuery = savedQuery; - }; - - $scope.onSavedQueryUpdated = savedQuery => { - $scope.savedQuery = { ...savedQuery }; - }; - - $scope.onClearSavedQuery = () => { - delete $scope.savedQuery; - delete $state.savedQuery; - $state.query = { - query: '', - language: localStorage.get('kibana.userQueryLanguage') || config.get('search:queryLanguage'), - }; - filterManager.setFilters(filterManager.getGlobalFilters()); - $state.save(); - $scope.fetch(); - }; - - const updateStateFromSavedQuery = savedQuery => { - $state.query = savedQuery.attributes.query; - $state.save(); - const savedQueryFilters = savedQuery.attributes.filters || []; - const globalFilters = filterManager.getGlobalFilters(); - filterManager.setFilters([...globalFilters, ...savedQueryFilters]); - - if (savedQuery.attributes.timefilter) { - timefilter.setTime({ - from: savedQuery.attributes.timefilter.from, - to: savedQuery.attributes.timefilter.to, - }); - if (savedQuery.attributes.timefilter.refreshInterval) { - timefilter.setRefreshInterval(savedQuery.attributes.timefilter.refreshInterval); - } - } - - $scope.fetch(); - }; - - $scope.$watch('savedQuery', newSavedQuery => { - if (!newSavedQuery) return; - - $state.savedQuery = newSavedQuery.id; - $state.save(); - - updateStateFromSavedQuery(newSavedQuery); - }); - - $scope.$watch('state.savedQuery', newSavedQueryId => { - if (!newSavedQueryId) { - $scope.savedQuery = undefined; - return; - } - if (!$scope.savedQuery || newSavedQueryId !== $scope.savedQuery.id) { - getSavedQuery(newSavedQueryId).then(savedQuery => { - $scope.$evalAsync(() => { - $scope.savedQuery = savedQuery; - updateStateFromSavedQuery(savedQuery); - }); - }); - } - }); - async function setupVisualization() { // If no timefield has been specified we don't create a histogram of messages if (!$scope.opts.timefield) return; diff --git a/src/plugins/data/public/ui/search_bar/create_search_bar.tsx b/src/plugins/data/public/ui/search_bar/create_search_bar.tsx index 6f1be2825dd01..9f3f61390848c 100644 --- a/src/plugins/data/public/ui/search_bar/create_search_bar.tsx +++ b/src/plugins/data/public/ui/search_bar/create_search_bar.tsx @@ -22,7 +22,7 @@ import { Subscription } from 'rxjs'; import { CoreStart } from 'src/core/public'; import { IStorageWrapper } from 'src/plugins/kibana_utils/public'; import { KibanaContextProvider } from '../../../../kibana_react/public'; -import { DataPublicPluginStart, esFilters } from '../..'; +import { DataPublicPluginStart, esFilters, Query, TimeRange, SavedQuery } from '../..'; import { QueryStart } from '../../query'; import { SearchBarOwnProps, SearchBar } from './search_bar'; @@ -52,6 +52,89 @@ const defaultOnRefreshChange = (query: QueryStart) => { }; }; +const defaultOnQuerySubmit = ( + props: StatefulSearchBarProps, + query: QueryStart, + setQueryStringState: Function +) => { + const { timefilter } = query.timefilter; + + return (payload: { dateRange: TimeRange; query?: Query }) => { + timefilter.setTime(payload.dateRange); + setQueryStringState(payload.query); + + if (props.onQuerySubmit) props.onQuerySubmit(payload); + }; +}; + +const defaultOnClearSavedQuery = ( + props: StatefulSearchBarProps, + uiSettings: CoreStart['uiSettings'], + query: QueryStart, + setQueryStringState: Function +) => { + return () => { + query.filterManager.removeAll(); + setQueryStringState({ + query: '', + language: uiSettings.get('search:queryLanguage'), + }); + + if (props.onClearSavedQuery) props.onClearSavedQuery(); + }; +}; + +const populateStateFromSavedQuery = ( + props: StatefulSearchBarProps, + query: QueryStart, + savedQuery: SavedQuery, + setQueryStringState: Function +) => { + const { timefilter } = query.timefilter; + // timefilter + if (savedQuery.attributes.timefilter) { + timefilter.setTime({ + from: savedQuery.attributes.timefilter.from, + to: savedQuery.attributes.timefilter.to, + }); + if (savedQuery.attributes.timefilter.refreshInterval) { + timefilter.setRefreshInterval(savedQuery.attributes.timefilter.refreshInterval); + } + } + + // query string + setQueryStringState(savedQuery.attributes.query); + if (props.onQuerySubmit) + props.onQuerySubmit({ dateRange: timefilter.getTime(), query: savedQuery.attributes.query }); + + // filters + const savedQueryFilters = savedQuery.attributes.filters || []; + const globalFilters = query.filterManager.getGlobalFilters(); + query.filterManager.setFilters([...globalFilters, ...savedQueryFilters]); +}; + +const defaultOnSavedQueryUpdated = ( + props: StatefulSearchBarProps, + query: QueryStart, + setQueryStringState: Function +) => { + return (savedQuery: SavedQuery) => { + populateStateFromSavedQuery(props, query, savedQuery, setQueryStringState); + if (props.onSavedQueryUpdated) props.onSavedQueryUpdated(savedQuery); + }; +}; + +const defaultOnQuerySaved = ( + props: StatefulSearchBarProps, + query: QueryStart, + setQueryStringState: Function +) => { + return (savedQuery: SavedQuery) => { + populateStateFromSavedQuery(props, query, savedQuery, setQueryStringState); + if (props.onSaved) props.onSaved(savedQuery); + }; +}; + export function createSearchBar({ core, storage, data }: StatefulSearchBarDeps) { // App name should come from the core application service. // Until it's available, we'll ask the user to provide it for the pre-wired component. @@ -61,6 +144,10 @@ export function createSearchBar({ core, storage, data }: StatefulSearchBarDeps) const fmFilters = filterManager.getFilters(); const [refreshInterval, setRefreshInterval] = useState(tfRefreshInterval.value); const [refreshPaused, setRefreshPaused] = useState(tfRefreshInterval.pause); + const [query, setQuery] = useState({ + query: props.query?.query || '', + language: props.query?.language || core.uiSettings.get('search:queryLanguage'), + }); const [filters, setFilters] = useState(fmFilters); @@ -110,15 +197,20 @@ export function createSearchBar({ core, storage, data }: StatefulSearchBarDeps) }} > ); From d82bebb1671ea5d316279fe89244bc929a11117d Mon Sep 17 00:00:00 2001 From: Liza K Date: Tue, 28 Jan 2020 16:13:12 +0200 Subject: [PATCH 02/29] Clean up visualize --- .../discover/np_ready/angular/discover.html | 2 +- .../discover/np_ready/angular/discover.js | 8 +- .../visualize/np_ready/editor/editor.html | 15 +- .../visualize/np_ready/editor/editor.js | 153 +++--------------- .../ui/search_bar/create_search_bar.tsx | 40 ++--- 5 files changed, 46 insertions(+), 172 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html index ed000a2527ad9..1276b9ed23970 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.html @@ -11,7 +11,7 @@

{{screenTitle}}

show-save-query="showSaveQuery" on-query-submit="updateQuery" index-patterns="[indexPattern]" - query="state.query" + query="state.query" > diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js index 20ed0c0f9ac53..9f39e205d7894 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js @@ -801,12 +801,8 @@ function discoverController( }; $scope.updateQuery = function({ query }) { - // storing the updated timerange in the state will trigger a fetch - // call automatically, so only trigger fetch in case this is a refresh call (no changes in parameters). - if (!_.isEqual($state.query, query)) { - $state.query = query; - $fetchObservable.next(); - } + $state.query = query; + $fetchObservable.next(); }; function onResults(resp) { diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html index 6190b92c9be3e..15b0cead96b3b 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.html @@ -42,22 +42,11 @@ show-filter-bar="showFilterBar() && isVisible" show-date-picker="showQueryBarTimePicker()" show-auto-refresh-only="!showQueryBarTimePicker()" + show-save-query="showSaveQuery" query="state.query" - saved-query="savedQuery" screen-title="state.vis.title" - on-query-submit="updateQueryAndFetch" + on-query-submit="updateQuery" index-patterns="[indexPattern]" - filters="filters" - on-filters-updated="onFiltersUpdated" - date-range-from="timeRange.from" - date-range-to="timeRange.to" - is-refresh-paused="refreshInterval.pause" - refresh-interval="refreshInterval.value" - on-refresh-change="onRefreshChange" - show-save-query="showSaveQuery" - on-saved="onQuerySaved" - on-saved-query-updated="onSavedQueryUpdated" - on-clear-saved-query="onClearSavedQuery" > diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js index 2a4fdeb4e4016..032839433e95d 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/editor/editor.js @@ -19,7 +19,8 @@ import angular from 'angular'; import _ from 'lodash'; -import { Subscription } from 'rxjs'; +import { Subscription, Subject, merge } from 'rxjs'; +import { debounceTime } from 'rxjs/operators'; import { i18n } from '@kbn/i18n'; import React from 'react'; @@ -92,12 +93,11 @@ function VisualizeAppController( chrome, getBasePath, core: { docLinks }, - savedQueryService, uiSettings, } = getServices(); const filterStateManager = new FilterStateManager(globalState, getAppState, filterManager); - const queryFilter = filterManager; + const $fetchObservable = new Subject(); // Retrieve the resolved SavedVis instance. const savedVis = $route.current.locals.savedVis; const _applyVis = () => { @@ -311,13 +311,6 @@ function VisualizeAppController( return appState; })(); - $scope.filters = queryFilter.getFilters(); - - $scope.onFiltersUpdated = filters => { - // The filters will automatically be set when the queryFilter emits an update event (see below) - queryFilter.setFilters(filters); - }; - $scope.showSaveQuery = visualizeCapabilities.saveQuery; $scope.$watch( @@ -340,7 +333,6 @@ function VisualizeAppController( $scope.searchSource = searchSource; $scope.state = $state; - $scope.refreshInterval = timefilter.getRefreshInterval(); // Create a PersistedState instance. $scope.uiState = $state.makeStateful('uiState'); @@ -367,7 +359,6 @@ function VisualizeAppController( return vis.type.options.showTimePicker && hasTimeField; }; - $scope.timeRange = timefilter.getTime(); $scope.opts = _.pick($scope, 'savedVis', 'isAddToDashMode'); stateMonitor = stateMonitorFactory.create($state, stateDefaults); @@ -375,38 +366,24 @@ function VisualizeAppController( $appStatus.dirty = status.dirty || !savedVis.id; }); + $scope.updateQuery = function({ query }) { + $state.query = query; + $fetchObservable.next(); + }; + $scope.$watch('state.query', (newQuery, oldQuery) => { if (!_.isEqual(newQuery, oldQuery)) { const query = migrateLegacyQuery(newQuery); if (!_.isEqual(query, newQuery)) { - $state.query = query; + $scope.updateQuery({ query }); } - $scope.fetch(); } }); $state.replace(); - const updateTimeRange = () => { - $scope.timeRange = timefilter.getTime(); - $scope.$broadcast('render'); - }; - const subscriptions = new Subscription(); - subscriptions.add( - subscribeWithScope($scope, timefilter.getRefreshIntervalUpdate$(), { - next: () => { - $scope.refreshInterval = timefilter.getRefreshInterval(); - }, - }) - ); - subscriptions.add( - subscribeWithScope($scope, timefilter.getTimeUpdate$(), { - next: updateTimeRange, - }) - ); - subscriptions.add( chrome.getIsVisible$().subscribe(isVisible => { $scope.$evalAsync(() => { @@ -420,28 +397,22 @@ function VisualizeAppController( $state.save(); $scope.query = $state.query; savedVis.searchSource.setField('query', $state.query); - savedVis.searchSource.setField('filter', $state.filters); - $scope.$broadcast('render'); + savedVis.searchSource.setField('filter', filterManager.getFilters()); }; // update the searchSource when filters update - subscriptions.add( - subscribeWithScope($scope, queryFilter.getUpdates$(), { - next: () => { - $scope.filters = queryFilter.getFilters(); - $scope.globalFilters = queryFilter.getGlobalFilters(); - }, - }) - ); - subscriptions.add( - subscribeWithScope($scope, queryFilter.getFetches$(), { - next: $scope.fetch, - }) - ); + + const searchBarChanges$ = merge( + timefilter.getAutoRefreshFetch$(), + timefilter.getFetch$(), + filterManager.getFetches$(), + $fetchObservable + ).pipe(debounceTime(100)); subscriptions.add( - subscribeWithScope($scope, timefilter.getAutoRefreshFetch$(), { + subscribeWithScope($scope, searchBarChanges$, { next: () => { + $scope.fetch(); $scope.vis.forceReload(); }, }) @@ -463,92 +434,6 @@ function VisualizeAppController( }); } - $scope.updateQueryAndFetch = function({ query, dateRange }) { - const isUpdate = - (query && !_.isEqual(query, $state.query)) || - (dateRange && !_.isEqual(dateRange, $scope.timeRange)); - - $state.query = query; - timefilter.setTime(dateRange); - - // If nothing has changed, trigger the fetch manually, otherwise it will happen as a result of the changes - if (!isUpdate) { - $scope.vis.forceReload(); - } - }; - - $scope.onRefreshChange = function({ isPaused, refreshInterval }) { - timefilter.setRefreshInterval({ - pause: isPaused, - value: refreshInterval ? refreshInterval : $scope.refreshInterval.value, - }); - }; - - $scope.onQuerySaved = savedQuery => { - $scope.savedQuery = savedQuery; - }; - - $scope.onSavedQueryUpdated = savedQuery => { - $scope.savedQuery = { ...savedQuery }; - }; - - $scope.onClearSavedQuery = () => { - delete $scope.savedQuery; - delete $state.savedQuery; - $state.query = { - query: '', - language: - localStorage.get('kibana.userQueryLanguage') || uiSettings.get('search:queryLanguage'), - }; - queryFilter.setFilters(queryFilter.getGlobalFilters()); - $state.save(); - $scope.fetch(); - }; - - const updateStateFromSavedQuery = savedQuery => { - $state.query = savedQuery.attributes.query; - $state.save(); - - const savedQueryFilters = savedQuery.attributes.filters || []; - const globalFilters = queryFilter.getGlobalFilters(); - queryFilter.setFilters([...globalFilters, ...savedQueryFilters]); - - if (savedQuery.attributes.timefilter) { - timefilter.setTime({ - from: savedQuery.attributes.timefilter.from, - to: savedQuery.attributes.timefilter.to, - }); - if (savedQuery.attributes.timefilter.refreshInterval) { - timefilter.setRefreshInterval(savedQuery.attributes.timefilter.refreshInterval); - } - } - - $scope.fetch(); - }; - - $scope.$watch('savedQuery', newSavedQuery => { - if (!newSavedQuery) return; - $state.savedQuery = newSavedQuery.id; - $state.save(); - - updateStateFromSavedQuery(newSavedQuery); - }); - - $scope.$watch('state.savedQuery', newSavedQueryId => { - if (!newSavedQueryId) { - $scope.savedQuery = undefined; - return; - } - if (!$scope.savedQuery || newSavedQueryId !== $scope.savedQuery.id) { - savedQueryService.getSavedQuery(newSavedQueryId).then(savedQuery => { - $scope.$evalAsync(() => { - $scope.savedQuery = savedQuery; - updateStateFromSavedQuery(savedQuery); - }); - }); - } - }); - /** * Called when the user clicks "Save" button. */ diff --git a/src/plugins/data/public/ui/search_bar/create_search_bar.tsx b/src/plugins/data/public/ui/search_bar/create_search_bar.tsx index 9f3f61390848c..081ce2d186f12 100644 --- a/src/plugins/data/public/ui/search_bar/create_search_bar.tsx +++ b/src/plugins/data/public/ui/search_bar/create_search_bar.tsx @@ -36,14 +36,14 @@ export type StatefulSearchBarProps = SearchBarOwnProps & { appName: string; }; -const defaultFiltersUpdated = (query: QueryStart) => { +const defaultFiltersUpdated = (queryService: QueryStart) => { return (filters: esFilters.Filter[]) => { - query.filterManager.setFilters(filters); + queryService.filterManager.setFilters(filters); }; }; -const defaultOnRefreshChange = (query: QueryStart) => { - const { timefilter } = query.timefilter; +const defaultOnRefreshChange = (queryService: QueryStart) => { + const { timefilter } = queryService.timefilter; return (options: { isPaused: boolean; refreshInterval: number }) => { timefilter.setRefreshInterval({ value: options.refreshInterval, @@ -54,27 +54,31 @@ const defaultOnRefreshChange = (query: QueryStart) => { const defaultOnQuerySubmit = ( props: StatefulSearchBarProps, - query: QueryStart, + queryService: QueryStart, + currentQuery: Query, setQueryStringState: Function ) => { - const { timefilter } = query.timefilter; + const { timefilter } = queryService.timefilter; return (payload: { dateRange: TimeRange; query?: Query }) => { + const isUpdate = + !_.isEqual(timefilter.getTime(), payload.dateRange) || + !_.isEqual(payload.query, currentQuery); timefilter.setTime(payload.dateRange); setQueryStringState(payload.query); - if (props.onQuerySubmit) props.onQuerySubmit(payload); + if (props.onQuerySubmit && isUpdate) props.onQuerySubmit(payload); }; }; const defaultOnClearSavedQuery = ( props: StatefulSearchBarProps, uiSettings: CoreStart['uiSettings'], - query: QueryStart, + queryService: QueryStart, setQueryStringState: Function ) => { return () => { - query.filterManager.removeAll(); + queryService.filterManager.removeAll(); setQueryStringState({ query: '', language: uiSettings.get('search:queryLanguage'), @@ -86,11 +90,11 @@ const defaultOnClearSavedQuery = ( const populateStateFromSavedQuery = ( props: StatefulSearchBarProps, - query: QueryStart, + queryService: QueryStart, savedQuery: SavedQuery, setQueryStringState: Function ) => { - const { timefilter } = query.timefilter; + const { timefilter } = queryService.timefilter; // timefilter if (savedQuery.attributes.timefilter) { timefilter.setTime({ @@ -109,28 +113,28 @@ const populateStateFromSavedQuery = ( // filters const savedQueryFilters = savedQuery.attributes.filters || []; - const globalFilters = query.filterManager.getGlobalFilters(); - query.filterManager.setFilters([...globalFilters, ...savedQueryFilters]); + const globalFilters = queryService.filterManager.getGlobalFilters(); + queryService.filterManager.setFilters([...globalFilters, ...savedQueryFilters]); }; const defaultOnSavedQueryUpdated = ( props: StatefulSearchBarProps, - query: QueryStart, + queryService: QueryStart, setQueryStringState: Function ) => { return (savedQuery: SavedQuery) => { - populateStateFromSavedQuery(props, query, savedQuery, setQueryStringState); + populateStateFromSavedQuery(props, queryService, savedQuery, setQueryStringState); if (props.onSavedQueryUpdated) props.onSavedQueryUpdated(savedQuery); }; }; const defaultOnQuerySaved = ( props: StatefulSearchBarProps, - query: QueryStart, + queryService: QueryStart, setQueryStringState: Function ) => { return (savedQuery: SavedQuery) => { - populateStateFromSavedQuery(props, query, savedQuery, setQueryStringState); + populateStateFromSavedQuery(props, queryService, savedQuery, setQueryStringState); if (props.onSaved) props.onSaved(savedQuery); }; }; @@ -207,7 +211,7 @@ export function createSearchBar({ core, storage, data }: StatefulSearchBarDeps) query={query} onFiltersUpdated={defaultFiltersUpdated(data.query)} onRefreshChange={defaultOnRefreshChange(data.query)} - onQuerySubmit={defaultOnQuerySubmit(props, data.query, setQuery)} + onQuerySubmit={defaultOnQuerySubmit(props, data.query, query, setQuery)} onClearSavedQuery={defaultOnClearSavedQuery(props, core.uiSettings, data.query, setQuery)} onSavedQueryUpdated={defaultOnSavedQueryUpdated(props, data.query, setQuery)} onSaved={defaultOnQuerySaved(props, data.query, setQuery)} From 732cb7d63cc096846ca3ed6add47010820f22733 Mon Sep 17 00:00:00 2001 From: Liza K Date: Tue, 28 Jan 2020 17:35:22 +0200 Subject: [PATCH 03/29] Clean up dashboard --- .../dashboard/np_ready/dashboard_app.html | 16 +-- .../dashboard/np_ready/dashboard_app.tsx | 12 -- .../np_ready/dashboard_app_controller.tsx | 107 ++---------------- .../np_ready/angular/context_app.html | 4 +- .../visualize/np_ready/editor/editor.html | 3 +- .../ui/search_bar/create_search_bar.tsx | 19 +++- 6 files changed, 25 insertions(+), 136 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/dashboard/np_ready/dashboard_app.html b/src/legacy/core_plugins/kibana/public/dashboard/np_ready/dashboard_app.html index 3cf8932958b6d..30382f460cd4c 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/np_ready/dashboard_app.html +++ b/src/legacy/core_plugins/kibana/public/dashboard/np_ready/dashboard_app.html @@ -13,20 +13,9 @@ show-save-query="showSaveQuery" query="model.query" - saved-query="savedQuery" screen-title="screenTitle" on-query-submit="updateQueryAndFetch" - index-patterns="indexPatterns" - filters="model.filters" - on-filters-updated="onFiltersUpdated" - date-range-from="model.timeRange.from" - date-range-to="model.timeRange.to" - is-refresh-paused="model.refreshInterval.pause" - refresh-interval="model.refreshInterval.value" - on-saved="onQuerySaved" - on-saved-query-updated="onSavedQueryUpdated" - on-clear-saved-query="onClearSavedQuery" - on-refresh-change="onRefreshChange"> + index-patterns="indexPatterns">