Skip to content

Commit 97c5890

Browse files
committed
[ML] Fix dv broken with time range
1 parent 861d28f commit 97c5890

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { isPopulatedObject } from './object_utils';
9+
import { RUNTIME_FIELD_TYPES } from '../../../../../src/plugins/data/common/index_patterns';
10+
import type { RuntimeField, RuntimeMappings } from '../types/fields';
11+
import type { RuntimeType } from '../../../runtime_fields/target/types/public';
12+
13+
export function isRuntimeField(arg: unknown): arg is RuntimeField {
14+
return (
15+
isPopulatedObject(arg) &&
16+
((Object.keys(arg).length === 1 && arg.hasOwnProperty('type')) ||
17+
(Object.keys(arg).length === 2 &&
18+
arg.hasOwnProperty('type') &&
19+
arg.hasOwnProperty('script') &&
20+
(typeof arg.script === 'string' ||
21+
(isPopulatedObject(arg.script) &&
22+
Object.keys(arg.script).length === 1 &&
23+
arg.script.hasOwnProperty('source') &&
24+
typeof arg.script.source === 'string')))) &&
25+
RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType)
26+
);
27+
}
28+
29+
export function isRuntimeMappings(arg: unknown): arg is RuntimeMappings {
30+
return isPopulatedObject(arg) && Object.values(arg).every((d) => isRuntimeField(d));
31+
}

x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ const getAggIntervals = async (
211211
query,
212212
aggs: buildSamplerAggregation(minMaxAggs, samplerShardSize),
213213
size: 0,
214-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
214+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
215215
},
216216
});
217217

@@ -297,7 +297,7 @@ export const getHistogramsForFields = async (
297297
query,
298298
aggs: buildSamplerAggregation(chartDataAggs, samplerShardSize),
299299
size: 0,
300-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
300+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
301301
},
302302
});
303303

@@ -753,7 +753,7 @@ export class DataVisualizer {
753753
filter: filterCriteria,
754754
},
755755
},
756-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
756+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
757757
};
758758
filterCriteria.push({ exists: { field } });
759759

@@ -799,7 +799,7 @@ export class DataVisualizer {
799799
},
800800
},
801801
aggs,
802-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
802+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
803803
};
804804

805805
const { body } = await this._asCurrentUser.search({
@@ -904,7 +904,7 @@ export class DataVisualizer {
904904
},
905905
},
906906
aggs: buildSamplerAggregation(aggs, samplerShardSize),
907-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
907+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
908908
};
909909

910910
const { body } = await this._asCurrentUser.search({
@@ -1027,7 +1027,7 @@ export class DataVisualizer {
10271027
},
10281028
},
10291029
aggs: buildSamplerAggregation(aggs, samplerShardSize),
1030-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
1030+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
10311031
};
10321032

10331033
const { body } = await this._asCurrentUser.search({
@@ -1103,7 +1103,7 @@ export class DataVisualizer {
11031103
},
11041104
},
11051105
aggs: buildSamplerAggregation(aggs, samplerShardSize),
1106-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
1106+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
11071107
};
11081108

11091109
const { body } = await this._asCurrentUser.search({
@@ -1172,7 +1172,7 @@ export class DataVisualizer {
11721172
},
11731173
},
11741174
aggs: buildSamplerAggregation(aggs, samplerShardSize),
1175-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
1175+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
11761176
};
11771177

11781178
const { body } = await this._asCurrentUser.search({
@@ -1237,7 +1237,7 @@ export class DataVisualizer {
12371237
filter: filterCriteria,
12381238
},
12391239
},
1240-
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
1240+
...(isPopulatedObject(runtimeMappings) ? { runtime_mappings: runtimeMappings } : {}),
12411241
};
12421242

12431243
const { body } = await this._asCurrentUser.search({

x-pack/plugins/ml/server/routes/schemas/data_visualizer_schema.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@
66
*/
77

88
import { schema } from '@kbn/config-schema';
9+
import { isRuntimeField } from '../../../common/util/runtime_field_utils';
910

1011
export const indexPatternTitleSchema = schema.object({
1112
/** Title of the index pattern for which to return stats. */
1213
indexPatternTitle: schema.string(),
1314
});
1415

15-
const runtimeMappingsSchema = schema.maybe(schema.any());
16+
const runtimeMappingsSchema = schema.maybe(
17+
schema.object(
18+
{},
19+
{
20+
unknowns: 'allow',
21+
validate: (v: object) => {
22+
if (Object.values(v).some((o) => !isRuntimeField(o))) {
23+
return 'Your error message about invalid runtime definition';
24+
}
25+
},
26+
}
27+
)
28+
);
1629

1730
export const dataVisualizerFieldHistogramsSchema = schema.object({
1831
/** Query to match documents in the index. */

0 commit comments

Comments
 (0)