[data views] add getDefaultDataView method (#113891)
* add new method to data views api * add tests Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
0bf0b94ee0
commit
708f06fc15
|
@ -50,9 +50,15 @@ describe('IndexPatterns', () => {
|
|||
let indexPatterns: DataViewsService;
|
||||
let savedObjectsClient: SavedObjectsClientCommon;
|
||||
let SOClientGetDelay = 0;
|
||||
const uiSettings = {
|
||||
get: () => Promise.resolve(false),
|
||||
getAll: () => {},
|
||||
set: () => () => {},
|
||||
remove: jest.fn(),
|
||||
} as any as UiSettingsCommon;
|
||||
const indexPatternObj = { id: 'id', version: 'a', attributes: { title: 'title' } };
|
||||
|
||||
beforeEach(() => {
|
||||
const indexPatternObj = { id: 'id', version: 'a', attributes: { title: 'title' } };
|
||||
savedObjectsClient = {} as SavedObjectsClientCommon;
|
||||
savedObjectsClient.find = jest.fn(
|
||||
() => Promise.resolve([indexPatternObj]) as Promise<Array<SavedObject<any>>>
|
||||
|
@ -86,10 +92,7 @@ describe('IndexPatterns', () => {
|
|||
});
|
||||
|
||||
indexPatterns = new DataViewsService({
|
||||
uiSettings: {
|
||||
get: () => Promise.resolve(false),
|
||||
getAll: () => {},
|
||||
} as any as UiSettingsCommon,
|
||||
uiSettings,
|
||||
savedObjectsClient: savedObjectsClient as unknown as SavedObjectsClientCommon,
|
||||
apiClient: createFieldsFetcher(),
|
||||
fieldFormats,
|
||||
|
@ -274,4 +277,49 @@ describe('IndexPatterns', () => {
|
|||
// successful subsequent request
|
||||
expect(async () => await indexPatterns.get(id)).toBeDefined();
|
||||
});
|
||||
|
||||
describe('getDefaultDataView', () => {
|
||||
test('gets default data view', async () => {
|
||||
indexPatterns.clearCache();
|
||||
jest.clearAllMocks();
|
||||
|
||||
expect(await indexPatterns.getDefaultDataView()).toBeInstanceOf(DataView);
|
||||
// make sure we're not pulling from cache
|
||||
expect(savedObjectsClient.get).toBeCalledTimes(1);
|
||||
expect(savedObjectsClient.find).toBeCalledTimes(1);
|
||||
});
|
||||
|
||||
test('returns undefined if no data views exist', async () => {
|
||||
savedObjectsClient.find = jest.fn(
|
||||
() => Promise.resolve([]) as Promise<Array<SavedObject<any>>>
|
||||
);
|
||||
savedObjectsClient.get = jest.fn(() => Promise.resolve(undefined) as Promise<any>);
|
||||
indexPatterns.clearCache();
|
||||
expect(await indexPatterns.getDefaultDataView()).toBeUndefined();
|
||||
});
|
||||
|
||||
test("default doesn't exist, grabs another data view", async () => {
|
||||
indexPatterns.clearCache();
|
||||
jest.clearAllMocks();
|
||||
uiSettings.get = jest.fn().mockResolvedValue(['bar']);
|
||||
|
||||
savedObjectsClient.find = jest.fn(
|
||||
() => Promise.resolve([indexPatternObj]) as Promise<Array<SavedObject<any>>>
|
||||
);
|
||||
|
||||
savedObjectsClient.get = jest.fn().mockResolvedValue({
|
||||
id: 'bar',
|
||||
version: 'foo',
|
||||
attributes: {
|
||||
title: 'something',
|
||||
},
|
||||
});
|
||||
|
||||
expect(await indexPatterns.getDefaultDataView()).toBeInstanceOf(DataView);
|
||||
// make sure we're not pulling from cache
|
||||
expect(savedObjectsClient.get).toBeCalledTimes(1);
|
||||
expect(savedObjectsClient.find).toBeCalledTimes(1);
|
||||
expect(uiSettings.remove).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -79,6 +79,10 @@ export class DataViewsService {
|
|||
private onError: OnError;
|
||||
private dataViewCache: ReturnType<typeof createDataViewCache>;
|
||||
|
||||
/**
|
||||
* @deprecated Use `getDefaultDataView` instead (when loading data view) and handle
|
||||
* 'no data view' case in api consumer code - no more auto redirect
|
||||
*/
|
||||
ensureDefaultDataView: EnsureDefaultDataView;
|
||||
|
||||
constructor({
|
||||
|
@ -681,6 +685,33 @@ export class DataViewsService {
|
|||
this.dataViewCache.clear(indexPatternId);
|
||||
return this.savedObjectsClient.delete(DATA_VIEW_SAVED_OBJECT_TYPE, indexPatternId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default data view as an object. If no default is found, or it is missing
|
||||
* another data view is selected as default and returned.
|
||||
* @returns default data view
|
||||
*/
|
||||
|
||||
async getDefaultDataView() {
|
||||
const patterns = await this.getIds();
|
||||
let defaultId = await this.config.get('defaultIndex');
|
||||
let defined = !!defaultId;
|
||||
const exists = patterns.includes(defaultId);
|
||||
|
||||
if (defined && !exists) {
|
||||
await this.config.remove('defaultIndex');
|
||||
defaultId = defined = false;
|
||||
}
|
||||
|
||||
if (patterns.length >= 1 && (await this.hasUserDataView().catch(() => true))) {
|
||||
defaultId = patterns[0];
|
||||
await this.config.set('defaultIndex', defaultId);
|
||||
}
|
||||
|
||||
if (defaultId) {
|
||||
return this.get(defaultId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue