Skip to content

Commit

Permalink
Prepare data.indexPatterns for move to NP (elastic#48083)
Browse files Browse the repository at this point in the history
Replace ui/saved_objects by core.savedObjects
  • Loading branch information
alexwizp committed Oct 15, 2019
1 parent a1b1be0 commit ac525f8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import React, { Component } from 'react';

import { EuiComboBox } from '@elastic/eui';
import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../../core/public';
import { getIndexPatternTitle } from '../utils';

interface IndexPatternSelectProps {
onChange: (opt: any) => void;
Expand Down Expand Up @@ -54,17 +55,6 @@ const getIndexPatterns = async (
return resp.savedObjects;
};

const getIndexPatternTitle = async (
client: SavedObjectsClientContract,
indexPatternId: string
): Promise<SimpleSavedObject<any>> => {
const savedObject = (await client.get('index-pattern', indexPatternId)) as SimpleSavedObject<any>;
if (savedObject.error) {
throw new Error(`Unable to get index-pattern title: ${savedObject.error.message}`);
}
return savedObject.attributes.title;
};

// Takes in stateful runtime dependencies and pre-wires them to the component
export function createIndexPatternSelect(savedObjectsClient: SavedObjectsClientContract) {
return (props: Omit<IndexPatternSelectProps, 'savedObjectsClient'>) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import chrome from 'ui/chrome';
import { fieldFormats } from 'ui/registry/field_formats';
// @ts-ignore
import { expandShorthand } from 'ui/utils/mapping_setup';
import { findObjectByTitle } from 'ui/saved_objects';

import { NotificationsSetup, SavedObjectsClientContract } from 'src/core/public';
import { SavedObjectNotFound, DuplicateField } from '../../../../../../plugins/kibana_utils/public';

import { findIndexPatternByTitle } from '../utils';
import { IndexPatternMissingIndices } from '../errors';
import { Field, FieldList, FieldType, FieldListInterface } from '../fields';
import { createFieldsFetcher } from './_fields_fetcher';
Expand Down Expand Up @@ -469,9 +469,8 @@ export class IndexPattern implements StaticIndexPattern {
return response.id;
};

const potentialDuplicateByTitle = await findObjectByTitle(
const potentialDuplicateByTitle = await findIndexPatternByTitle(
this.savedObjectsClient,
type,
this.title
);
// If there is potentially duplicate title, just create it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ jest.mock('ui/registry/field_formats', () => ({
},
}));

jest.mock('ui/notify', () => ({
toastNotifications: {
addDanger: jest.fn(),
},
}));

jest.mock('./index_pattern', () => {
class IndexPattern {
init = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ export type IndexPatternsStart = ReturnType<IndexPatternsService['start']>;

/** @public */
export { IndexPattern, IndexPatterns, StaticIndexPattern, Field, FieldType, FieldListInterface };

/** @public */
export { getIndexPatternTitle, findIndexPatternByTitle } from './utils';
46 changes: 45 additions & 1 deletion src/legacy/core_plugins/data/public/index_patterns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
* under the License.
*/

import { get } from 'lodash';
import { find, get } from 'lodash';

import { Field, FieldType } from './fields';
import { StaticIndexPattern } from './index_patterns';
import { getFilterableKbnTypeNames } from '../../../../../plugins/data/public';

import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../core/public';

export const ILLEGAL_CHARACTERS = 'ILLEGAL_CHARACTERS';
export const CONTAINS_SPACES = 'CONTAINS_SPACES';
export const INDEX_PATTERN_ILLEGAL_CHARACTERS_VISIBLE = ['\\', '/', '?', '"', '<', '>', '|'];
Expand All @@ -44,6 +46,48 @@ function findIllegalCharacters(indexPattern: string): string[] {
return illegalCharacters;
}

/**
* Returns an object matching a given title
*
* @param client {SavedObjectsClientContract}
* @param title {string}
* @returns {Promise<SimpleSavedObject|undefined>}
*/
export async function findIndexPatternByTitle(
client: SavedObjectsClientContract,
title: string
): Promise<SimpleSavedObject<any> | void> {
if (!title) {
return Promise.resolve();
}

const { savedObjects } = await client.find({
type: 'index-pattern',
perPage: 10,
search: `"${title}"`,
searchFields: ['title'],
fields: ['title'],
});

return find(
savedObjects,
(obj: SimpleSavedObject<any>) => obj.get('title').toLowerCase() === title.toLowerCase()
);
}

export async function getIndexPatternTitle(
client: SavedObjectsClientContract,
indexPatternId: string
): Promise<SimpleSavedObject<any>> {
const savedObject = (await client.get('index-pattern', indexPatternId)) as SimpleSavedObject<any>;

if (savedObject.error) {
throw new Error(`Unable to get index-pattern title: ${savedObject.error.message}`);
}

return savedObject.attributes.title;
}

function indexPatternContainsSpaces(indexPattern: string): boolean {
return indexPattern.includes(' ');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { i18n } from '@kbn/i18n';
import { toastNotifications } from 'ui/notify';
import { VegaView } from './vega_view/vega_view';
import { VegaMapView } from './vega_view/vega_map_view';
import { findObjectByTitle } from 'ui/saved_objects';
import { timefilter } from 'ui/timefilter';
import { start as data } from '../../../core_plugins/data/public/legacy';

import { findIndexPatternByTitle } from '../../data/public/index_patterns';

export const createVegaVisualization = ({ serviceSettings }) => class VegaVisualization {
constructor(el, vis) {
this.savedObjectsClient = chrome.getSavedObjectsClient();
Expand All @@ -40,7 +41,7 @@ export const createVegaVisualization = ({ serviceSettings }) => class VegaVisual
async findIndex(index) {
let idxObj;
if (index) {
idxObj = await findObjectByTitle(this.savedObjectsClient, 'index-pattern', index);
idxObj = await findIndexPatternByTitle(this.savedObjectsClient, index);
if (!idxObj) {
throw new Error(i18n.translate('visTypeVega.visualization.indexNotFoundErrorMessage', {
defaultMessage: 'Index {index} not found',
Expand Down

0 comments on commit ac525f8

Please sign in to comment.