Skip to content

Commit

Permalink
Typescriptify search source (#47644)
Browse files Browse the repository at this point in the history
* Initial refactor of search source

* Add abort signal to search source fetch and remove cancel queued

* Remove usages of Angular Promises

* Remove usages of angular "sessionId" service

* Remove config as dependency

* Update deps on config and esShardTimeout

* Remove remaining angular dependencies from SearchSource

* Fix Karma tests

* Separate callClient and handleResponse and add tests for each

* Add tests for fetchSoon

* Add back search source test and convert to jest

* Create search strategy registry test

* Revert empty test

* Remove filter predicates from search source

* Update typings and throw response errors

* Fix proxy to properly return response from ES

* Update jest snapshots

* Remove unused translations

* Don't pass search request to onRequestStart, create it afterwards

* Fix search source & get search params tests

* Fix issue with angular scope not firing after setting state on vis

* Fix issue with angular scope not firing after setting state on vis

* Typescriptify courier/search source

* Fix references

* Fix types

* Fix removal of underscores

* Fix fetching

* Fix tag cloud vis

* Fix setting of visConfig to not happen async

* Remove unused snapshots

* Remove courier and search poller

* Update types

* Fix issue with filters not applying

* Fix issue with search embeddable time ranges

* Remove deleted file again

* Fix source includes

* Fix searchsource constructor

* Don't pass null as initial value for search source

* Fix inmemorytable type

* Fix issue with filters aggregation

* Fix tests

* Mock new platform

* Fix agg filters

* Fix context app tests

* Fix context tests (for real)

* Fix normalizeSortRequest for dateNanos

* Fix issue with multiple levels of Other buckets

* Adding in last commit

* Review feedback

* Update references to ui/courier

* Fix eslint

* Fix tests

* Fix filter matches index for filters with partial meta

* Fix type errors

* Fix references

* Address review feedback

* Fix failing test
  • Loading branch information
lukasolson authored Nov 26, 2019
1 parent ab3944c commit e3a97dd
Show file tree
Hide file tree
Showing 91 changed files with 1,646 additions and 1,742 deletions.
3 changes: 0 additions & 3 deletions src/fixtures/stubbed_search_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ export default function stubSearchSource(Private, $q, Promise) {
onRequestStart(fn) {
this._requestStartHandlers.push(fn);
},
requestIsStarting(req) {
return Promise.map(this._requestStartHandlers, fn => fn(req));
},
requestIsStopped() {}
};

Expand Down
15 changes: 8 additions & 7 deletions src/legacy/core_plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ import {
ExpressionFunction,
KibanaDatatableColumn,
} from 'src/plugins/expressions/public';
import { SearchSource } from '../../../../../ui/public/courier/search_source';
import {
SearchSource,
SearchSourceContract,
getRequestInspectorStats,
getResponseInspectorStats,
} from '../../../../../ui/public/courier';
// @ts-ignore
import {
FilterBarQueryFilterProvider,
QueryFilter,
} from '../../../../../ui/public/filter_manager/query_filter';

import { buildTabularInspectorData } from '../../../../../ui/public/inspector/build_tabular_inspector_data';
import {
getRequestInspectorStats,
getResponseInspectorStats,
} from '../../../../../ui/public/courier/utils/courier_inspector_utils';
import { calculateObjectHash } from '../../../../../ui/public/vis/lib/calculate_object_hash';
import { getTime } from '../../../../../ui/public/timefilter';
// @ts-ignore
Expand All @@ -50,7 +51,7 @@ import { PersistedState } from '../../../../../ui/public/persisted_state';
import { Adapters } from '../../../../../../plugins/inspector/public';

export interface RequestHandlerParams {
searchSource: SearchSource;
searchSource: SearchSourceContract;
aggs: AggConfigs;
timeRange?: TimeRange;
query?: Query;
Expand Down Expand Up @@ -119,7 +120,7 @@ const handleCourierRequest = async ({
return aggs.toDsl(metricsAtAllLevels);
});

requestSearchSource.onRequestStart((paramSearchSource: SearchSource, options: any) => {
requestSearchSource.onRequestStart((paramSearchSource, options) => {
return aggs.onSearchRequestStart(paramSearchSource, options);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import { timefilter } from 'ui/timefilter';

export function createSearchSource(SearchSource, initialState, indexPattern, aggs, useTimeFilter, filters = []) {
const searchSource = new SearchSource(initialState);
const searchSource = initialState ? new SearchSource(initialState) : new SearchSource();
// Do not not inherit from rootSearchSource to avoid picking up time and globals
searchSource.setParent(false);
searchSource.setParent(undefined);
searchSource.setField('filter', () => {
const activeFilters = [...filters];
if (useTimeFilter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { SearchSource } from 'ui/courier';
import { SavedObject } from 'ui/saved_objects/saved_object';
import { SearchSourceContract } from '../../../../../ui/public/courier';
import { esFilters, Query, RefreshInterval } from '../../../../../../plugins/data/public';

export interface SavedObjectDashboard extends SavedObject {
Expand All @@ -34,7 +34,7 @@ export interface SavedObjectDashboard extends SavedObject {
// TODO: write a migration to rid of this, it's only around for bwc.
uiStateJSON?: string;
lastSavedTitle: string;
searchSource: SearchSource;
searchSource: SearchSourceContract;
destroy: () => void;
refreshInterval?: RefreshInterval;
getQuery(): Query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('context app', function () {
.then(() => {
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.calledOnce).to.be(true);
expect(setParentSpy.firstCall.args[0]).to.eql(false);
expect(setParentSpy.firstCall.args[0]).to.be(undefined);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('context app', function () {
)
.then(() => {
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.alwaysCalledWith(false)).to.be(true);
expect(setParentSpy.alwaysCalledWith(undefined)).to.be(true);
expect(setParentSpy.called).to.be(true);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('context app', function () {
)
.then(() => {
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.alwaysCalledWith(false)).to.be(true);
expect(setParentSpy.alwaysCalledWith(undefined)).to.be(true);
expect(setParentSpy.called).to.be(true);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function fetchAnchorProvider(indexPatterns) {
) {
const indexPattern = await indexPatterns.get(indexPatternId);
const searchSource = new SearchSource()
.setParent(false)
.setParent(undefined)
.setField('index', indexPattern)
.setField('version', true)
.setField('size', 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/

import { SortDirection } from '../../../../../../../ui/public/courier';
import { IndexPatterns, IndexPattern, getServices } from '../../../kibana_services';
import { reverseSortDir, SortDirection } from './utils/sorting';
import { reverseSortDir } from './utils/sorting';
import { extractNanos, convertIsoToMillis } from './utils/date_conversion';
import { fetchHitsInInterval } from './utils/fetch_hits_in_interval';
import { generateIntervals } from './utils/generate_intervals';
Expand Down Expand Up @@ -114,7 +115,7 @@ function fetchContextProvider(indexPatterns: IndexPatterns) {

async function createSearchSource(indexPattern: IndexPattern, filters: esFilters.Filter[]) {
return new SearchSource()
.setParent(false)
.setParent(undefined)
.setField('index', indexPattern)
.setField('filter', filters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/
import { reverseSortDir, SortDirection } from '../sorting';
import { reverseSortDir } from '../sorting';
import { SortDirection } from '../../../../../../../../../ui/public/courier';

jest.mock('ui/new_platform');

describe('function reverseSortDir', function() {
test('reverse a given sort direction', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
import { SearchSource } from '../../../../kibana_services';
import {
EsQuerySortValue,
SortDirection,
SearchSourceContract,
} from '../../../../../../../../ui/public/courier';
import { convertTimeValueToIso } from './date_conversion';
import { SortDirection } from './sorting';
import { EsHitRecordList } from '../context';
import { IntervalValue } from './generate_intervals';
import { EsQuerySort } from './get_es_query_sort';
import { EsQuerySearchAfter } from './get_es_query_search_after';

interface RangeQuery {
Expand All @@ -38,9 +40,9 @@ interface RangeQuery {
* and filters set.
*/
export async function fetchHitsInInterval(
searchSource: SearchSource,
searchSource: SearchSourceContract,
timeField: string,
sort: EsQuerySort,
sort: [EsQuerySortValue, EsQuerySortValue],
sortDir: SortDirection,
interval: IntervalValue[],
searchAfter: EsQuerySearchAfter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { SortDirection } from './sorting';
import { SortDirection } from '../../../../../../../../ui/public/courier';

export type IntervalValue = number | null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import { SortDirection } from './sorting';

type EsQuerySortValue = Record<string, SortDirection>;

export type EsQuerySort = [EsQuerySortValue, EsQuerySortValue];
import { EsQuerySortValue, SortDirection } from '../../../../../../../../ui/public/courier/types';

/**
* Returns `EsQuerySort` which is used to sort records in the ES query
Expand All @@ -33,6 +30,6 @@ export function getEsQuerySort(
timeField: string,
tieBreakerField: string,
sortDir: SortDirection
): EsQuerySort {
): [EsQuerySortValue, EsQuerySortValue] {
return [{ [timeField]: sortDir }, { [tieBreakerField]: sortDir }];
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
* under the License.
*/

import { SortDirection } from '../../../../../../../../ui/public/courier';
import { IndexPattern } from '../../../../kibana_services';

export enum SortDirection {
asc = 'asc',
desc = 'desc',
}

/**
* The list of field names that are allowed for sorting, but not included in
* index pattern fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Subscription } from 'rxjs';
import { i18n } from '@kbn/i18n';
import { TExecuteTriggerActions } from 'src/plugins/ui_actions/public';
import { npStart } from 'ui/new_platform';
import { SearchSourceContract } from '../../../../../ui/public/courier';
import {
esFilters,
TimeRange,
Expand Down Expand Up @@ -51,7 +52,6 @@ import {
getServices,
IndexPattern,
RequestAdapter,
SearchSource,
} from '../kibana_services';
import { SEARCH_EMBEDDABLE_TYPE } from './constants';

Expand Down Expand Up @@ -92,7 +92,7 @@ export class SearchEmbeddable extends Embeddable<SearchInput, SearchOutput>
private inspectorAdaptors: Adapters;
private searchScope?: SearchScope;
private panelTitle: string = '';
private filtersSearchSource?: SearchSource;
private filtersSearchSource?: SearchSourceContract;
private searchInstance?: JQLite;
private autoRefreshFetchSubscription?: Subscription;
private subscription?: Subscription;
Expand Down Expand Up @@ -194,13 +194,11 @@ export class SearchEmbeddable extends Embeddable<SearchInput, SearchOutput>
searchScope.inspectorAdapters = this.inspectorAdaptors;

const { searchSource } = this.savedSearch;
const indexPattern = (searchScope.indexPattern = searchSource.getField('index'));
const indexPattern = (searchScope.indexPattern = searchSource.getField('index'))!;

const timeRangeSearchSource = searchSource.create();
timeRangeSearchSource.setField('filter', () => {
if (!this.searchScope || !this.input.timeRange) {
return;
}
if (!this.searchScope || !this.input.timeRange) return;
return getTime(indexPattern, this.input.timeRange);
});

Expand Down Expand Up @@ -241,7 +239,7 @@ export class SearchEmbeddable extends Embeddable<SearchInput, SearchOutput>
};

searchScope.filter = async (field, value, operator) => {
let filters = generateFilters(this.filterManager, field, value, operator, indexPattern.id);
let filters = generateFilters(this.filterManager, field, value, operator, indexPattern.id!);
filters = filters.map(filter => ({
...filter,
$state: { store: esFilters.FilterStateStore.APP_STATE },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class SearchEmbeddableFactory extends EmbeddableFactory<
const queryFilter = Private(getServices().FilterBarQueryFilterProvider);
try {
const savedObject = await searchLoader.get(savedObjectId);
const indexPattern = savedObject.searchSource.getField('index');
return new SearchEmbeddable(
{
savedSearch: savedObject,
Expand All @@ -92,7 +93,7 @@ export class SearchEmbeddableFactory extends EmbeddableFactory<
editUrl,
queryFilter,
editable: getServices().capabilities.discover.save as boolean,
indexPatterns: _.compact([savedObject.searchSource.getField('index')]),
indexPatterns: indexPattern ? [indexPattern] : [],
},
input,
this.executeTriggerActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import angular from 'angular'; // just used in embeddables and discover controll
import uiRoutes from 'ui/routes';
// @ts-ignore
import { uiModules } from 'ui/modules';
import { SearchSource } from 'ui/courier';
// @ts-ignore
import { StateProvider } from 'ui/state_management/state';
// @ts-ignore
Expand All @@ -43,6 +42,7 @@ import { wrapInI18nContext } from 'ui/i18n';
import { docTitle } from 'ui/doc_title';
// @ts-ignore
import * as docViewsRegistry from 'ui/registry/doc_views';
import { SearchSource } from '../../../../ui/public/courier';

const services = {
// new plattform
Expand Down Expand Up @@ -87,9 +87,10 @@ export { callAfterBindingsWorkaround } from 'ui/compat';
export {
getRequestInspectorStats,
getResponseInspectorStats,
} from 'ui/courier/utils/courier_inspector_utils';
// @ts-ignore
export { hasSearchStategyForIndexPattern, isDefaultTypeIndexPattern } from 'ui/courier';
hasSearchStategyForIndexPattern,
isDefaultTypeIndexPattern,
SearchSource,
} from '../../../../ui/public/courier';
// @ts-ignore
export { intervalOptions } from 'ui/agg_types/buckets/_interval_options';
// @ts-ignore
Expand All @@ -115,7 +116,6 @@ export { unhashUrl } from 'ui/state_management/state_hashing';
// EXPORT types
export { Vis } from 'ui/vis';
export { StaticIndexPattern, IndexPatterns, IndexPattern, FieldType } from 'ui/index_patterns';
export { SearchSource } from 'ui/courier';
export { ElasticSearchHit } from 'ui/registry/doc_views_types';
export { DocViewRenderProps, DocViewRenderFn } from 'ui/registry/doc_views';
export { Adapters } from 'ui/inspector/types';
5 changes: 3 additions & 2 deletions src/legacy/core_plugins/kibana/public/discover/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
* under the License.
*/

import { SearchSource } from './kibana_services';
import { SearchSourceContract } from '../../../../ui/public/courier';
import { SortOrder } from './angular/doc_table/components/table_header/helpers';
export { SortOrder } from './angular/doc_table/components/table_header/helpers';

export interface SavedSearch {
readonly id: string;
title: string;
searchSource: SearchSource;
searchSource: SearchSourceContract;
description?: string;
columns: string[];
sort: SortOrder[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import * as Rx from 'rxjs';
import { buildPipeline } from 'ui/visualize/loader/pipeline_helpers';
import { SavedObject } from 'ui/saved_objects/saved_object';
import { Vis } from 'ui/vis';
import { SearchSource } from 'ui/courier';
import { queryGeohashBounds } from 'ui/visualize/loader/utils';
import { getTableAggs } from 'ui/visualize/loader/pipeline_helpers/utilities';
import { AppState } from 'ui/state_management/app_state';
import { npStart } from 'ui/new_platform';
import { IExpressionLoaderParams } from 'src/plugins/expressions/public';
import { SearchSourceContract } from '../../../../../ui/public/courier';
import { VISUALIZE_EMBEDDABLE_TYPE } from './constants';
import {
TimeRange,
Expand All @@ -53,7 +53,7 @@ const getKeys = <T extends {}>(o: T): Array<keyof T> => Object.keys(o) as Array<
export interface VisSavedObject extends SavedObject {
vis: Vis;
description?: string;
searchSource: SearchSource;
searchSource: SearchSourceContract;
title: string;
uiStateJSON?: string;
destroy: () => void;
Expand Down
5 changes: 3 additions & 2 deletions src/legacy/ui/public/agg_types/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { npStart } from 'ui/new_platform';
import { SearchSourceContract, FetchOptions } from '../courier/types';
import { AggType } from './agg_type';
import { FieldParamType } from './param_types/field';
import { AggGroupNames } from '../vis/editors/default/agg_groups';
Expand Down Expand Up @@ -233,10 +234,10 @@ export class AggConfig {
/**
* Hook for pre-flight logic, see AggType#onSearchRequestStart
* @param {Courier.SearchSource} searchSource
* @param {Courier.SearchRequest} searchRequest
* @param {Courier.FetchOptions} options
* @return {Promise<undefined>}
*/
onSearchRequestStart(searchSource: any, options: any) {
onSearchRequestStart(searchSource: SearchSourceContract, options?: FetchOptions) {
if (!this.type) {
return Promise.resolve();
}
Expand Down
Loading

0 comments on commit e3a97dd

Please sign in to comment.