Skip to content

Commit

Permalink
[ML] Display metric rather than distribution chart for script field (#…
Browse files Browse the repository at this point in the history
…34931)

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

* [ML] Cleaned up code in getChartType
  • Loading branch information
peteharverson authored Apr 11, 2019
1 parent 870552d commit 08273e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
26 changes: 26 additions & 0 deletions x-pack/plugins/ml/public/util/__tests__/chart_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
25 changes: 21 additions & 4 deletions x-pack/plugins/ml/public/util/chart_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 08273e5

Please sign in to comment.