Skip to content

Commit

Permalink
Integrate index pattern with new data client (opensearch-project#2146)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristen Tian <[email protected]>
  • Loading branch information
kristenTian committed Sep 15, 2022
1 parent 72d9b1a commit 0a34341
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 44 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/

import { i18n } from '@osd/i18n';
import _ from 'lodash';
import { SavedObjectsClientCommon } from '../..';

import { createIndexPatternCache } from '.';
import { IndexPattern } from './index_pattern';
import {
Expand Down Expand Up @@ -238,6 +238,7 @@ export class IndexPatternsService {
metaFields,
type: options.type,
params: options.params || {},
dataSourceId: options.dataSourceId,
});
};

Expand All @@ -254,6 +255,7 @@ export class IndexPatternsService {
...options,
type: indexPattern.type,
params: indexPattern.typeMeta && indexPattern.typeMeta.params,
dataSourceId: indexPattern.dataSourceRef?.id,
});

/**
Expand Down Expand Up @@ -355,12 +357,14 @@ export class IndexPatternsService {
typeMeta,
type,
},
references,
} = savedObject;

const parsedSourceFilters = sourceFilters ? JSON.parse(sourceFilters) : undefined;
const parsedTypeMeta = typeMeta ? JSON.parse(typeMeta) : undefined;
const parsedFieldFormatMap = fieldFormatMap ? JSON.parse(fieldFormatMap) : {};
const parsedFields: FieldSpec[] = fields ? JSON.parse(fields) : [];
const dataSourceRef = !_.isEmpty(references) ? references[0] : undefined;

this.addFormatsToFields(parsedFields, parsedFieldFormatMap);
return {
Expand All @@ -373,6 +377,7 @@ export class IndexPatternsService {
fields: this.fieldArrayToMap(parsedFields),
typeMeta: parsedTypeMeta,
type,
dataSourceRef,
};
};

Expand Down Expand Up @@ -401,7 +406,7 @@ export class IndexPatternsService {
}

const spec = this.savedObjectToSpec(savedObject);
const { title, type, typeMeta } = spec;
const { title, type, typeMeta, dataSourceRef } = spec;
const parsedFieldFormats: FieldFormatMap = savedObject.attributes.fieldFormatMap
? JSON.parse(savedObject.attributes.fieldFormatMap)
: {};
Expand All @@ -415,6 +420,7 @@ export class IndexPatternsService {
metaFields: await this.config.get(UI_SETTINGS.META_FIELDS),
type,
params: typeMeta && typeMeta.params,
dataSourceId: dataSourceRef?.id,
})
: spec.fields;
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export interface GetFieldsOptions {
params?: any;
lookBack?: boolean;
metaFields?: string[];
dataSourceId?: string;
}

export interface IIndexPatternsApiClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,20 @@ export class IndexPatternsApiClient implements IIndexPatternsApiClient {
}

getFieldsForTimePattern(options: GetFieldsOptions = {}) {
const { pattern, lookBack, metaFields } = options;
const { pattern, lookBack, metaFields, dataSourceId } = options;

const url = this._getUrl(['_fields_for_time_pattern']);

return this._request(url, {
pattern,
look_back: lookBack,
meta_fields: metaFields,
data_source: dataSourceId,
}).then((resp: any) => resp.fields);
}

getFieldsForWildcard(options: GetFieldsOptions = {}) {
const { pattern, metaFields, type, params } = options;
const { pattern, metaFields, type, params, dataSourceId } = options;

let url;
let query;
Expand All @@ -83,12 +84,14 @@ export class IndexPatternsApiClient implements IIndexPatternsApiClient {
pattern,
meta_fields: metaFields,
params: JSON.stringify(params),
data_source: dataSourceId,
};
} else {
url = this._getUrl(['_fields_for_wildcard']);
query = {
pattern,
meta_fields: metaFields,
data_source: dataSourceId,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

import { LegacyAPICaller } from 'opensearch-dashboards/server';
import { LegacyAPICaller, OpenSearchClient } from 'opensearch-dashboards/server';

import { getFieldCapabilities, resolveTimePattern, createNoMatchingIndicesError } from './lib';

Expand All @@ -48,9 +48,9 @@ interface FieldSubType {
}

export class IndexPatternsFetcher {
private _callDataCluster: LegacyAPICaller;
private _callDataCluster: LegacyAPICaller | OpenSearchClient;

constructor(callDataCluster: LegacyAPICaller) {
constructor(callDataCluster: LegacyAPICaller | OpenSearchClient) {
this._callDataCluster = callDataCluster;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import { defaults, keyBy, sortBy } from 'lodash';

import { LegacyAPICaller } from 'opensearch-dashboards/server';
import { LegacyAPICaller, OpenSearchClient } from 'opensearch-dashboards/server';
import { callFieldCapsApi } from '../opensearch_api';
import { FieldCapsResponse, readFieldCapsResponse } from './field_caps_response';
import { mergeOverrides } from './overrides';
Expand All @@ -47,7 +47,7 @@ import { FieldDescriptor } from '../../index_patterns_fetcher';
* @return {Promise<Array<FieldDescriptor>>}
*/
export async function getFieldCapabilities(
callCluster: LegacyAPICaller,
callCluster: LegacyAPICaller | OpenSearchClient,
indices: string | string[] = [],
metaFields: string[] = [],
fieldCapsOptions?: { allowNoIndices: boolean }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

import { LegacyAPICaller } from 'opensearch-dashboards/server';
import { LegacyAPICaller, OpenSearchClient } from 'opensearch-dashboards/server';
import { convertOpenSearchError } from './errors';
import { FieldCapsResponse } from './field_capabilities';

Expand Down Expand Up @@ -57,10 +57,18 @@ export interface IndexAliasResponse {
* @return {Promise<IndexAliasResponse>}
*/
export async function callIndexAliasApi(
callCluster: LegacyAPICaller,
callCluster: LegacyAPICaller | OpenSearchClient,
indices: string[] | string
): Promise<IndicesAliasResponse> {
try {
if ('transport' in callCluster) {
return ((await callCluster.indices.getAlias({
index: indices,
ignore_unavailable: true,
allow_no_indices: true,
})) as unknown) as Promise<IndicesAliasResponse>; // todo: Pending #... verify test
}

return (await callCluster('indices.getAlias', {
index: indices,
ignoreUnavailable: true,
Expand All @@ -84,11 +92,20 @@ export async function callIndexAliasApi(
* @return {Promise<FieldCapsResponse>}
*/
export async function callFieldCapsApi(
callCluster: LegacyAPICaller,
callCluster: LegacyAPICaller | OpenSearchClient,
indices: string[] | string,
fieldCapsOptions: { allowNoIndices: boolean } = { allowNoIndices: false }
) {
try {
if ('transport' in callCluster) {
return ((await callCluster.fieldCaps({
index: indices,
fields: '*',
ignore_unavailable: true,
allow_no_indices: fieldCapsOptions.allowNoIndices,
})) as unknown) as FieldCapsResponse; // todo: Pending #... verify test
}

return (await callCluster('fieldCaps', {
index: indices,
fields: '*',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import { chain } from 'lodash';
import moment from 'moment';

import { LegacyAPICaller } from 'opensearch-dashboards/server';
import { LegacyAPICaller, OpenSearchClient } from 'opensearch-dashboards/server';

import { timePatternToWildcard } from './time_pattern_to_wildcard';
import { callIndexAliasApi, IndicesAliasResponse } from './opensearch_api';
Expand All @@ -47,7 +47,10 @@ import { callIndexAliasApi, IndicesAliasResponse } from './opensearch_api';
* and the indices that actually match the time
* pattern (matches);
*/
export async function resolveTimePattern(callCluster: LegacyAPICaller, timePattern: string) {
export async function resolveTimePattern(
callCluster: LegacyAPICaller | OpenSearchClient,
timePattern: string
) {
const aliases = await callIndexAliasApi(callCluster, timePatternToWildcard(timePattern));

const allIndexDetails = chain<IndicesAliasResponse>(aliases)
Expand Down
15 changes: 13 additions & 2 deletions src/plugins/data/server/index_patterns/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ export function registerRoutes(http: HttpServiceSetup) {
meta_fields: schema.oneOf([schema.string(), schema.arrayOf(schema.string())], {
defaultValue: [],
}),
data_source: schema.maybe(schema.string()),
}),
},
},
async (context, request, response) => {
const { callAsCurrentUser } = context.core.opensearch.legacy.client;
const callAsCurrentUser = decideClient(context, request);
const indexPatterns = new IndexPatternsFetcher(callAsCurrentUser);
const { pattern, meta_fields: metaFields } = request.query;

Expand Down Expand Up @@ -112,11 +113,13 @@ export function registerRoutes(http: HttpServiceSetup) {
meta_fields: schema.oneOf([schema.string(), schema.arrayOf(schema.string())], {
defaultValue: [],
}),
data_source: schema.maybe(schema.string()),
}),
},
},
async (context: RequestHandlerContext, request: any, response: any) => {
const { callAsCurrentUser } = context.core.opensearch.legacy.client;
const callAsCurrentUser = decideClient(context, request);

const indexPatterns = new IndexPatternsFetcher(callAsCurrentUser);
const { pattern, interval, look_back: lookBack, meta_fields: metaFields } = request.query;

Expand Down Expand Up @@ -147,3 +150,11 @@ export function registerRoutes(http: HttpServiceSetup) {
}
);
}

const decideClient = (context: RequestHandlerContext, request: any) => {
if (request.query.data_source) {
// todo: # Pending
}

return context.core.opensearch.legacy.client.callAsCurrentUser;
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { Header } from './components/header';
import { context as contextType } from '../../../../../../opensearch_dashboards_react/public';
import { IndexPatternCreationConfig } from '../../../../../../../plugins/index_pattern_management/public';
import { MatchedItem } from '../../types';
import { IndexPatternManagmentContextValue } from '../../../../types';
import { DataSourceRef, IndexPatternManagmentContextValue } from '../../../../types';

interface StepIndexPatternProps {
allIndices: MatchedItem[];
Expand All @@ -63,6 +63,7 @@ interface StepIndexPatternProps {
goToNextStep: (query: string, timestampField?: string) => void;
initialQuery?: string;
showSystemIndices: boolean;
dataSourceRef?: DataSourceRef;
}

interface StepIndexPatternState {
Expand Down Expand Up @@ -163,7 +164,8 @@ export class StepIndexPattern extends Component<StepIndexPatternProps, StepIndex
};

fetchIndices = async (query: string) => {
const { indexPatternCreationType } = this.props;
const { indexPatternCreationType, dataSourceRef } = this.props;
const dataSourceId = dataSourceRef?.id;
const { existingIndexPatterns } = this.state;
const { http } = this.context.services;
const getIndexTags = (indexName: string) => indexPatternCreationType.getIndexTags(indexName);
Expand All @@ -179,7 +181,14 @@ export class StepIndexPattern extends Component<StepIndexPatternProps, StepIndex

if (query.endsWith('*')) {
const exactMatchedIndices = await ensureMinimumTime(
getIndices({ http, getIndexTags, pattern: query, showAllIndices, searchClient })
getIndices({
http,
getIndexTags,
pattern: query,
showAllIndices,
searchClient,
dataSourceId,
})
);
// If the search changed, discard this state
if (query !== this.lastQuery) {
Expand All @@ -190,8 +199,22 @@ export class StepIndexPattern extends Component<StepIndexPatternProps, StepIndex
}

const [partialMatchedIndices, exactMatchedIndices] = await ensureMinimumTime([
getIndices({ http, getIndexTags, pattern: `${query}*`, showAllIndices, searchClient }),
getIndices({ http, getIndexTags, pattern: query, showAllIndices, searchClient }),
getIndices({
http,
getIndexTags,
pattern: `${query}*`,
showAllIndices,
searchClient,
dataSourceId,
}),
getIndices({
http,
getIndexTags,
pattern: query,
showAllIndices,
searchClient,
dataSourceId,
}),
]);

// If the search changed, discard this state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { TimeField } from './components/time_field';
import { AdvancedOptions } from './components/advanced_options';
import { ActionButtons } from './components/action_buttons';
import { context } from '../../../../../../opensearch_dashboards_react/public';
import { IndexPatternManagmentContextValue } from '../../../../types';
import { DataSourceRef, IndexPatternManagmentContextValue } from '../../../../types';
import { IndexPatternCreationConfig } from '../../../..';

interface StepTimeFieldProps {
Expand All @@ -55,6 +55,7 @@ interface StepTimeFieldProps {
createIndexPattern: (selectedTimeField: string | undefined, indexPatternId: string) => void;
indexPatternCreationType: IndexPatternCreationConfig;
selectedTimeField?: string;
dataSourceRef?: DataSourceRef;
}

interface StepTimeFieldState {
Expand Down Expand Up @@ -116,14 +117,15 @@ export class StepTimeField extends Component<StepTimeFieldProps, StepTimeFieldSt
}

fetchTimeFields = async () => {
const { indexPattern: pattern } = this.props;
const { indexPattern: pattern, dataSourceRef } = this.props;
const { getFetchForWildcardOptions } = this.props.indexPatternCreationType;

this.setState({ isFetchingTimeFields: true });
const fields = await ensureMinimumTime(
this.context.services.data.indexPatterns.getFieldsForWildcard({
pattern,
...getFetchForWildcardOptions(),
dataSourceId: dataSourceRef?.id,
})
);
const timeFields = extractTimeFields(fields);
Expand Down
Loading

0 comments on commit 0a34341

Please sign in to comment.