Skip to content

Commit

Permalink
fix some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Nov 27, 2020
1 parent 7b25522 commit 37044aa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Returns an object matching a given title
<b>Signature:</b>

```typescript
findByTitle: (title: string) => Promise<IndexPattern | undefined>;
findByTitle: (title: string, refresh?: boolean) => Promise<IndexPattern | undefined>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export declare class IndexPatternsService
| [clearCache](./kibana-plugin-plugins-data-public.indexpatternsservice.clearcache.md) | | <code>(id?: string &#124; undefined) =&gt; void</code> | Clear index pattern list cache |
| [ensureDefaultIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.ensuredefaultindexpattern.md) | | <code>EnsureDefaultIndexPattern</code> | |
| [fieldArrayToMap](./kibana-plugin-plugins-data-public.indexpatternsservice.fieldarraytomap.md) | | <code>(fields: FieldSpec[], fieldAttrs?: FieldAttrs &#124; undefined) =&gt; Record&lt;string, FieldSpec&gt;</code> | Converts field array to map |
| [findByTitle](./kibana-plugin-plugins-data-public.indexpatternsservice.findbytitle.md) | | <code>(title: string) =&gt; Promise&lt;IndexPattern &#124; undefined&gt;</code> | Returns an object matching a given title |
| [findByTitle](./kibana-plugin-plugins-data-public.indexpatternsservice.findbytitle.md) | | <code>(title: string, refresh?: boolean) =&gt; Promise&lt;IndexPattern &#124; undefined&gt;</code> | Returns an object matching a given title |
| [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | <code>(id: string) =&gt; Promise&lt;IndexPattern&gt;</code> | Get an index pattern by id. Cache optimized |
| [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | <code>() =&gt; Promise&lt;SavedObject&lt;IndexPatternSavedObjectAttrs&gt;[] &#124; null &#124; undefined&gt;</code> | |
| [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | <code>() =&gt; Promise&lt;IndexPattern &#124; null&gt;</code> | Get default index pattern |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,16 @@ export class IndexPatternsService {
* @param title {string}
* @returns {Promise<SavedObject|undefined>}
*/
findByTitle = async (title: string) => {
const savedObject = await findByTitle(this.savedObjectsClient, title);
findByTitle = async (title: string, refresh: boolean = false) => {
if (!this.savedObjectsCache || refresh) {
await this.refreshSavedObjectsCache();
}
if (this.savedObjectsCache) {
const so = this.savedObjectsCache.find((obj) => obj.attributes.title === title);

if (savedObject?.id) {
return this.get(savedObject?.id);
if (so?.id) {
return this.get(so.id);
}
}
};

Expand Down
11 changes: 4 additions & 7 deletions src/plugins/data/common/index_patterns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { find } from 'lodash';
import { SavedObjectsClientCommon, SavedObject } from '..';
import type { IndexPatternSavedObjectAttrs } from './index_patterns';
import type { SavedObjectsClientCommon } from '../types';

/**
* Returns an object matching a given title
Expand All @@ -29,17 +29,14 @@ import { SavedObjectsClientCommon, SavedObject } from '..';
*/
export async function findByTitle(client: SavedObjectsClientCommon, title: string) {
if (title) {
const savedObjects = await client.find({
const savedObjects = await client.find<IndexPatternSavedObjectAttrs>({
type: 'index-pattern',
perPage: 10,
search: `"${title}"`,
searchFields: ['title'],
fields: ['title'],
});

return find(
savedObjects,
(obj: SavedObject<any>) => obj.attributes.title.toLowerCase() === title.toLowerCase()
);
return savedObjects.find((obj) => obj.attributes.title.toLowerCase() === title.toLowerCase());
}
}
2 changes: 1 addition & 1 deletion src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ export class IndexPatternsService {
// (undocumented)
ensureDefaultIndexPattern: EnsureDefaultIndexPattern;
fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record<string, FieldSpec>;
findByTitle: (title: string) => Promise<IndexPattern | undefined>;
findByTitle: (title: string, refresh?: boolean) => Promise<IndexPattern | undefined>;
get: (id: string) => Promise<IndexPattern>;
// Warning: (ae-forgotten-export) The symbol "IndexPatternSavedObjectAttrs" needs to be exported by the entry point index.d.ts
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import React, { memo, useCallback, useMemo, useState, useEffect } from 'react';

import { AppMountParameters, OverlayRef } from 'kibana/public';
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../../../../kibana_react/public';
import {
Expand All @@ -31,6 +30,7 @@ import {
} from '../types';
import { APP_NAME } from '../visualize_constants';
import { getTopNavConfig } from '../utils';
import type { IndexPattern } from '../../../../data/common/index_patterns';

interface VisualizeTopNavProps {
currentAppState: VisualizeAppState;
Expand Down Expand Up @@ -165,14 +165,23 @@ const TopNav = ({
]);

useEffect(() => {
const asyncSetIndexPattern = async () => {
let index: IndexPattern | null = null;

if (vis.type.getUsedIndexPattern) {
[index] = await vis.type.getUsedIndexPattern(vis.params);
}
if (!index) {
index = await services.data.indexPatterns.getDefault();
}
if (index) {
setIndexPattern(index);
}
};
if (!vis.data.indexPattern) {
services.data.indexPatterns.getDefault().then((index) => {
if (index) {
setIndexPattern(index);
}
});
asyncSetIndexPattern();
}
}, [services.data.indexPatterns, vis.data.indexPattern]);
}, [vis.params, vis.type, services.data.indexPatterns, vis.data.indexPattern]);

return isChromeVisible ? (
/**
Expand Down

0 comments on commit 37044aa

Please sign in to comment.