[ML] Display metric rather than distribution chart for script field (#34931)

* [ML] Display metric rather than distribution chart for script field

* [ML] Cleaned up code in getChartType
This commit is contained in:
Pete Harverson 2019-04-11 16:34:36 +01:00 committed by GitHub
parent 870552dd78
commit 08273e58a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 4 deletions

View file

@ -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', () => {

View file

@ -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;
}
return CHART_TYPE.SINGLE_METRIC;
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 chartType;
}
export function getExploreSeriesLink(series) {