Skip to content

Commit

Permalink
Fixed custom labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed Aug 23, 2022
1 parent 28e20f7 commit 9bd61fb
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,21 @@ export const cardinalityOperation: OperationDefinition<
filterable: true,
shiftable: true,
windowable: true,
getDefaultLabel: (column, indexPattern) =>
ofName(getSafeName(column.sourceField, indexPattern), column.timeShift, column.window),
getDefaultLabel: (column, indexPattern) => {
const { customLabel, label } = getSafeName(column.sourceField, indexPattern);
if (customLabel) {
return label;
}
return ofName(label, column.timeShift, column.window);
},
buildColumn({ field, previousColumn }, columnParams) {
const label =
field.customLabel ||
ofName(field.displayName, previousColumn?.timeShift, previousColumn?.window);

return {
label: ofName(field.displayName, previousColumn?.timeShift, previousColumn?.window),
label,
customLabel: Boolean(field.customLabel),
dataType: 'number',
operationType: OPERATION_TYPE,
scale: SCALE,
Expand Down Expand Up @@ -185,7 +195,8 @@ export const cardinalityOperation: OperationDefinition<
onFieldChange: (oldColumn, field) => {
return {
...oldColumn,
label: ofName(field.displayName, oldColumn.timeShift, oldColumn.window),
label: field.customLabel || ofName(field.displayName, oldColumn.timeShift, oldColumn.window),
customLabel: Boolean(field.customLabel),
sourceField: field.name,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ function ofName(
timeScale: string | undefined,
window: string | undefined
) {
if (field?.customLabel) {
return field.customLabel;
}

return adjustTimeScaleLabelSuffix(
field?.type !== 'document'
? i18n.translate('xpack.lens.indexPattern.valueCountOf', {
Expand Down Expand Up @@ -120,13 +116,12 @@ export const countOperation: OperationDefinition<CountIndexPatternColumn, 'field
return ofName(field, column.timeShift, column.timeScale, column.window);
},
buildColumn({ field, previousColumn }, columnParams) {
const label =
field.customLabel ||
ofName(field, previousColumn?.timeShift, previousColumn?.timeScale, previousColumn?.window);

return {
label: ofName(
field,
previousColumn?.timeShift,
previousColumn?.timeScale,
previousColumn?.window
),
label,
customLabel: Boolean(field.customLabel),
dataType: 'number',
operationType: 'count',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ export const dateHistogramOperation: OperationDefinition<
};
}
},
getDefaultLabel: (column, indexPattern) => getSafeName(column.sourceField, indexPattern),
getDefaultLabel: (column, indexPattern) => {
const { label } = getSafeName(column.sourceField, indexPattern);
return label;
},
buildColumn({ field }, columnParams) {
return {
label: field.displayName,
label: field.customLabel || field.displayName,
customLabel: Boolean(field.customLabel),
dataType: 'date',
operationType: 'date_histogram',
sourceField: field.name,
Expand All @@ -134,7 +138,7 @@ export const dateHistogramOperation: OperationDefinition<
onFieldChange: (oldColumn, field) => {
return {
...oldColumn,
label: field.displayName,
label: field.customLabel || field.displayName,
sourceField: field.name,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,28 @@ export function combineErrorMessages(
return messages.length ? messages : undefined;
}

export function getSafeName(name: string, indexPattern: IndexPattern): string {
export function getSafeName(name: string, indexPattern: IndexPattern) {
const field = indexPattern.getFieldByName(name);
return field
? field.displayName
: i18n.translate('xpack.lens.indexPattern.missingFieldLabel', {
if (!field) {
return {
label: i18n.translate('xpack.lens.indexPattern.missingFieldLabel', {
defaultMessage: 'Missing field',
});
}),
customLabel: false,
};
}

if (field.customLabel) {
return {
label: field.customLabel,
customLabel: true,
};
}

return {
label: field.displayName,
customLabel: false,
};
}

export function isValidNumber(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ export const lastValueOperation: OperationDefinition<
displayName: i18n.translate('xpack.lens.indexPattern.lastValue', {
defaultMessage: 'Last value',
}),
getDefaultLabel: (column, indexPattern) =>
ofName(getSafeName(column.sourceField, indexPattern), column.timeShift, column.window),
getDefaultLabel: (column, indexPattern) => {
const { customLabel, label } = getSafeName(column.sourceField, indexPattern);
if (customLabel) {
return label;
}
return ofName(label, column.timeShift, column.window);
},
input: 'field',
onFieldChange: (oldColumn, field) => {
const newParams = { ...oldColumn.params };
Expand All @@ -147,7 +152,7 @@ export const lastValueOperation: OperationDefinition<
return {
...oldColumn,
dataType: field.type as DataType,
label: ofName(field.displayName, oldColumn.timeShift, oldColumn.window),
label: field.customLabel || ofName(field.displayName, oldColumn.timeShift, oldColumn.window),
sourceField: field.name,
params: newParams,
scale: field.type === 'string' ? 'ordinal' : 'ratio',
Expand Down Expand Up @@ -209,7 +214,10 @@ export const lastValueOperation: OperationDefinition<
const showArrayValues = isScriptedField(field) || lastValueParams?.showArrayValues;

return {
label: ofName(field.displayName, previousColumn?.timeShift, previousColumn?.window),
label:
field.customLabel ||
ofName(field.displayName, previousColumn?.timeShift, previousColumn?.window),
customLabel: Boolean(field.customLabel),
dataType: field.type as DataType,
operationType: 'last_value',
isBucketed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ function buildMetricOperation<T extends MetricColumn<string>>({
documentationDescription?: string;
}) {
const labelLookup = (name: string, column?: BaseIndexPatternColumn) => {
const label = ofName(name);
return adjustTimeScaleLabelSuffix(
label,
name,
undefined,
optionalTimeScaling ? column?.timeScale : undefined,
undefined,
Expand Down Expand Up @@ -121,11 +120,19 @@ function buildMetricOperation<T extends MetricColumn<string>>({
optionalTimeScaling
? (adjustTimeScaleOnOtherColumnChange(layer, thisColumnId) as T)
: (layer.columns[thisColumnId] as T),
getDefaultLabel: (column, indexPattern, columns) =>
labelLookup(getSafeName(column.sourceField, indexPattern), column),
getDefaultLabel: (column, indexPattern, columns) => {
const { label, customLabel } = getSafeName(column.sourceField, indexPattern);
if (customLabel) {
return label;
}
return labelLookup(label, column);
},
buildColumn: ({ field, previousColumn }, columnParams) => {
return {
label: labelLookup(field.displayName, previousColumn),
label: field.customLabel
? field.customLabel
: labelLookup(field.displayName, previousColumn),
customLabel: Boolean(field.customLabel),
dataType: supportsDate && field.type === 'date' ? 'date' : 'number',
operationType: type,
sourceField: field.name,
Expand All @@ -147,7 +154,8 @@ function buildMetricOperation<T extends MetricColumn<string>>({
onFieldChange: (oldColumn, field) => {
return {
...oldColumn,
label: labelLookup(field.displayName, oldColumn),
label: field.customLabel || labelLookup(field.displayName, oldColumn),
customLabel: Boolean(field.customLabel),
dataType: field.type,
sourceField: field.name,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,28 @@ export const percentileOperation: OperationDefinition<
!newField.aggregationRestrictions
);
},
getDefaultLabel: (column, indexPattern, columns) =>
ofName(
getSafeName(column.sourceField, indexPattern),
column.params.percentile,
column.timeShift,
column.window
),
getDefaultLabel: (column, indexPattern, columns) => {
const { customLabel, label } = getSafeName(column.sourceField, indexPattern);
if (customLabel) {
return label;
}

return ofName(label, column.params.percentile, column.timeShift, column.window);
},
buildColumn: ({ field, previousColumn, indexPattern }, columnParams) => {
const existingPercentileParam =
previousColumn &&
isColumnOfType<PercentileIndexPatternColumn>('percentile', previousColumn) &&
previousColumn.params.percentile;
const newPercentileParam =
columnParams?.percentile ?? (existingPercentileParam || DEFAULT_PERCENTILE_VALUE);

const { customLabel, label } = getSafeName(field.name, indexPattern);
return {
label: ofName(
getSafeName(field.name, indexPattern),
newPercentileParam,
previousColumn?.timeShift,
previousColumn?.window
),
label: customLabel
? label
: ofName(label, newPercentileParam, previousColumn?.timeShift, previousColumn?.window),
customLabel,
dataType: 'number',
operationType: 'percentile',
sourceField: field.name,
Expand All @@ -145,12 +146,15 @@ export const percentileOperation: OperationDefinition<
onFieldChange: (oldColumn, field) => {
return {
...oldColumn,
label: ofName(
field.displayName,
oldColumn.params.percentile,
oldColumn.timeShift,
oldColumn.window
),
label:
field.customLabel ||
ofName(
field.displayName,
oldColumn.params.percentile,
oldColumn.timeShift,
oldColumn.window
),
customLabel: Boolean(field.customLabel),
sourceField: field.name,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,27 @@ export const percentileRanksOperation: OperationDefinition<
!newField.aggregationRestrictions
);
},
getDefaultLabel: (column, indexPattern, columns) =>
ofName(
getSafeName(column.sourceField, indexPattern),
column.params.value,
column.timeShift,
column.window
),
getDefaultLabel: (column, indexPattern, columns) => {
const { label, customLabel } = getSafeName(column.sourceField, indexPattern);
if (customLabel) {
return label;
}
return ofName(label, column.params.value, column.timeShift, column.window);
},
buildColumn: ({ field, previousColumn, indexPattern }, columnParams) => {
const existingPercentileRanksParam =
previousColumn &&
isColumnOfType<PercentileRanksIndexPatternColumn>('percentile_rank', previousColumn) &&
previousColumn.params.value;
const newPercentileRanksParam =
columnParams?.value ?? (existingPercentileRanksParam || DEFAULT_PERCENTILE_RANKS_VALUE);

const { label, customLabel } = getSafeName(field.name, indexPattern);

return {
label: ofName(
getSafeName(field.name, indexPattern),
newPercentileRanksParam,
previousColumn?.timeShift,
previousColumn?.window
),
label: customLabel
? label
: ofName(label, newPercentileRanksParam, previousColumn?.timeShift, previousColumn?.window),
dataType: 'number',
operationType: 'percentile_rank',
sourceField: field.name,
Expand All @@ -138,12 +138,10 @@ export const percentileRanksOperation: OperationDefinition<
onFieldChange: (oldColumn, field) => {
return {
...oldColumn,
label: ofName(
field.displayName,
oldColumn.params.value,
oldColumn.timeShift,
oldColumn.window
),
label:
field.customLabel ||
ofName(field.displayName, oldColumn.params.value, oldColumn.timeShift, oldColumn.window),
customLabel: Boolean(field.customLabel),
sourceField: field.name,
};
},
Expand Down

0 comments on commit 9bd61fb

Please sign in to comment.