Skip to content

Commit

Permalink
adding search source persistable state interface (#120461)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Dec 9, 2021
1 parent 5b359a9 commit d1774fe
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DATA_VIEW_SAVED_OBJECT_TYPE } from '../../../../data/common';

export const extractReferences = (
state: SerializedSearchSourceFields
): [SerializedSearchSourceFields & { indexRefName?: string }, SavedObjectReference[]] => {
): [SerializedSearchSourceFields, SavedObjectReference[]] => {
let searchSourceFields: SerializedSearchSourceFields & { indexRefName?: string } = { ...state };
const references: SavedObjectReference[] = [];
if (searchSourceFields.index) {
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data/common/search/search_source/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export const searchSourceInstanceMock: MockedKeys<ISearchSource> = {
export const searchSourceCommonMock: jest.Mocked<ISearchStartSearchSource> = {
create: jest.fn().mockReturnValue(searchSourceInstanceMock),
createEmpty: jest.fn().mockReturnValue(searchSourceInstanceMock),
telemetry: jest.fn(),
getAllMigrations: jest.fn(),
inject: jest.fn(),
extract: jest.fn(),
};

export const createSearchSourceMock = (fields?: SearchSourceFields, response?: any) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ describe('SearchSource service', () => {
dependencies
);

expect(Object.keys(start)).toEqual(['create', 'createEmpty']);
expect(Object.keys(start)).toEqual([
'create',
'createEmpty',
'extract',
'inject',
'getAllMigrations',
'telemetry',
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
* Side Public License, v 1.
*/

import { createSearchSource, SearchSource, SearchSourceDependencies } from './';
import { mapValues } from 'lodash';
import {
createSearchSource,
extractReferences,
injectReferences,
SearchSource,
SearchSourceDependencies,
SerializedSearchSourceFields,
} from './';
import { IndexPatternsContract } from '../..';
import { mergeMigrationFunctionMaps } from '../../../../kibana_utils/common';
import { getAllMigrations as filtersGetAllMigrations } from '../../query/persistable_state';

export class SearchSourceService {
public setup() {}
Expand All @@ -24,6 +34,28 @@ export class SearchSourceService {
createEmpty: () => {
return new SearchSource({}, dependencies);
},
extract: (state: SerializedSearchSourceFields) => {
const [newState, references] = extractReferences(state);
return { state: newState, references };
},
inject: injectReferences,
getAllMigrations: () => {
const searchSourceMigrations = {};

// we don't know if embeddables have any migrations defined so we need to fetch them and map the received functions so we pass
// them the correct input and that we correctly map the response
const filterMigrations = mapValues(filtersGetAllMigrations(), (migrate) => {
return (state: SerializedSearchSourceFields) => ({
...state,
filter: migrate(state.filter),
});
});

return mergeMigrationFunctionMaps(searchSourceMigrations, filterMigrations);
},
telemetry: () => {
return {};
},
};
}

Expand Down
19 changes: 12 additions & 7 deletions src/plugins/data/common/search/search_source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Query } from '../..';
import { Filter } from '../../es_query';
import { IndexPattern } from '../..';
import { SearchSource } from './search_source';
import { PersistableStateService } from '../../../../kibana_utils/common';

/**
* search source interface
Expand All @@ -24,7 +25,8 @@ export type ISearchSource = Pick<SearchSource, keyof SearchSource>;
* high level search service
* @public
*/
export interface ISearchStartSearchSource {
export interface ISearchStartSearchSource
extends PersistableStateService<SerializedSearchSourceFields> {
/**
* creates {@link SearchSource} based on provided serialized {@link SearchSourceFields}
* @param fields
Expand All @@ -43,15 +45,17 @@ export enum SortDirection {
desc = 'desc',
}

export interface SortDirectionFormat {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type SortDirectionFormat = {
order: SortDirection;
format?: string;
}
};

export interface SortDirectionNumeric {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type SortDirectionNumeric = {
order: SortDirection;
numeric_type?: 'double' | 'long' | 'date' | 'date_nanos';
}
};

export type EsQuerySortValue = Record<
string,
Expand Down Expand Up @@ -114,7 +118,8 @@ export interface SearchSourceFields {
parent?: SearchSourceFields;
}

export interface SerializedSearchSourceFields {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type SerializedSearchSourceFields = {
type?: string;
/**
* {@link Query}
Expand Down Expand Up @@ -159,7 +164,7 @@ export interface SerializedSearchSourceFields {
terminate_after?: number;

parent?: SerializedSearchSourceFields;
}
};

export interface SearchSourceOptions {
callParentStartHandlers?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const searchSourceMock = { ...searchSourceInstanceMock };
const mockSearchSourceService: jest.Mocked<ISearchStartSearchSource> = {
create: jest.fn().mockReturnValue(searchSourceMock),
createEmpty: jest.fn().mockReturnValue(searchSourceMock),
telemetry: jest.fn(),
inject: jest.fn(),
extract: jest.fn(),
getAllMigrations: jest.fn(),
};
const mockDataClientSearchDefault = jest.fn().mockImplementation(
(): Rx.Observable<{ rawResponse: SearchResponse<unknown> }> =>
Expand Down

0 comments on commit d1774fe

Please sign in to comment.