Skip to content

Commit

Permalink
tsdb support in lens (#152533)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Mar 8, 2023
1 parent e70c4f9 commit 8a11060
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/aggs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
aggMedian,
aggMin,
aggMovingAvg,
aggRate,
AggParamsAvg,
AggParamsBucketAvg,
AggParamsBucketAvgSerialized,
Expand Down Expand Up @@ -309,4 +310,5 @@ export interface AggFunctionsMapping {
aggSum: ReturnType<typeof aggSum>;
aggTopHit: ReturnType<typeof aggTopHit>;
aggTopMetrics: ReturnType<typeof aggTopMetrics>;
aggRate: ReturnType<typeof aggRate>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { adjustTimeScaleLabelSuffix } from '../../time_scale_utils';
import type { ReferenceBasedIndexPatternColumn } from '../column_types';
import { getManagedColumnsFrom, isColumnValidAsReference } from '../../layer_helpers';
import { operationDefinitionMap } from '..';
import { FieldBasedIndexPatternColumn } from '../../../types';
import { IndexPatternField } from '../../../../../types';

export const buildLabelFunction =
(ofName: (name?: string) => string) =>
Expand Down Expand Up @@ -203,3 +205,45 @@ export function optionallHistogramBasedOperationToExpression(
},
];
}

function isMetricCounterField(field?: IndexPatternField) {
return field?.timeSeriesMetric === 'counter';
}

function checkReferencedColumnMetric(
layer: FormBasedLayer,
columnId: string,
indexPattern: IndexPattern
) {
const column = layer.columns[columnId] as ReferenceBasedIndexPatternColumn;
return column.references
.filter((referencedId) => 'sourceField' in layer.columns[referencedId])
.map((referencedId) => {
const fieldName = (layer.columns[referencedId] as FieldBasedIndexPatternColumn).sourceField;
if (!isMetricCounterField(indexPattern.getFieldByName(fieldName))) {
return i18n.translate('xpack.lens.indexPattern.invalidReferenceConfiguration', {
defaultMessage: 'Dimension "{dimensionLabel}" is configured incorrectly',
values: {
dimensionLabel: layer.columns[referencedId].label,
},
});
}
});
}

export function getErrorForRateReference(
layer: FormBasedLayer,
columnId: string,
name: string,
indexPattern: IndexPattern
) {
const dateErrors = checkForDateHistogram(layer, name) ?? [];
const referenceErrors = checkReferences(layer, columnId) ?? [];
const metricCounterErrors = checkReferencedColumnMetric(layer, columnId, indexPattern) ?? [];
if (metricCounterErrors.length) {
return metricCounterErrors.concat(referenceErrors);
}
if (dateErrors.length) {
return dateErrors.concat(referenceErrors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ export const cardinalityOperation: OperationDefinition<
}),
allowAsReference: true,
input: 'field',
getPossibleOperationForField: ({ aggregationRestrictions, aggregatable, type }) => {
getPossibleOperationForField: ({
aggregationRestrictions,
aggregatable,
type,
timeSeriesMetric,
}) => {
if (
supportedTypes.has(type) &&
aggregatable &&
timeSeriesMetric !== 'counter' &&
(!aggregationRestrictions || aggregationRestrictions.cardinality)
) {
return { dataType: 'number', isBucketed: IS_BUCKETED, scale: SCALE };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ export const countOperation: OperationDefinition<CountIndexPatternColumn, 'field
sourceField: field.name,
};
},
getPossibleOperationForField: ({ aggregationRestrictions, aggregatable, type }) => {
getPossibleOperationForField: ({
aggregationRestrictions,
aggregatable,
type,
timeSeriesMetric,
}) => {
if (
type === 'document' ||
(aggregatable &&
timeSeriesMetric !== 'counter' &&
(!aggregationRestrictions || aggregationRestrictions.value_count) &&
supportedTypes.has(type))
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,7 @@ export function getFilter(
}
return filter;
}

export function isMetricCounterField(field?: IndexPatternField) {
return field?.timeSeriesMetric === 'counter';
}
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ interface FieldlessOperationDefinition<C extends BaseIndexPatternColumn, P = {}>
* Returns the meta data of the operation if applied. Undefined
* if the field is not applicable.
*/
getPossibleOperation: () => OperationMetadata;
getPossibleOperation: (index?: IndexPattern) => OperationMetadata | undefined;
/**
* Function turning a column into an agg config passed to the `esaggs` function
* together with the agg configs returned from other columns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export const lastValueOperation: OperationDefinition<
: oldColumn.filter,
};
},
getPossibleOperationForField: ({ aggregationRestrictions, type }) => {
if (supportedTypes.has(type) && !aggregationRestrictions) {
getPossibleOperationForField: ({ aggregationRestrictions, type, timeSeriesMetric }) => {
if (supportedTypes.has(type) && !aggregationRestrictions && timeSeriesMetric !== 'counter') {
return {
dataType: type as DataType,
isBucketed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const typeToFn: Record<string, string> = {

const supportedTypes = ['number', 'histogram'];

function isTimeSeriesCompatible(type: string, timeSeriesMetric?: string) {
return timeSeriesMetric !== 'counter' || ['min', 'max'].includes(type);
}

function buildMetricOperation<T extends MetricColumn<string>>({
type,
displayName,
Expand Down Expand Up @@ -94,10 +98,16 @@ function buildMetricOperation<T extends MetricColumn<string>>({
description,
input: 'field',
timeScalingMode: optionalTimeScaling ? 'optional' : undefined,
getPossibleOperationForField: ({ aggregationRestrictions, aggregatable, type: fieldType }) => {
getPossibleOperationForField: ({
aggregationRestrictions,
aggregatable,
type: fieldType,
timeSeriesMetric,
}) => {
if (
(supportedTypes.includes(fieldType) || (supportsDate && fieldType === 'date')) &&
aggregatable &&
isTimeSeriesCompatible(type, timeSeriesMetric) &&
(!aggregationRestrictions || aggregationRestrictions[type])
) {
return {
Expand All @@ -113,6 +123,7 @@ function buildMetricOperation<T extends MetricColumn<string>>({
newField &&
supportedTypes.includes(newField.type) &&
newField.aggregatable &&
isTimeSeriesCompatible(type, newField.timeSeriesMetric) &&
(!newField.aggregationRestrictions || newField.aggregationRestrictions![type])
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ export const percentileOperation: OperationDefinition<
filterable: true,
shiftable: true,
canReduceTimeRange: true,
getPossibleOperationForField: ({ aggregationRestrictions, aggregatable, type: fieldType }) => {
getPossibleOperationForField: ({
aggregationRestrictions,
aggregatable,
type: fieldType,
timeSeriesMetric,
}) => {
if (
supportedFieldTypes.includes(fieldType) &&
aggregatable &&
timeSeriesMetric !== 'counter' &&
(!aggregationRestrictions || aggregationRestrictions.percentiles)
) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ export const percentileRanksOperation: OperationDefinition<
filterable: true,
shiftable: true,
canReduceTimeRange: true,
getPossibleOperationForField: ({ aggregationRestrictions, aggregatable, type: fieldType }) => {
getPossibleOperationForField: ({
aggregationRestrictions,
aggregatable,
type: fieldType,
timeSeriesMetric,
}) => {
if (
supportedFieldTypes.includes(fieldType) &&
aggregatable &&
timeSeriesMetric !== 'counter' &&
(!aggregationRestrictions || !aggregationRestrictions.percentile_ranks)
) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export function insertNewColumn({
// TODO: need to create on the fly the new columns for Formula,
// like we do for fullReferences to show a seamless transition
}
const possibleOperation = operationDefinition.getPossibleOperation();
const possibleOperation = operationDefinition.getPossibleOperation(indexPattern);
const isBucketed = Boolean(possibleOperation?.isBucketed);
const addOperationFn = isBucketed ? addBucket : addMetric;
const buildColumnFn = columnParams
Expand Down Expand Up @@ -1689,7 +1689,7 @@ export function isOperationAllowedAsReference({
hasValidMetadata =
Boolean(metadata) && validation.validateMetadata(metadata!, operationType, field.name);
} else if (operationDefinition.input === 'none') {
const metadata = operationDefinition.getPossibleOperation();
const metadata = operationDefinition.getPossibleOperation(indexPattern);
hasValidMetadata = Boolean(metadata) && validation.validateMetadata(metadata!, operationType);
} else if (operationDefinition.input === 'fullReference') {
const metadata = operationDefinition.getPossibleOperation(indexPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,16 @@ export function getAvailableOperationsByMetadata(
);
});
} else if (operationDefinition.input === 'none') {
addToMap(
{
type: 'none',
operationType: operationDefinition.type,
},
operationDefinition.getPossibleOperation()
);
const validOperation = operationDefinition.getPossibleOperation(indexPattern);
if (validOperation) {
addToMap(
{
type: 'none',
operationType: operationDefinition.type,
},
validOperation
);
}
} else if (operationDefinition.input === 'fullReference') {
const validOperation = operationDefinition.getPossibleOperation(indexPattern);
if (validOperation) {
Expand Down

0 comments on commit 8a11060

Please sign in to comment.