From c92d8235a20ac98f10ff2ec5afcfa2d5111bb93f Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Sun, 6 Oct 2019 14:12:52 +0300 Subject: [PATCH] Remove ui/persisted_log - Part 2 (#47236) * Added storage interface to avoid importing ui/storage * Move persisted log into data plugin / query * Fix jest tests * import getQueryLog * Add store to graph search bar * Added window localStorage * Deleted persisted log folder --- src/legacy/core_plugins/data/public/plugin.ts | 1 + .../data/public/query}/persisted_log/index.ts | 3 +-- .../persisted_log/persisted_log.test.ts | 0 .../query}/persisted_log/persisted_log.ts | 6 ++--- .../components/query_bar_input.test.mocks.ts | 2 +- .../query_bar/components/query_bar_input.tsx | 11 +++++----- .../components/query_bar_top_row.tsx | 7 +++--- .../query/query_bar/lib/get_query_log.ts | 16 +++++++++----- .../components/create_search_bar.tsx | 2 +- .../public/shim/legacy_dependencies_plugin.ts | 2 ++ .../data/public/timefilter/time_history.ts | 7 +++--- .../public/timefilter/timefilter_service.ts | 6 +++-- src/legacy/core_plugins/data/public/types.ts | 7 ++++++ .../ui/public/agg_types/buckets/filters.js | 4 +++- .../public/persisted_log/recently_accessed.ts | 22 ------------------- .../public/components/search_bar.test.tsx | 3 +++ 16 files changed, 49 insertions(+), 50 deletions(-) rename src/legacy/{ui/public => core_plugins/data/public/query}/persisted_log/index.ts (88%) rename src/legacy/{ui/public => core_plugins/data/public/query}/persisted_log/persisted_log.test.ts (100%) rename src/legacy/{ui/public => core_plugins/data/public/query}/persisted_log/persisted_log.ts (95%) delete mode 100644 src/legacy/ui/public/persisted_log/recently_accessed.ts diff --git a/src/legacy/core_plugins/data/public/plugin.ts b/src/legacy/core_plugins/data/public/plugin.ts index a5aa55673cac6..b487651175d47 100644 --- a/src/legacy/core_plugins/data/public/plugin.ts +++ b/src/legacy/core_plugins/data/public/plugin.ts @@ -107,6 +107,7 @@ export class DataPlugin const timefilterService = this.timefilter.setup({ uiSettings, + store: __LEGACY.storage, }); this.setupApi = { indexPatterns: indexPatternsService, diff --git a/src/legacy/ui/public/persisted_log/index.ts b/src/legacy/core_plugins/data/public/query/persisted_log/index.ts similarity index 88% rename from src/legacy/ui/public/persisted_log/index.ts rename to src/legacy/core_plugins/data/public/query/persisted_log/index.ts index 52de69b9c50bf..9b21c748da02d 100644 --- a/src/legacy/ui/public/persisted_log/index.ts +++ b/src/legacy/core_plugins/data/public/query/persisted_log/index.ts @@ -17,5 +17,4 @@ * under the License. */ -export { PersistedLog } from './persisted_log'; -export { recentlyAccessed } from './recently_accessed'; +export * from './persisted_log'; diff --git a/src/legacy/ui/public/persisted_log/persisted_log.test.ts b/src/legacy/core_plugins/data/public/query/persisted_log/persisted_log.test.ts similarity index 100% rename from src/legacy/ui/public/persisted_log/persisted_log.test.ts rename to src/legacy/core_plugins/data/public/query/persisted_log/persisted_log.test.ts diff --git a/src/legacy/ui/public/persisted_log/persisted_log.ts b/src/legacy/core_plugins/data/public/query/persisted_log/persisted_log.ts similarity index 95% rename from src/legacy/ui/public/persisted_log/persisted_log.ts rename to src/legacy/core_plugins/data/public/query/persisted_log/persisted_log.ts index 0824d17757311..e0e6a0d0c44e4 100644 --- a/src/legacy/ui/public/persisted_log/persisted_log.ts +++ b/src/legacy/core_plugins/data/public/query/persisted_log/persisted_log.ts @@ -20,9 +20,7 @@ import _ from 'lodash'; import * as Rx from 'rxjs'; import { map } from 'rxjs/operators'; -import { Storage } from 'ui/storage'; - -const localStorage = new Storage(window.localStorage); +import { Storage } from '../../types'; const defaultIsDuplicate = (oldItem: any, newItem: any) => { return _.isEqual(oldItem, newItem); @@ -44,7 +42,7 @@ export class PersistedLog { private update$ = new Rx.BehaviorSubject(undefined); - constructor(name: string, options: PersistedLogOptions = {}, storage = localStorage) { + constructor(name: string, options: PersistedLogOptions = {}, storage: Storage) { this.name = name; this.maxLength = typeof options.maxLength === 'string' diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.mocks.ts b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.mocks.ts index 683ced28dba97..80ee38ea1b076 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.mocks.ts +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.test.mocks.ts @@ -45,7 +45,7 @@ export const mockFetchIndexPatterns = jest .fn() .mockReturnValue(Promise.resolve([mockIndexPattern])); -jest.mock('ui/persisted_log', () => ({ +jest.mock('../../persisted_log', () => ({ PersistedLog: mockPersistedLogFactory, })); diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx index 3cdd8d4b9c40c..685a793b4de3d 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx @@ -25,7 +25,6 @@ import { EuiFieldText, EuiOutsideClickDetector, PopoverAnchorPosition } from '@e import { InjectedIntl, injectI18n } from '@kbn/i18n/react'; import { debounce, compact, isEqual } from 'lodash'; -import { PersistedLog } from 'ui/persisted_log'; import { AutocompleteSuggestion, @@ -36,11 +35,11 @@ import { KibanaReactContextValue, } from '../../../../../../../plugins/kibana_react/public'; import { IndexPattern, StaticIndexPattern } from '../../../index_patterns'; -import { Query } from '../index'; +import { Query, getQueryLog } from '../index'; import { fromUser, matchPairs, toUser } from '../lib'; import { QueryLanguageSwitcher } from './language_switcher'; import { SuggestionsComponent } from './typeahead/suggestions_component'; -import { getQueryLog } from '../lib/get_query_log'; +import { PersistedLog } from '../../persisted_log'; import { fetchIndexPatterns } from '../lib/fetch_index_patterns'; import { IDataPluginServices } from '../../../types'; @@ -392,6 +391,7 @@ export class QueryBarInputUI extends Component { }; public componentDidMount() { + const { uiSettings, store, appName } = this.services; const parsedQuery = fromUser(toUser(this.props.query.query)); if (!isEqual(this.props.query.query, parsedQuery)) { this.onChange({ ...this.props.query, query: parsedQuery }); @@ -399,12 +399,13 @@ export class QueryBarInputUI extends Component { this.persistedLog = this.props.persistedLog ? this.props.persistedLog - : getQueryLog(this.services.uiSettings, this.services.appName, this.props.query.language); + : getQueryLog(uiSettings, store, appName, this.props.query.language); this.fetchIndexPatterns().then(this.updateSuggestions); } public componentDidUpdate(prevProps: Props) { + const { uiSettings, store, appName } = this.services; const parsedQuery = fromUser(toUser(this.props.query.query)); if (!isEqual(this.props.query.query, parsedQuery)) { this.onChange({ ...this.props.query, query: parsedQuery }); @@ -412,7 +413,7 @@ export class QueryBarInputUI extends Component { this.persistedLog = this.props.persistedLog ? this.props.persistedLog - : getQueryLog(this.services.uiSettings, this.services.appName, this.props.query.language); + : getQueryLog(uiSettings, store, appName, this.props.query.language); if (!isEqual(prevProps.indexPatterns, this.props.indexPatterns)) { this.fetchIndexPatterns().then(this.updateSuggestions); diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx index 6895c9ecd018c..716bb677b946f 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_top_row.tsx @@ -22,7 +22,6 @@ import { doesKueryExpressionHaveLuceneSyntaxError } from '@kbn/es-query'; import classNames from 'classnames'; import React, { useState, useEffect } from 'react'; import { documentationLinks } from 'ui/documentation_links'; -import { PersistedLog } from 'ui/persisted_log'; import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiLink, EuiSuperDatePicker } from '@elastic/eui'; // @ts-ignore @@ -34,10 +33,10 @@ import { useKibana } from '../../../../../../../plugins/kibana_react/public'; import { IndexPattern } from '../../../index_patterns'; import { QueryBarInput } from './query_bar_input'; -import { getQueryLog } from '../lib/get_query_log'; -import { Query } from '../index'; +import { Query, getQueryLog } from '../index'; import { TimeHistoryContract } from '../../../timefilter'; import { IDataPluginServices } from '../../../types'; +import { PersistedLog } from '../../persisted_log'; interface Props { query?: Query; @@ -72,7 +71,7 @@ function QueryBarTopRowUI(props: Props) { useEffect(() => { if (!props.query) return; - persistedLog = getQueryLog(uiSettings!, appName, props.query.language); + persistedLog = getQueryLog(uiSettings!, store, appName, props.query.language); }, [queryLanguage]); function onClickSubmitButton(event: React.MouseEvent) { diff --git a/src/legacy/core_plugins/data/public/query/query_bar/lib/get_query_log.ts b/src/legacy/core_plugins/data/public/query/query_bar/lib/get_query_log.ts index 70dc1d3fe700e..8b26e14c6ed7b 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/lib/get_query_log.ts +++ b/src/legacy/core_plugins/data/public/query/query_bar/lib/get_query_log.ts @@ -17,16 +17,22 @@ * under the License. */ -import { PersistedLog } from 'ui/persisted_log'; import { UiSettingsClientContract } from 'src/core/public'; +import { PersistedLog } from '../../persisted_log'; +import { Storage } from '../../../types'; export function getQueryLog( uiSettings: UiSettingsClientContract, + store: Storage, appName: string, language: string ) { - return new PersistedLog(`typeahead:${appName}-${language}`, { - maxLength: uiSettings.get('history:limit'), - filterDuplicates: true, - }); + return new PersistedLog( + `typeahead:${appName}-${language}`, + { + maxLength: uiSettings.get('history:limit'), + filterDuplicates: true, + }, + store + ); } diff --git a/src/legacy/core_plugins/data/public/search/search_bar/components/create_search_bar.tsx b/src/legacy/core_plugins/data/public/search/search_bar/components/create_search_bar.tsx index add49e47971d3..bf40cb8e7e0b6 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/components/create_search_bar.tsx +++ b/src/legacy/core_plugins/data/public/search/search_bar/components/create_search_bar.tsx @@ -20,8 +20,8 @@ import React from 'react'; import { Filter } from '@kbn/es-query'; import { CoreStart } from 'src/core/public'; -import { Storage } from 'ui/storage'; import { AutocompletePublicPluginStart } from 'src/plugins/data/public'; +import { Storage } from '../../../types'; import { KibanaContextProvider } from '../../../../../../../../src/plugins/kibana_react/public'; import { TimefilterSetup } from '../../../timefilter'; import { FilterManager, SearchBar } from '../../../'; diff --git a/src/legacy/core_plugins/data/public/shim/legacy_dependencies_plugin.ts b/src/legacy/core_plugins/data/public/shim/legacy_dependencies_plugin.ts index 126754388f13f..511482c6239fb 100644 --- a/src/legacy/core_plugins/data/public/shim/legacy_dependencies_plugin.ts +++ b/src/legacy/core_plugins/data/public/shim/legacy_dependencies_plugin.ts @@ -25,6 +25,7 @@ import { initLegacyModule } from './legacy_module'; /** @internal */ export interface LegacyDependenciesPluginSetup { savedObjectsClient: any; + storage: Storage; } export interface LegacyDependenciesPluginStart { @@ -37,6 +38,7 @@ export class LegacyDependenciesPlugin implements Plugin { return { savedObjectsClient: chrome.getSavedObjectsClient(), + storage: new Storage(window.localStorage), } as LegacyDependenciesPluginSetup; } diff --git a/src/legacy/core_plugins/data/public/timefilter/time_history.ts b/src/legacy/core_plugins/data/public/timefilter/time_history.ts index 7dcd5843ae530..22778d1adea3c 100644 --- a/src/legacy/core_plugins/data/public/timefilter/time_history.ts +++ b/src/legacy/core_plugins/data/public/timefilter/time_history.ts @@ -19,12 +19,13 @@ import moment from 'moment'; import { TimeRange } from 'src/plugins/data/public'; -import { PersistedLog } from 'ui/persisted_log'; +import { PersistedLog } from '../query/persisted_log'; +import { Storage } from '../types'; export class TimeHistory { private history: PersistedLog; - constructor() { + constructor(store: Storage) { const historyOptions = { maxLength: 10, filterDuplicates: true, @@ -32,7 +33,7 @@ export class TimeHistory { return oldItem.from === newItem.from && oldItem.to === newItem.to; }, }; - this.history = new PersistedLog('kibana.timepicker.timeHistory', historyOptions); + this.history = new PersistedLog('kibana.timepicker.timeHistory', historyOptions, store); } add(time: TimeRange) { diff --git a/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts index 96c490a195d3d..cda9b93ef08aa 100644 --- a/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter_service.ts @@ -19,6 +19,7 @@ import { UiSettingsClientContract } from 'src/core/public'; import { TimeHistory, Timefilter, TimeHistoryContract, TimefilterContract } from './index'; +import { Storage } from '../types'; /** * Filter Service @@ -27,15 +28,16 @@ import { TimeHistory, Timefilter, TimeHistoryContract, TimefilterContract } from export interface TimeFilterServiceDependencies { uiSettings: UiSettingsClientContract; + store: Storage; } export class TimefilterService { - public setup({ uiSettings }: TimeFilterServiceDependencies): TimefilterSetup { + public setup({ uiSettings, store }: TimeFilterServiceDependencies): TimefilterSetup { const timefilterConfig = { timeDefaults: uiSettings.get('timepicker:timeDefaults'), refreshIntervalDefaults: uiSettings.get('timepicker:refreshIntervalDefaults'), }; - const history = new TimeHistory(); + const history = new TimeHistory(store); const timefilter = new Timefilter(timefilterConfig, history); return { diff --git a/src/legacy/core_plugins/data/public/types.ts b/src/legacy/core_plugins/data/public/types.ts index 4b7a5c1402ea7..0ef7e483dd63f 100644 --- a/src/legacy/core_plugins/data/public/types.ts +++ b/src/legacy/core_plugins/data/public/types.ts @@ -20,6 +20,13 @@ import { UiSettingsClientContract, CoreStart } from 'src/core/public'; import { AutocompletePublicPluginStart } from 'src/plugins/data/public'; +export interface Storage { + get: (key: string) => any; + set: (key: string, value: any) => void; + remove: (key: string) => any; + clear: () => void; +} + export interface IDataPluginServices extends Partial { appName: string; uiSettings: UiSettingsClientContract; diff --git a/src/legacy/ui/public/agg_types/buckets/filters.js b/src/legacy/ui/public/agg_types/buckets/filters.js index 9b7795d8fd9fa..19be75ccf8c1e 100644 --- a/src/legacy/ui/public/agg_types/buckets/filters.js +++ b/src/legacy/ui/public/agg_types/buckets/filters.js @@ -24,6 +24,7 @@ import { BucketAggType } from './_bucket_agg_type'; import { createFilterFilters } from './create_filter/filters'; import { FiltersParamEditor } from '../../vis/editors/default/controls/filters'; import { i18n } from '@kbn/i18n'; +import { Storage } from 'ui/storage'; import chrome from 'ui/chrome'; import { buildEsQuery } from '@kbn/es-query'; @@ -31,6 +32,7 @@ import { setup as data } from '../../../../core_plugins/data/public/legacy'; const { getQueryLog } = data.query.helpers; const config = chrome.getUiSettingsClient(); +const storage = new Storage(window.localStorage); export const filtersBucketAgg = new BucketAggType({ name: 'filters', @@ -50,7 +52,7 @@ export const filtersBucketAgg = new BucketAggType({ if (!_.size(inFilters)) return; inFilters.forEach((filter) => { - const persistedLog = getQueryLog(config, 'filtersAgg', filter.input.language); + const persistedLog = getQueryLog(config, storage, 'filtersAgg', filter.input.language); persistedLog.add(filter.input.query); }); diff --git a/src/legacy/ui/public/persisted_log/recently_accessed.ts b/src/legacy/ui/public/persisted_log/recently_accessed.ts deleted file mode 100644 index 6d984d155d551..0000000000000 --- a/src/legacy/ui/public/persisted_log/recently_accessed.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { npStart } from '../new_platform'; - -export const recentlyAccessed = npStart.core.chrome.recentlyAccessed; diff --git a/x-pack/legacy/plugins/graph/public/components/search_bar.test.tsx b/x-pack/legacy/plugins/graph/public/components/search_bar.test.tsx index 3c37c77e6d450..760b2aba5ba16 100644 --- a/x-pack/legacy/plugins/graph/public/components/search_bar.test.tsx +++ b/x-pack/legacy/plugins/graph/public/components/search_bar.test.tsx @@ -40,6 +40,9 @@ function wrapSearchBarInContext(testProps: OuterSearchBarProps) { notifications: {} as CoreStart['notifications'], http: {} as CoreStart['http'], overlays: {} as CoreStart['overlays'], + store: { + get: () => {}, + }, }; return (