Skip to content

Commit

Permalink
Move indexPattern.popularizeField into discover (#77668) (#78895)
Browse files Browse the repository at this point in the history
Move indexPattern.popularizeField into discover
  • Loading branch information
mattkime authored Sep 30, 2020
1 parent 4d322d0 commit 72388e6
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Constructs a new instance of the `IndexPattern` class
<b>Signature:</b>

```typescript
constructor({ spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
constructor({ spec, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| { spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, } | <code>IndexPatternDeps</code> | |
| { spec, fieldFormats, shortDotsEnable, metaFields, } | <code>IndexPatternDeps</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare class IndexPattern implements IIndexPattern
| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)({ spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, })](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
| [(constructor)({ spec, fieldFormats, shortDotsEnable, metaFields, })](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
## Properties
Expand Down Expand Up @@ -54,8 +54,11 @@ export declare class IndexPattern implements IIndexPattern
| [getTimeField()](./kibana-plugin-plugins-data-public.indexpattern.gettimefield.md) | | |
| [isTimeBased()](./kibana-plugin-plugins-data-public.indexpattern.istimebased.md) | | |
| [isTimeNanosBased()](./kibana-plugin-plugins-data-public.indexpattern.istimenanosbased.md) | | |
<<<<<<< HEAD
| [isUnsupportedTimePattern()](./kibana-plugin-plugins-data-public.indexpattern.isunsupportedtimepattern.md) | | |
| [popularizeField(fieldName, unit)](./kibana-plugin-plugins-data-public.indexpattern.popularizefield.md) | | |
=======
>>>>>>> cdb922f2a2... Move indexPattern.popularizeField into discover (#77668)
| [removeScriptedField(fieldName)](./kibana-plugin-plugins-data-public.indexpattern.removescriptedfield.md) | | Remove scripted field from field list |
| [toSpec()](./kibana-plugin-plugins-data-public.indexpattern.tospec.md) | | |

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export declare class IndexPatternsService
| [createAndSave(spec, override, skipFetchFields)](./kibana-plugin-plugins-data-public.indexpatternsservice.createandsave.md) | | Create a new index pattern and save it right away |
| [createSavedObject(indexPattern, override)](./kibana-plugin-plugins-data-public.indexpatternsservice.createsavedobject.md) | | Save a new index pattern |
| [delete(indexPatternId)](./kibana-plugin-plugins-data-public.indexpatternsservice.delete.md) | | Deletes an index pattern from .kibana index |
<<<<<<< HEAD
| [migrate(indexPattern, newTitle)](./kibana-plugin-plugins-data-public.indexpatternsservice.migrate.md) | | |
| [updateSavedObject(indexPattern, saveAttempts)](./kibana-plugin-plugins-data-public.indexpatternsservice.updatesavedobject.md) | | Save existing index pattern. Will attempt to merge differences if there are conflicts |
=======
| [updateSavedObject(indexPattern, saveAttempts, ignoreErrors)](./kibana-plugin-plugins-data-public.indexpatternsservice.updatesavedobject.md) | | Save existing index pattern. Will attempt to merge differences if there are conflicts |
>>>>>>> cdb922f2a2... Move indexPattern.popularizeField into discover (#77668)

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Save existing index pattern. Will attempt to merge differences if there are conf
<b>Signature:</b>

```typescript
updateSavedObject(indexPattern: IndexPattern, saveAttempts?: number): Promise<void | Error>;
updateSavedObject(indexPattern: IndexPattern, saveAttempts?: number, ignoreErrors?: boolean): Promise<void | Error>;
```

## Parameters
Expand All @@ -18,6 +18,7 @@ updateSavedObject(indexPattern: IndexPattern, saveAttempts?: number): Promise<vo
| --- | --- | --- |
| indexPattern | <code>IndexPattern</code> | |
| saveAttempts | <code>number</code> | |
| ignoreErrors | <code>boolean</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Constructs a new instance of the `IndexPattern` class
<b>Signature:</b>

```typescript
constructor({ spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
constructor({ spec, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| { spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, } | <code>IndexPatternDeps</code> | |
| { spec, fieldFormats, shortDotsEnable, metaFields, } | <code>IndexPatternDeps</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare class IndexPattern implements IIndexPattern
| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)({ spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, })](./kibana-plugin-plugins-data-server.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
| [(constructor)({ spec, fieldFormats, shortDotsEnable, metaFields, })](./kibana-plugin-plugins-data-server.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
## Properties
Expand Down Expand Up @@ -54,8 +54,11 @@ export declare class IndexPattern implements IIndexPattern
| [getTimeField()](./kibana-plugin-plugins-data-server.indexpattern.gettimefield.md) | | |
| [isTimeBased()](./kibana-plugin-plugins-data-server.indexpattern.istimebased.md) | | |
| [isTimeNanosBased()](./kibana-plugin-plugins-data-server.indexpattern.istimenanosbased.md) | | |
<<<<<<< HEAD
| [isUnsupportedTimePattern()](./kibana-plugin-plugins-data-server.indexpattern.isunsupportedtimepattern.md) | | |
| [popularizeField(fieldName, unit)](./kibana-plugin-plugins-data-server.indexpattern.popularizefield.md) | | |
=======
>>>>>>> cdb922f2a2... Move indexPattern.popularizeField into discover (#77668)
| [removeScriptedField(fieldName)](./kibana-plugin-plugins-data-server.indexpattern.removescriptedfield.md) | | Remove scripted field from field list |
| [toSpec()](./kibana-plugin-plugins-data-server.indexpattern.tospec.md) | | |

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ describe('IndexPattern', () => {

describe('api', () => {
test('should have expected properties', () => {
expect(indexPattern).toHaveProperty('popularizeField');
expect(indexPattern).toHaveProperty('getScriptedFields');
expect(indexPattern).toHaveProperty('getNonScriptedFields');
expect(indexPattern).toHaveProperty('addScriptedField');
Expand Down Expand Up @@ -241,50 +240,4 @@ describe('IndexPattern', () => {
expect(restoredPattern.fieldFormatMap.bytes instanceof MockFieldFormatter).toEqual(true);
});
});

describe('popularizeField', () => {
test('should increment the popularity count by default', () => {
indexPattern.fields.forEach(async (field) => {
const oldCount = field.count || 0;

await indexPattern.popularizeField(field.name);

expect(field.count).toEqual(oldCount + 1);
});
});

test('should increment the popularity count', () => {
indexPattern.fields.forEach(async (field) => {
const oldCount = field.count || 0;
const incrementAmount = 4;

await indexPattern.popularizeField(field.name, incrementAmount);

expect(field.count).toEqual(oldCount + incrementAmount);
});
});

test('should decrement the popularity count', () => {
indexPattern.fields.forEach(async (field) => {
const oldCount = field.count || 0;
const incrementAmount = 4;
const decrementAmount = -2;

await indexPattern.popularizeField(field.name, incrementAmount);
await indexPattern.popularizeField(field.name, decrementAmount);

expect(field.count).toEqual(oldCount + incrementAmount + decrementAmount);
});
});

test('should not go below 0', () => {
indexPattern.fields.forEach(async (field) => {
const decrementAmount = -Number.MAX_VALUE;

await indexPattern.popularizeField(field.name, decrementAmount);

expect(field.count).toEqual(0);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,18 @@ export class IndexPattern implements IIndexPattern {
public metaFields: string[];
// savedObject version
public version: string | undefined;
private savedObjectsClient: SavedObjectsClientCommon;
public sourceFilters?: SourceFilter[];
private originalSavedObjectBody: SavedObjectBody = {};
private shortDotsEnable: boolean = false;
private fieldFormats: FieldFormatsStartCommon;

constructor({
spec = {},
savedObjectsClient,
fieldFormats,
shortDotsEnable = false,
metaFields = [],
}: IndexPatternDeps) {
// set dependencies
this.savedObjectsClient = savedObjectsClient;
this.fieldFormats = fieldFormats;
// set config
this.shortDotsEnable = shortDotsEnable;
Expand Down Expand Up @@ -267,39 +264,6 @@ export class IndexPattern implements IIndexPattern {
}
}

async popularizeField(fieldName: string, unit = 1) {
/**
* This function is just used by Discover and it's high likely to be removed in the near future
* It doesn't use the save function to skip the error message that's displayed when
* a user adds several columns in a higher frequency that the changes can be persisted to ES
* resulting in 409 errors
*/
if (!this.id) return;
const field = this.fields.getByName(fieldName);
if (!field) {
return;
}
const count = Math.max((field.count || 0) + unit, 0);
if (field.count === count) {
return;
}
field.count = count;

try {
const res = await this.savedObjectsClient.update(
'index-pattern',
this.id,
this.getAsSavedObjectBody(),
{
version: this.version,
}
);
this.version = res.version;
} catch (e) {
// no need for an error message here
}
}

getNonScriptedFields() {
return [...this.fields.getAll().filter((field) => !field.scripted)];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ export class IndexPatternsService {

async updateSavedObject(
indexPattern: IndexPattern,
saveAttempts: number = 0
saveAttempts: number = 0,
ignoreErrors: boolean = false
): Promise<void | Error> {
if (!indexPattern.id) return;

Expand Down Expand Up @@ -600,6 +601,9 @@ export class IndexPatternsService {
}

if (unresolvedCollision) {
if (ignoreErrors) {
return;
}
const title = i18n.translate('data.indexPatterns.unableWriteLabel', {
defaultMessage:
'Unable to write index pattern! Refresh the page to get the most up to date changes for this index pattern.',
Expand All @@ -619,7 +623,7 @@ export class IndexPatternsService {
indexPatternCache.clear(indexPattern.id!);

// Try the save again
return this.updateSavedObject(indexPattern, saveAttempts);
return this.updateSavedObject(indexPattern, saveAttempts, ignoreErrors);
}
throw err;
});
Expand Down
6 changes: 2 additions & 4 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ export type IMetricAggType = MetricAggType;
// @public (undocumented)
export class IndexPattern implements IIndexPattern {
// Warning: (ae-forgotten-export) The symbol "IndexPatternDeps" needs to be exported by the entry point index.d.ts
constructor({ spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
constructor({ spec, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
addScriptedField(name: string, script: string, fieldType?: string): Promise<void>;
// (undocumented)
fieldFormatMap: Record<string, any>;
Expand Down Expand Up @@ -1169,8 +1169,6 @@ export class IndexPattern implements IIndexPattern {
isUnsupportedTimePattern(): boolean;
// (undocumented)
metaFields: string[];
// (undocumented)
popularizeField(fieldName: string, unit?: number): Promise<void>;
removeScriptedField(fieldName: string): void;
resetOriginalSavedObjectBody: () => void;
// Warning: (ae-forgotten-export) The symbol "SourceFilter" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -1394,7 +1392,7 @@ export class IndexPatternsService {
refreshFields: (indexPattern: IndexPattern) => Promise<void>;
savedObjectToSpec: (savedObject: SavedObject<IndexPatternAttributes>) => IndexPatternSpec;
setDefault: (id: string, force?: boolean) => Promise<void>;
updateSavedObject(indexPattern: IndexPattern, saveAttempts?: number): Promise<void | Error>;
updateSavedObject(indexPattern: IndexPattern, saveAttempts?: number, ignoreErrors?: boolean): Promise<void | Error>;
}

// Warning: (ae-missing-release-tag) "TypeMeta" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ export type IMetricAggType = MetricAggType;
// @public (undocumented)
export class IndexPattern implements IIndexPattern {
// Warning: (ae-forgotten-export) The symbol "IndexPatternDeps" needs to be exported by the entry point index.d.ts
constructor({ spec, savedObjectsClient, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
constructor({ spec, fieldFormats, shortDotsEnable, metaFields, }: IndexPatternDeps);
addScriptedField(name: string, script: string, fieldType?: string): Promise<void>;
// (undocumented)
fieldFormatMap: Record<string, any>;
Expand Down Expand Up @@ -586,8 +586,6 @@ export class IndexPattern implements IIndexPattern {
isUnsupportedTimePattern(): boolean;
// (undocumented)
metaFields: string[];
// (undocumented)
popularizeField(fieldName: string, unit?: number): Promise<void>;
removeScriptedField(fieldName: string): void;
resetOriginalSavedObjectBody: () => void;
// Warning: (ae-forgotten-export) The symbol "SourceFilter" 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 @@ -19,6 +19,7 @@

import _ from 'lodash';
import { esFilters } from '../../../../../../data/public';
import { popularizeField } from '../../../helpers/popularize_field';

import { MAX_CONTEXT_SIZE, MIN_CONTEXT_SIZE, QUERY_PARAMETER_KEYS } from './constants';

Expand Down Expand Up @@ -56,7 +57,7 @@ export function getQueryParameterActions(filterManager, indexPatterns) {
filterManager.addFilters(newFilters);
if (indexPatterns) {
const indexPattern = await indexPatterns.get(indexPatternId);
indexPattern.popularizeField(field.name, 1);
await popularizeField(indexPattern, field.name, indexPatterns);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ getAngularModule().directive('contextApp', function ContextApp() {
});

function ContextAppController($scope, Private) {
const { filterManager, indexpatterns, uiSettings } = getServices();
const queryParameterActions = getQueryParameterActions(filterManager, indexpatterns);
const { filterManager, indexPatterns, uiSettings } = getServices();
const queryParameterActions = getQueryParameterActions(filterManager, indexPatterns);
const queryActions = Private(QueryActionsProvider);
this.state = createInitialState(
parseInt(uiSettings.get(CONTEXT_STEP_SETTING), 10),
Expand Down
Loading

0 comments on commit 72388e6

Please sign in to comment.