Skip to content

Commit

Permalink
TS cleanup on new IndexPatterns implementation. (elastic#42646)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Aug 7, 2019
1 parent ead4645 commit fb440e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
32 changes: 18 additions & 14 deletions src/legacy/ui/public/index_patterns/index_patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { SavedObjectsClientContract, SimpleSavedObject, UiSettingsClient } from 'src/core/public';
import { idx } from '@kbn/elastic-idx';
// @ts-ignore
import { fieldFormats } from '../registry/field_formats';

Expand All @@ -33,7 +34,7 @@ export class IndexPatterns {

private config: UiSettingsClient;
private savedObjectsClient: SavedObjectsClientContract;
private savedObjectsCache?: Array<SimpleSavedObject<{}>> | null;
private savedObjectsCache?: Array<SimpleSavedObject<Record<string, any>>> | null;

constructor(config: UiSettingsClient, savedObjectsClient: SavedObjectsClientContract) {
this.config = config;
Expand All @@ -48,35 +49,38 @@ export class IndexPatterns {
})).savedObjects;
}

getIds = async (refresh: boolean) => {
getIds = async (refresh: boolean = false) => {
if (!this.savedObjectsCache || refresh) {
await this.refreshSavedObjectsCache();
}
if (this.savedObjectsCache) {
return this.savedObjectsCache.map(obj => _.get(obj, 'id'));
if (!this.savedObjectsCache) {
return [];
}
return this.savedObjectsCache.map(obj => idx(obj, _ => _.id));
};

getTitles = async (refresh: boolean) => {
getTitles = async (refresh: boolean = false): Promise<string[]> => {
if (!this.savedObjectsCache || refresh) {
await this.refreshSavedObjectsCache();
}
if (this.savedObjectsCache) {
return this.savedObjectsCache.map(obj => _.get(obj, 'attributes.title'));
if (!this.savedObjectsCache) {
return [];
}
return this.savedObjectsCache.map(obj => idx(obj, _ => _.attributes.title));
};

getFields = async (fields: string[], refresh: boolean) => {
getFields = async (fields: string[], refresh: boolean = false) => {
if (!this.savedObjectsCache || refresh) {
await this.refreshSavedObjectsCache();
}
if (this.savedObjectsCache) {
return this.savedObjectsCache.map(obj => {
const result: Record<string, any> = {};
fields.forEach(f => (result[f] = _.get(obj, f) || _.get(obj, `attributes.${f}`)));
return result;
});
if (!this.savedObjectsCache) {
return [];
}
return this.savedObjectsCache.map((obj: Record<string, any>) => {
const result: Record<string, any> = {};
fields.forEach((f: string) => (result[f] = obj[f] || idx(obj, _ => _.attributes[f])));
return result;
});
};

clearCache = (id?: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const StepDetailsForm: SFC<Props> = React.memo(({ overrides = {}, onChang
}

try {
setIndexPatternTitles((await kibanaContext.indexPatterns.getTitles(false)) as string[]);
setIndexPatternTitles(await kibanaContext.indexPatterns.getTitles());
} catch (e) {
toastNotifications.addDanger(
i18n.translate('xpack.ml.dataframe.stepDetailsForm.errorGettingIndexPatternTitles', {
Expand Down

0 comments on commit fb440e7

Please sign in to comment.