diff --git a/x-pack/plugins/ml/public/util/__tests__/chart_utils.js b/x-pack/plugins/ml/public/util/__tests__/chart_utils.js index a6a26f3fed8f..a6c8a9ed1ec1 100644 --- a/x-pack/plugins/ml/public/util/__tests__/chart_utils.js +++ b/x-pack/plugins/ml/public/util/__tests__/chart_utils.js @@ -201,10 +201,36 @@ describe('ML - chart utils', () => { ], }; + const overScriptFieldModelPlotConfig = { + metricFunction: 'count', + functionDescription: 'count', + fieldName: 'highest_registered_domain', + entityFields: [ + { + fieldName: 'highest_registered_domain', + fieldValue: 'elastic.co', + fieldType: 'over', + } + ], + datafeedConfig: { + script_fields: { + highest_registered_domain: { + script: { + source: 'return domainSplit(doc[\'query\'].value, params).get(1);', + lang: 'painless' + }, + ignore_failure: false + } + } + } + }; + it('returns single metric chart type as expected for configs', () => { expect(getChartType(singleMetricConfig)).to.be(CHART_TYPE.SINGLE_METRIC); expect(getChartType(multiMetricConfig)).to.be(CHART_TYPE.SINGLE_METRIC); expect(getChartType(varpModelPlotConfig)).to.be(CHART_TYPE.SINGLE_METRIC); + expect(getChartType(overScriptFieldModelPlotConfig)).to.be(CHART_TYPE.SINGLE_METRIC); + }); it('returns event distribution chart type as expected for configs', () => { diff --git a/x-pack/plugins/ml/public/util/chart_utils.js b/x-pack/plugins/ml/public/util/chart_utils.js index 5b097cb1f2c8..c73c89ad8c16 100644 --- a/x-pack/plugins/ml/public/util/chart_utils.js +++ b/x-pack/plugins/ml/public/util/chart_utils.js @@ -140,23 +140,40 @@ const POPULATION_DISTRIBUTION_ENABLED = true; // get the chart type based on its configuration export function getChartType(config) { - + let chartType = CHART_TYPE.SINGLE_METRIC; if ( EVENT_DISTRIBUTION_ENABLED && config.functionDescription === 'rare' && (config.entityFields.some(f => f.fieldType === 'over') === false) ) { - return CHART_TYPE.EVENT_DISTRIBUTION; + chartType = CHART_TYPE.EVENT_DISTRIBUTION; } else if ( POPULATION_DISTRIBUTION_ENABLED && config.functionDescription !== 'rare' && config.entityFields.some(f => f.fieldType === 'over') && config.metricFunction !== null // Event distribution chart relies on the ML function mapping to an ES aggregation ) { - return CHART_TYPE.POPULATION_DISTRIBUTION; + chartType = CHART_TYPE.POPULATION_DISTRIBUTION; + } + + if (chartType === CHART_TYPE.EVENT_DISTRIBUTION || chartType === CHART_TYPE.POPULATION_DISTRIBUTION) { + // Check that the config does not use script fields defined in the datafeed config. + if (config.datafeedConfig !== undefined && config.datafeedConfig.script_fields !== undefined) { + const scriptFields = Object.keys(config.datafeedConfig.script_fields); + const checkFields = config.entityFields.map(entity => entity.fieldName); + if (config.metricFieldName) { + checkFields.push(config.metricFieldName); + } + const usesScriptFields = + (checkFields.find(fieldName => scriptFields.includes(fieldName)) !== undefined); + if (usesScriptFields === true) { + // Only single metric chart type supports query of model plot data. + chartType = CHART_TYPE.SINGLE_METRIC; + } + } } - return CHART_TYPE.SINGLE_METRIC; + return chartType; } export function getExploreSeriesLink(series) {