Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move indexPattern.popularizeField into discover #77668

Merged
merged 16 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +54,6 @@ export declare class IndexPattern implements IIndexPattern
| [isTimeBased()](./kibana-plugin-plugins-data-public.indexpattern.istimebased.md) | | |
| [isTimeBasedWildcard()](./kibana-plugin-plugins-data-public.indexpattern.istimebasedwildcard.md) | | |
| [isTimeNanosBased()](./kibana-plugin-plugins-data-public.indexpattern.istimenanosbased.md) | | |
| [popularizeField(fieldName, unit)](./kibana-plugin-plugins-data-public.indexpattern.popularizefield.md) | | |
| [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,5 +43,5 @@ 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 |
| [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 |

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,7 +54,6 @@ export declare class IndexPattern implements IIndexPattern
| [isTimeBased()](./kibana-plugin-plugins-data-server.indexpattern.istimebased.md) | | |
| [isTimeBasedWildcard()](./kibana-plugin-plugins-data-server.indexpattern.istimebasedwildcard.md) | | |
| [isTimeNanosBased()](./kibana-plugin-plugins-data-server.indexpattern.istimenanosbased.md) | | |
| [popularizeField(fieldName, unit)](./kibana-plugin-plugins-data-server.indexpattern.popularizefield.md) | | |
| [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 @@ -242,50 +241,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 @@ -272,39 +269,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 @@ -516,7 +516,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 @@ -567,6 +568,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 @@ -586,7 +590,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 @@ -1085,7 +1085,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, lang?: string): Promise<void>;
// (undocumented)
fieldFormatMap: Record<string, any>;
Expand Down Expand Up @@ -1164,8 +1164,6 @@ export class IndexPattern implements IIndexPattern {
isTimeNanosBased(): 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 @@ -1387,7 +1385,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 @@ -623,7 +623,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, lang?: string): Promise<void>;
// (undocumented)
fieldFormatMap: Record<string, any>;
Expand Down Expand Up @@ -706,8 +706,6 @@ export class IndexPattern implements IIndexPattern {
isTimeNanosBased(): 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 @@ -55,8 +56,11 @@ export function getQueryParameterActions(filterManager, indexPatterns) {
);
filterManager.addFilters(newFilters);
if (indexPatterns) {
const indexPattern = await indexPatterns.get(indexPatternId);
indexPattern.popularizeField(field.name, 1);
try {
const indexPattern = await indexPatterns.get(indexPatternId);
await popularizeField(indexPattern, field.name, indexPatterns);
// eslint-disable-next-line no-empty
} catch {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the popularizeField function doesn't throw an Exception, I think you don't need to try{}catch{} here

}
};

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