Skip to content

Commit

Permalink
Remove ui/persisted_log - Part 2 (#47236)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Liza Katz committed Oct 6, 2019
1 parent 35751f9 commit 2389fe8
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/legacy/core_plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class DataPlugin

const timefilterService = this.timefilter.setup({
uiSettings,
store: __LEGACY.storage,
});
this.setupApi = {
indexPatterns: indexPatternsService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
* under the License.
*/

export { PersistedLog } from './persisted_log';
export { recentlyAccessed } from './recently_accessed';
export * from './persisted_log';
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -44,7 +42,7 @@ export class PersistedLog<T = any> {

private update$ = new Rx.BehaviorSubject(undefined);

constructor(name: string, options: PersistedLogOptions<T> = {}, storage = localStorage) {
constructor(name: string, options: PersistedLogOptions<T> = {}, storage: Storage) {
this.name = name;
this.maxLength =
typeof options.maxLength === 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const mockFetchIndexPatterns = jest
.fn()
.mockReturnValue(Promise.resolve([mockIndexPattern]));

jest.mock('ui/persisted_log', () => ({
jest.mock('../../persisted_log', () => ({
PersistedLog: mockPersistedLogFactory,
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -392,27 +391,29 @@ export class QueryBarInputUI extends Component<Props, State> {
};

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 });
}

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 });
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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<HTMLButtonElement>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 '../../../';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { initLegacyModule } from './legacy_module';
/** @internal */
export interface LegacyDependenciesPluginSetup {
savedObjectsClient: any;
storage: Storage;
}

export interface LegacyDependenciesPluginStart {
Expand All @@ -37,6 +38,7 @@ export class LegacyDependenciesPlugin implements Plugin<any, any> {

return {
savedObjectsClient: chrome.getSavedObjectsClient(),
storage: new Storage(window.localStorage),
} as LegacyDependenciesPluginSetup;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@

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<TimeRange>;

constructor() {
constructor(store: Storage) {
const historyOptions = {
maxLength: 10,
filterDuplicates: true,
isDuplicate: (oldItem: TimeRange, newItem: TimeRange) => {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { UiSettingsClientContract } from 'src/core/public';
import { TimeHistory, Timefilter, TimeHistoryContract, TimefilterContract } from './index';
import { Storage } from '../types';

/**
* Filter Service
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/legacy/core_plugins/data/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoreStart> {
appName: string;
uiSettings: UiSettingsClientContract;
Expand Down
4 changes: 3 additions & 1 deletion src/legacy/ui/public/agg_types/buckets/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ 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';
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',
Expand All @@ -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);
});

Expand Down
22 changes: 0 additions & 22 deletions src/legacy/ui/public/persisted_log/recently_accessed.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ function wrapSearchBarInContext(testProps: OuterSearchBarProps) {
notifications: {} as CoreStart['notifications'],
http: {} as CoreStart['http'],
overlays: {} as CoreStart['overlays'],
store: {
get: () => {},
},
};

return (
Expand Down

0 comments on commit 2389fe8

Please sign in to comment.