Skip to content

Commit

Permalink
Merge remote-tracking branch 'elastic/master' into monitoring/logstas…
Browse files Browse the repository at this point in the history
…h_pipelines_pagination
  • Loading branch information
chrisronline committed Sep 25, 2019
2 parents d8b7616 + 7ba6b78 commit 93df33f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('IndexPatternDimensionPanel', () => {
setState,
columnId: 'col1',
layerId: 'first',
uniqueLabel: 'stuff',
filterOperations: () => true,
storage: {} as Storage,
uiSettings: {} as UiSettingsClientContract,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type IndexPatternDimensionPanelProps = DatasourceDimensionPanelProps & {
savedObjectsClient: SavedObjectsClientContract;
layerId: string;
http: HttpServiceBase;
uniqueLabel: string;
};

export interface OperationFieldSupportMatrix {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function PopoverEditor(props: PopoverEditorProps) {
setState,
layerId,
currentIndexPattern,
uniqueLabel,
} = props;
const { operationByDocument, operationByField, fieldByOperation } = operationFieldSupportMatrix;
const [isPopoverOpen, setPopoverOpen] = useState(false);
Expand Down Expand Up @@ -219,7 +220,7 @@ export function PopoverEditor(props: PopoverEditorProps) {
defaultMessage: 'Edit configuration',
})}
>
{selectedColumn.label}
{uniqueLabel}
</EuiLink>
) : (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
IndexPatternPersistedState,
IndexPatternPrivateState,
IndexPatternColumn,
uniqueLabels,
} from './indexpattern';
import { DatasourcePublicAPI, Operation, Datasource } from '../types';
import { coreMock } from 'src/core/public/mocks';
Expand Down Expand Up @@ -170,6 +171,47 @@ describe('IndexPattern Data Source', () => {
};
});

describe('uniqueLabels', () => {
it('appends a suffix to duplicates', () => {
const col: IndexPatternColumn = {
dataType: 'number',
isBucketed: false,
label: 'Foo',
operationType: 'count',
};
const map = uniqueLabels({
a: {
columnOrder: ['a', 'b'],
columns: {
a: col,
b: col,
},
indexPatternId: 'foo',
},
b: {
columnOrder: ['c', 'd'],
columns: {
c: col,
d: {
...col,
label: 'Foo [1]',
},
},
indexPatternId: 'foo',
},
});

expect(map).toMatchInlineSnapshot(`
Object {
"a": "Foo",
"b": "Foo [1]",
"c": "Foo [2]",
"d": "Foo [1] [1]",
}
`);
});
});

describe('#initialize', () => {
it('should load a default state', async () => {
const state = await indexPatternDatasource.initialize();
Expand Down Expand Up @@ -239,13 +281,13 @@ describe('IndexPattern Data Source', () => {
};
const state = await indexPatternDatasource.initialize(queryPersistedState);
expect(indexPatternDatasource.toExpression(state, 'first')).toMatchInlineSnapshot(`
"esaggs
index=\\"1\\"
metricsAtAllLevels=false
partialRows=false
includeFormatHints=true
aggConfigs='[{\\"id\\":\\"col1\\",\\"enabled\\":true,\\"type\\":\\"count\\",\\"schema\\":\\"metric\\",\\"params\\":{}},{\\"id\\":\\"col2\\",\\"enabled\\":true,\\"type\\":\\"date_histogram\\",\\"schema\\":\\"segment\\",\\"params\\":{\\"field\\":\\"timestamp\\",\\"useNormalizedEsInterval\\":true,\\"interval\\":\\"1d\\",\\"drop_partials\\":false,\\"min_doc_count\\":1,\\"extended_bounds\\":{}}}]' | lens_rename_columns idMap='{\\"col-0-col1\\":\\"col1\\",\\"col-1-col2\\":\\"col2\\"}'"
`);
"esaggs
index=\\"1\\"
metricsAtAllLevels=false
partialRows=false
includeFormatHints=true
aggConfigs='[{\\"id\\":\\"col1\\",\\"enabled\\":true,\\"type\\":\\"count\\",\\"schema\\":\\"metric\\",\\"params\\":{}},{\\"id\\":\\"col2\\",\\"enabled\\":true,\\"type\\":\\"date_histogram\\",\\"schema\\":\\"segment\\",\\"params\\":{\\"field\\":\\"timestamp\\",\\"useNormalizedEsInterval\\":true,\\"interval\\":\\"1d\\",\\"drop_partials\\":false,\\"min_doc_count\\":1,\\"extended_bounds\\":{}}}]' | lens_rename_columns idMap='{\\"col-0-col1\\":\\"col1\\",\\"col-1-col2\\":\\"col2\\"}'"
`);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { render } from 'react-dom';
import { I18nProvider } from '@kbn/i18n/react';
import { CoreSetup, SavedObjectsClientContract } from 'src/core/public';
import { Storage } from 'ui/storage';
import { i18n } from '@kbn/i18n';
import {
DatasourceDimensionPanelProps,
DatasourceDataPanelProps,
Expand Down Expand Up @@ -97,16 +98,47 @@ export type IndexPatternPrivateState = IndexPatternPersistedState & {
showEmptyFields: boolean;
};

export function columnToOperation(column: IndexPatternColumn): Operation {
export function columnToOperation(column: IndexPatternColumn, uniqueLabel?: string): Operation {
const { dataType, label, isBucketed, scale } = column;
return {
label,
dataType,
isBucketed,
scale,
label: uniqueLabel || label,
};
}

/**
* Return a map of columnId => unique column label. Exported for testing reasons.
*/
export function uniqueLabels(layers: Record<string, IndexPatternLayer>) {
const columnLabelMap = {} as Record<string, string>;
const counts = {} as Record<string, number>;

const makeUnique = (label: string) => {
let uniqueLabel = label;

while (counts[uniqueLabel] >= 0) {
const num = ++counts[uniqueLabel];
uniqueLabel = i18n.translate('xpack.lens.indexPattern.uniqueLabel', {
defaultMessage: '{label} [{num}]',
values: { label, num },
});
}

counts[uniqueLabel] = 0;
return uniqueLabel;
};

Object.values(layers).forEach(layer => {
Object.entries(layer.columns).forEach(([columnId, column]) => {
columnLabelMap[columnId] = makeUnique(column.label);
});
});

return columnLabelMap;
}

type UnwrapPromise<T> = T extends Promise<infer P> ? P : T;
type InferFromArray<T> = T extends Array<infer P> ? P : T;

Expand Down Expand Up @@ -253,6 +285,8 @@ export function getIndexPatternDatasource({
setState: StateSetter<IndexPatternPrivateState>,
layerId: string
) {
const columnLabelMap = uniqueLabels(state.layers);

return {
getTableSpec: () => {
return state.layers[layerId].columnOrder.map(colId => ({ columnId: colId }));
Expand All @@ -261,7 +295,7 @@ export function getIndexPatternDatasource({
const layer = state.layers[layerId];

if (layer && layer.columns[columnId]) {
return columnToOperation(layer.columns[columnId]);
return columnToOperation(layer.columns[columnId], columnLabelMap[columnId]);
}
return null;
},
Expand All @@ -276,6 +310,7 @@ export function getIndexPatternDatasource({
savedObjectsClient={savedObjectsClient}
layerId={props.layerId}
http={core.http}
uniqueLabel={columnLabelMap[props.columnId]}
{...props}
/>
</I18nProvider>,
Expand Down

0 comments on commit 93df33f

Please sign in to comment.