Skip to content

Commit

Permalink
Added checking field type for formula
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Sep 23, 2022
1 parent 651353a commit 45a165c
Showing 1 changed file with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { DataView, DataViewField, METRIC_TYPES } from '@kbn/data-plugin/common';
import { SchemaConfig } from '../../..';
import { isFieldValid, SchemaConfig } from '../../..';
import { Operations } from '../../constants';
import { isMetricWithField, getStdDeviationFormula, ExtendedColumnConverterArgs } from '../convert';
import { getFormulaFromMetric, SUPPORTED_METRICS } from '../convert/supported_metrics';
Expand Down Expand Up @@ -59,14 +59,40 @@ const METRIC_OPS_WITHOUT_PARAMS: string[] = [
Operations.COUNT,
];

const isDataViewField = (field: string | DataViewField): field is DataViewField => {
if (field && typeof field === 'object') {
return true;
}
return false;
};

const isValidAgg = (agg: SchemaConfig<METRIC_TYPES>, dataView: DataView) => {
const aggregation = SUPPORTED_METRICS[agg.aggType];
if (!aggregation) {
return false;
}
if (isMetricWithField(agg)) {
if (!agg.aggParams?.field) {
return false;
}
const sourceField = getFieldNameFromField(agg.aggParams?.field);
const field = dataView.getFieldByName(sourceField!);
if (!isFieldValid(field, aggregation)) {
return false;
}
}

return true;
};

const getFormulaForAggsWithoutParams = (
agg: SchemaConfig<METRIC_TYPES>,
dataView: DataView,
selector: string | undefined,
reducedTimeRange?: string
) => {
const op = SUPPORTED_METRICS[agg.aggType];
if (!op) {
if (!isValidAgg(agg, dataView) || !op) {
return null;
}

Expand All @@ -82,7 +108,7 @@ const getFormulaForPercentileRanks = (
) => {
const value = Number(agg.aggId?.split('.')[1]);
const op = SUPPORTED_METRICS[agg.aggType];
if (!op) {
if (!isValidAgg(agg, dataView) || !op) {
return null;
}

Expand All @@ -98,7 +124,7 @@ const getFormulaForPercentile = (
) => {
const percentile = Number(agg.aggId?.split('.')[1]);
const op = SUPPORTED_METRICS[agg.aggType];
if (!op) {
if (!isValidAgg(agg, dataView) || !op) {
return null;
}

Expand All @@ -108,13 +134,6 @@ const getFormulaForPercentile = (
)})`;
};

const isDataViewField = (field: string | DataViewField): field is DataViewField => {
if (field && typeof field === 'object') {
return true;
}
return false;
};

const getFormulaForSubMetric = ({
agg,
dataView,
Expand Down Expand Up @@ -225,6 +244,9 @@ export const getFormulaForAgg = ({
}

if (isStdDevAgg(agg) && agg.aggId) {
if (!isValidAgg(agg, dataView)) {
return null;
}
return getStdDeviationFormula(agg.aggId, getFieldNameFromField(agg.aggParams?.field) ?? '');
}

Expand Down

0 comments on commit 45a165c

Please sign in to comment.