Skip to content

Commit

Permalink
[ML] Transforms: Use data views service for loading data views (#131819)
Browse files Browse the repository at this point in the history
* [ML] Transforms: Use data views service for loading data views

* [ML] Edits from review
  • Loading branch information
peteharverson authored May 10, 2022
1 parent 4b430ad commit 69efa44
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 121 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/transform/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"ui": true,
"requiredPlugins": [
"data",
"dataViews",
"home",
"licensing",
"management",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { indexService } from '../services/es_index_service';
export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {
const {
http,
savedObjects,
data: { dataViews: dataViewsContract },
ml: { extractErrorMessage },
application: { capabilities },
} = useAppDependencies();
Expand All @@ -46,9 +46,8 @@ export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {
const checkDataViewExists = useCallback(
async (indexName: string) => {
try {
if (await indexService.dataViewExists(savedObjects.client, indexName)) {
setDataViewExists(true);
}
const dvExists = await indexService.dataViewExists(dataViewsContract, indexName);
setDataViewExists(dvExists);
} catch (e) {
const error = extractErrorMessage(e);

Expand All @@ -63,7 +62,7 @@ export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {
);
}
},
[savedObjects.client, toastNotifications, extractErrorMessage]
[dataViewsContract, toastNotifications, extractErrorMessage]
);

const checkUserIndexPermission = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,31 @@
*/

import { buildEsQuery } from '@kbn/es-query';
import { SavedObjectsClientContract, SimpleSavedObject, IUiSettingsClient } from '@kbn/core/public';
import type { IUiSettingsClient } from '@kbn/core/public';
import { getEsQueryConfig } from '@kbn/data-plugin/public';
import { DataView, DataViewAttributes, DataViewsContract } from '@kbn/data-views-plugin/public';
import type { DataView, DataViewsContract } from '@kbn/data-views-plugin/public';

import { matchAllQuery } from '../../common';

import { isDataView } from '../../../../common/types/data_view';

export type SavedSearchQuery = object;

type DataViewId = string;

let dataViewCache: Array<SimpleSavedObject<Record<string, any>>> = [];
let fullDataViews;
let currentDataView = null;
let dataViewCache: DataView[] = [];

export let refreshDataViews: () => Promise<unknown>;

export function loadDataViews(
savedObjectsClient: SavedObjectsClientContract,
dataViews: DataViewsContract
) {
fullDataViews = dataViews;
return savedObjectsClient
.find<DataViewAttributes>({
type: 'index-pattern',
fields: ['id', 'title', 'type', 'fields'],
perPage: 10000,
})
.then((response) => {
dataViewCache = response.savedObjects;

if (refreshDataViews === null) {
refreshDataViews = () => {
return new Promise((resolve, reject) => {
loadDataViews(savedObjectsClient, dataViews)
.then((resp) => {
resolve(resp);
})
.catch((error) => {
reject(error);
});
});
};
}

return dataViewCache;
});
export async function loadDataViews(dataViewsContract: DataViewsContract) {
dataViewCache = await dataViewsContract.find('*', 10000);
return dataViewCache;
}

export function getDataViewIdByTitle(dataViewTitle: string): string | undefined {
return dataViewCache.find((d) => d?.attributes?.title === dataViewTitle)?.id;
return dataViewCache.find(({ title }) => title === dataViewTitle)?.id;
}

type CombinedQuery = Record<'bool', any> | object;

export function loadCurrentDataView(dataViews: DataViewsContract, dataViewId: DataViewId) {
fullDataViews = dataViews;
currentDataView = fullDataViews.get(dataViewId);
return currentDataView;
}

export interface SearchItems {
dataView: DataView;
savedSearch: any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,24 @@ import { getSavedSearch, getSavedSearchUrlConflictMessage } from '../../../share

import { useAppDependencies } from '../../app_dependencies';

import {
createSearchItems,
getDataViewIdByTitle,
loadCurrentDataView,
loadDataViews,
SearchItems,
} from './common';
import { createSearchItems, getDataViewIdByTitle, loadDataViews, SearchItems } from './common';

export const useSearchItems = (defaultSavedObjectId: string | undefined) => {
const [savedObjectId, setSavedObjectId] = useState(defaultSavedObjectId);
const [error, setError] = useState<string | undefined>();

const appDeps = useAppDependencies();
const dataViews = appDeps.data.dataViews;
const dataViewsContract = appDeps.data.dataViews;
const uiSettings = appDeps.uiSettings;
const savedObjectsClient = appDeps.savedObjects.client;

const [searchItems, setSearchItems] = useState<SearchItems | undefined>(undefined);

async function fetchSavedObject(id: string) {
await loadDataViews(savedObjectsClient, dataViews);

let fetchedDataView;
let fetchedSavedSearch;

try {
fetchedDataView = await loadCurrentDataView(dataViews, id);
fetchedDataView = await dataViewsContract.get(id);
} catch (e) {
// Just let fetchedDataView stay undefined in case it doesn't exist.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export type CloneAction = ReturnType<typeof useCloneAction>;
export const useCloneAction = (forceDisable: boolean, transformNodes: number) => {
const history = useHistory();
const appDeps = useAppDependencies();
const savedObjectsClient = appDeps.savedObjects.client;
const dataViews = appDeps.data.dataViews;
const dataViewsContract = appDeps.data.dataViews;
const toastNotifications = useToastNotifications();

const { getDataViewIdByTitle, loadDataViews } = useSearchItems(undefined);
Expand All @@ -32,7 +31,7 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) =>
const clickHandler = useCallback(
async (item: TransformListRow) => {
try {
await loadDataViews(savedObjectsClient, dataViews);
await loadDataViews(dataViewsContract);
const dataViewTitle = Array.isArray(item.config.source.index)
? item.config.source.index.join(',')
: item.config.source.index;
Expand All @@ -57,14 +56,7 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) =>
});
}
},
[
history,
savedObjectsClient,
dataViews,
toastNotifications,
loadDataViews,
getDataViewIdByTitle,
]
[history, dataViewsContract, toastNotifications, loadDataViews, getDataViewIdByTitle]
);

const action: TransformListAction = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ const getDataViewTitleFromTargetIndex = (item: TransformListRow) =>

export type DiscoverAction = ReturnType<typeof useDiscoverAction>;
export const useDiscoverAction = (forceDisable: boolean) => {
const appDeps = useAppDependencies();
const { share } = appDeps;
const savedObjectsClient = appDeps.savedObjects.client;
const dataViews = appDeps.data.dataViews;
const isDiscoverAvailable = !!appDeps.application.capabilities.discover?.show;
const {
share,
data: { dataViews: dataViewsContract },
application: { capabilities },
} = useAppDependencies();
const isDiscoverAvailable = !!capabilities.discover?.show;

const { getDataViewIdByTitle, loadDataViews } = useSearchItems(undefined);

const [dataViewsLoaded, setDataViewsLoaded] = useState(false);

useEffect(() => {
async function checkDataViewAvailability() {
await loadDataViews(savedObjectsClient, dataViews);
await loadDataViews(dataViewsContract);
setDataViewsLoaded(true);
}

checkDataViewAvailability();
}, [dataViews, loadDataViews, savedObjectsClient]);
}, [loadDataViews, dataViewsContract]);

const clickHandler = useCallback(
(item: TransformListRow) => {
Expand Down
16 changes: 4 additions & 12 deletions x-pack/plugins/transform/public/app/services/es_index_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/

import { HttpSetup, SavedObjectsClientContract } from '@kbn/core/public';
import { DataView } from '@kbn/data-views-plugin/public';
import type { HttpSetup } from '@kbn/core/public';
import type { DataViewsContract } from '@kbn/data-views-plugin/public';
import { API_BASE_PATH } from '../../../common/constants';

export class IndexService {
Expand All @@ -18,16 +18,8 @@ export class IndexService {
return privilege.hasAllPrivileges;
}

async dataViewExists(savedObjectsClient: SavedObjectsClientContract, indexName: string) {
const response = await savedObjectsClient.find<DataView>({
type: 'index-pattern',
perPage: 1,
search: `"${indexName}"`,
searchFields: ['title'],
fields: ['title'],
});
const ip = response.savedObjects.find((obj) => obj.attributes.title === indexName);
return ip !== undefined;
async dataViewExists(dataViewsContract: DataViewsContract, indexName: string) {
return (await dataViewsContract.find(indexName)).some(({ title }) => title === indexName);
}
}

Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/transform/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { i18n as kbnI18n } from '@kbn/i18n';

import type { CoreSetup } from '@kbn/core/public';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
import type { HomePublicPluginSetup } from '@kbn/home-plugin/public';
import type { SavedObjectsStart } from '@kbn/saved-objects-plugin/public';
import type { ManagementSetup } from '@kbn/management-plugin/public';
Expand All @@ -21,6 +22,7 @@ import { getTransformHealthRuleType } from './alerting';

export interface PluginsDependencies {
data: DataPublicPluginStart;
dataViews: DataViewsPublicPluginStart;
management: ManagementSetup;
home: HomePublicPluginSetup;
savedObjects: SavedObjectsStart;
Expand Down
11 changes: 6 additions & 5 deletions x-pack/plugins/transform/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/

import { i18n } from '@kbn/i18n';
import { CoreSetup, Plugin, Logger, PluginInitializerContext } from '@kbn/core/server';
import { CoreSetup, CoreStart, Plugin, Logger, PluginInitializerContext } from '@kbn/core/server';

import { LicenseType } from '@kbn/licensing-plugin/common/types';

import { Dependencies } from './types';
import { PluginSetupDependencies, PluginStartDependencies } from './types';
import { ApiRoutes } from './routes';
import { License } from './services';
import { registerTransformHealthRuleType } from './lib/alerting';
Expand Down Expand Up @@ -38,8 +38,8 @@ export class TransformServerPlugin implements Plugin<{}, void, any, any> {
}

setup(
{ http, getStartServices, elasticsearch }: CoreSetup,
{ licensing, features, alerting }: Dependencies
{ http, getStartServices, elasticsearch }: CoreSetup<PluginStartDependencies>,
{ licensing, features, alerting }: PluginSetupDependencies
): {} {
const router = http.createRouter();

Expand Down Expand Up @@ -74,6 +74,7 @@ export class TransformServerPlugin implements Plugin<{}, void, any, any> {
this.apiRoutes.setup({
router,
license: this.license,
getStartServices,
});

if (alerting) {
Expand All @@ -83,7 +84,7 @@ export class TransformServerPlugin implements Plugin<{}, void, any, any> {
return {};
}

start() {}
start(core: CoreStart, plugins: PluginStartDependencies) {}

stop() {}
}
Loading

0 comments on commit 69efa44

Please sign in to comment.