Skip to content

Commit

Permalink
add new method to data views api
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime committed Oct 5, 2021
1 parent 8f7dd3f commit dbf036a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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);
}
}
}

/**
Expand Down

0 comments on commit dbf036a

Please sign in to comment.