Skip to content

Commit 982639f

Browse files
Aaron Caldwellkibanamachine
andauthored
[Maps] Fix threshold alert issue resolving nested fields (#83577)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 085698e commit 982639f

4 files changed

Lines changed: 214 additions & 44 deletions

File tree

x-pack/plugins/stack_alerts/server/alert_types/geo_threshold/geo_threshold.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function transformResults(
3838
return _.map(subBuckets, (subBucket) => {
3939
const locationFieldResult = _.get(
4040
subBucket,
41-
`entityHits.hits.hits[0].fields.${geoField}[0]`,
41+
`entityHits.hits.hits[0].fields["${geoField}"][0]`,
4242
''
4343
);
4444
const location = locationFieldResult
@@ -50,7 +50,7 @@ export function transformResults(
5050
: null;
5151
const dateInShape = _.get(
5252
subBucket,
53-
`entityHits.hits.hits[0].fields.${dateField}[0]`,
53+
`entityHits.hits.hits[0].fields["${dateField}"][0]`,
5454
null
5555
);
5656
const docId = _.get(subBucket, `entityHits.hits.hits[0]._id`);

x-pack/plugins/stack_alerts/server/alert_types/geo_threshold/tests/es_sample_response.json

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,7 @@
1313
"relation" : "gte"
1414
},
1515
"max_score" : 0.0,
16-
"hits" : [
17-
{
18-
"_index" : "flight_tracks",
19-
"_id" : "XOng1XQB6yyY-xQxbwWM",
20-
"_score" : 0.0,
21-
"fields" : {
22-
"@timestamp" : [
23-
"2020-09-28T18:01:29.580Z"
24-
]
25-
}
26-
},
27-
{
28-
"_index" : "flight_tracks",
29-
"_id" : "Xeng1XQB6yyY-xQxbwWM",
30-
"_score" : 0.0,
31-
"fields" : {
32-
"@timestamp" : [
33-
"2020-09-28T18:01:29.580Z"
34-
]
35-
}
36-
},
37-
{
38-
"_index" : "flight_tracks",
39-
"_id" : "Xung1XQB6yyY-xQxbwWM",
40-
"_score" : 0.0,
41-
"fields" : {
42-
"@timestamp" : [
43-
"2020-09-28T18:01:29.580Z"
44-
]
45-
}
46-
},
47-
{
48-
"_index" : "flight_tracks",
49-
"_id" : "UOjg1XQB6yyY-xQxZvMz",
50-
"_score" : 0.0,
51-
"fields" : {
52-
"@timestamp" : [
53-
"2020-09-28T18:01:27.266Z"
54-
]
55-
}
56-
}
57-
]
16+
"hits" : []
5817
},
5918
"aggregations" : {
6019
"shapes" : {
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"took" : 2760,
3+
"timed_out" : false,
4+
"_shards" : {
5+
"total" : 1,
6+
"successful" : 1,
7+
"skipped" : 0,
8+
"failed" : 0
9+
},
10+
"hits" : {
11+
"total" : {
12+
"value" : 10000,
13+
"relation" : "gte"
14+
},
15+
"max_score" : 0.0,
16+
"hits" : []
17+
},
18+
"aggregations" : {
19+
"shapes" : {
20+
"meta" : { },
21+
"buckets" : {
22+
"0DrJu3QB6yyY-xQxv6Ip" : {
23+
"doc_count" : 1047,
24+
"entitySplit" : {
25+
"doc_count_error_upper_bound" : 0,
26+
"sum_other_doc_count" : 957,
27+
"buckets" : [
28+
{
29+
"key" : "936",
30+
"doc_count" : 9,
31+
"entityHits" : {
32+
"hits" : {
33+
"total" : {
34+
"value" : 9,
35+
"relation" : "eq"
36+
},
37+
"max_score" : null,
38+
"hits" : [
39+
{
40+
"_index" : "flight_tracks",
41+
"_id" : "N-ng1XQB6yyY-xQxnGSM",
42+
"_score" : null,
43+
"fields" : {
44+
"time_data.@timestamp" : [
45+
"2020-09-28T18:01:41.190Z"
46+
],
47+
"geo.coords.location" : [
48+
"40.62806099653244, -82.8814151789993"
49+
],
50+
"entity_id" : [
51+
"936"
52+
]
53+
},
54+
"sort" : [
55+
1601316101190
56+
]
57+
}
58+
]
59+
}
60+
}
61+
},
62+
{
63+
"key" : "AAL2019",
64+
"doc_count" : 9,
65+
"entityHits" : {
66+
"hits" : {
67+
"total" : {
68+
"value" : 9,
69+
"relation" : "eq"
70+
},
71+
"max_score" : null,
72+
"hits" : [
73+
{
74+
"_index" : "flight_tracks",
75+
"_id" : "iOng1XQB6yyY-xQxnGSM",
76+
"_score" : null,
77+
"fields" : {
78+
"time_data.@timestamp" : [
79+
"2020-09-28T18:01:41.191Z"
80+
],
81+
"geo.coords.location" : [
82+
"39.006176185794175, -82.22068064846098"
83+
],
84+
"entity_id" : [
85+
"AAL2019"
86+
]
87+
},
88+
"sort" : [
89+
1601316101191
90+
]
91+
}
92+
]
93+
}
94+
}
95+
},
96+
{
97+
"key" : "AAL2323",
98+
"doc_count" : 9,
99+
"entityHits" : {
100+
"hits" : {
101+
"total" : {
102+
"value" : 9,
103+
"relation" : "eq"
104+
},
105+
"max_score" : null,
106+
"hits" : [
107+
{
108+
"_index" : "flight_tracks",
109+
"_id" : "n-ng1XQB6yyY-xQxnGSM",
110+
"_score" : null,
111+
"fields" : {
112+
"time_data.@timestamp" : [
113+
"2020-09-28T18:01:41.191Z"
114+
],
115+
"geo.coords.location" : [
116+
"41.6677269525826, -84.71324851736426"
117+
],
118+
"entity_id" : [
119+
"AAL2323"
120+
]
121+
},
122+
"sort" : [
123+
1601316101191
124+
]
125+
}
126+
]
127+
}
128+
}
129+
},
130+
{
131+
"key" : "ABD5250",
132+
"doc_count" : 9,
133+
"entityHits" : {
134+
"hits" : {
135+
"total" : {
136+
"value" : 9,
137+
"relation" : "eq"
138+
},
139+
"max_score" : null,
140+
"hits" : [
141+
{
142+
"_index" : "flight_tracks",
143+
"_id" : "GOng1XQB6yyY-xQxnGWM",
144+
"_score" : null,
145+
"fields" : {
146+
"time_data.@timestamp" : [
147+
"2020-09-28T18:01:41.192Z"
148+
],
149+
"geo.coords.location" : [
150+
"39.07997465226799, 6.073727197945118"
151+
],
152+
"entity_id" : [
153+
"ABD5250"
154+
]
155+
},
156+
"sort" : [
157+
1601316101192
158+
]
159+
}
160+
]
161+
}
162+
}
163+
}
164+
]
165+
}
166+
}
167+
}
168+
}
169+
}
170+
}

x-pack/plugins/stack_alerts/server/alert_types/geo_threshold/tests/geo_threshold.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import sampleJsonResponse from './es_sample_response.json';
8+
import sampleJsonResponseWithNesting from './es_sample_response_with_nesting.json';
89
import { getMovedEntities, transformResults } from '../geo_threshold';
910
import { OTHER_CATEGORY } from '../es_query_builder';
1011
import { SearchResponse } from 'elasticsearch';
@@ -51,6 +52,46 @@ describe('geo_threshold', () => {
5152
]);
5253
});
5354

55+
const nestedDateField = 'time_data.@timestamp';
56+
const nestedGeoField = 'geo.coords.location';
57+
it('should correctly transform expected results if fields are nested', async () => {
58+
const transformedResults = transformResults(
59+
(sampleJsonResponseWithNesting as unknown) as SearchResponse<unknown>,
60+
nestedDateField,
61+
nestedGeoField
62+
);
63+
expect(transformedResults).toEqual([
64+
{
65+
dateInShape: '2020-09-28T18:01:41.190Z',
66+
docId: 'N-ng1XQB6yyY-xQxnGSM',
67+
entityName: '936',
68+
location: [-82.8814151789993, 40.62806099653244],
69+
shapeLocationId: '0DrJu3QB6yyY-xQxv6Ip',
70+
},
71+
{
72+
dateInShape: '2020-09-28T18:01:41.191Z',
73+
docId: 'iOng1XQB6yyY-xQxnGSM',
74+
entityName: 'AAL2019',
75+
location: [-82.22068064846098, 39.006176185794175],
76+
shapeLocationId: '0DrJu3QB6yyY-xQxv6Ip',
77+
},
78+
{
79+
dateInShape: '2020-09-28T18:01:41.191Z',
80+
docId: 'n-ng1XQB6yyY-xQxnGSM',
81+
entityName: 'AAL2323',
82+
location: [-84.71324851736426, 41.6677269525826],
83+
shapeLocationId: '0DrJu3QB6yyY-xQxv6Ip',
84+
},
85+
{
86+
dateInShape: '2020-09-28T18:01:41.192Z',
87+
docId: 'GOng1XQB6yyY-xQxnGWM',
88+
entityName: 'ABD5250',
89+
location: [6.073727197945118, 39.07997465226799],
90+
shapeLocationId: '0DrJu3QB6yyY-xQxv6Ip',
91+
},
92+
]);
93+
});
94+
5495
it('should return an empty array if no results', async () => {
5596
const transformedResults = transformResults(undefined, dateField, geoField);
5697
expect(transformedResults).toEqual([]);

0 commit comments

Comments
 (0)