Skip to content

Commit e923e33

Browse files
committed
[ML] Remove duplicated geo examples
1 parent da837f2 commit e923e33

File tree

5 files changed

+110
-5
lines changed

5 files changed

+110
-5
lines changed

x-pack/plugins/data_visualizer/common/types/field_request_config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ export interface DocumentCounts {
2929
interval?: number;
3030
}
3131

32+
export interface LatLongExample {
33+
lat: number;
34+
lon: number;
35+
}
36+
37+
export interface GeoPointExample {
38+
coordinates: number[];
39+
type?: string;
40+
}
41+
3242
export interface FieldVisStats {
3343
error?: Error;
3444
cardinality?: number;
@@ -56,7 +66,7 @@ export interface FieldVisStats {
5666
topValues?: Array<{ key: number | string; doc_count: number }>;
5767
topValuesSampleSize?: number;
5868
topValuesSamplerShardSize?: number;
59-
examples?: Array<string | object>;
69+
examples?: Array<string | GeoPointExample | object>;
6070
timeRangeEarliest?: number;
6171
timeRangeLatest?: number;
6272
}

x-pack/plugins/data_visualizer/public/application/common/components/examples_list/examples_list.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import React, { FC } from 'react';
1010
import { EuiListGroup, EuiListGroupItem } from '@elastic/eui';
1111

1212
import { FormattedMessage } from '@kbn/i18n-react';
13+
import { GeoPointExample } from '../../../../../common/types/field_request_config';
1314
import { ExpandedRowFieldHeader } from '../stats_table/components/expanded_row_field_header';
1415
import { ExpandedRowPanel } from '../stats_table/components/field_data_expanded_row/expanded_row_panel';
1516
interface Props {
16-
examples: Array<string | object>;
17+
examples: Array<string | GeoPointExample | object>;
1718
}
1819

1920
export const ExamplesList: FC<Props> = ({ examples }) => {

x-pack/plugins/data_visualizer/public/application/common/components/expanded_row/geo_point_content_with_map/geo_point_content_with_map.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* 2.0; you may not use this file except in compliance with the Elastic License
55
* 2.0.
66
*/
7-
8-
import React, { FC, useEffect, useState } from 'react';
7+
import React, { FC, useEffect, useMemo, useState } from 'react';
98
import { DataView } from '@kbn/data-views-plugin/public';
109
import { ES_GEO_FIELD_TYPE, LayerDescriptor } from '@kbn/maps-plugin/common';
10+
import { getUniqCoordinates } from '../../../util/geo_utils';
1111
import { CombinedQuery } from '../../../../index_data_visualizer/types/combined_query';
1212
import { ExpandedRowContent } from '../../stats_table/components/field_data_expanded_row/expanded_row_content';
1313
import { DocumentStatsTable } from '../../stats_table/components/field_data_expanded_row/document_stats';
@@ -29,6 +29,8 @@ export const GeoPointContentWithMap: FC<{
2929
services: { maps: mapsPlugin, data },
3030
} = useDataVisualizerKibana();
3131

32+
const uniqueExamples = useMemo(() => getUniqCoordinates(stats?.examples), [stats?.examples]);
33+
3234
// Update the layer list with updated geo points upon refresh
3335
useEffect(() => {
3436
async function updateIndexPatternSearchLayer() {
@@ -64,7 +66,7 @@ export const GeoPointContentWithMap: FC<{
6466
return (
6567
<ExpandedRowContent dataTestSubj={'dataVisualizerIndexBasedMapContent'}>
6668
<DocumentStatsTable config={config} />
67-
<ExamplesList examples={stats.examples} />
69+
<ExamplesList examples={uniqueExamples} />
6870
<ExpandedRowPanel className={'dvPanel__wrapper dvMap__wrapper'} grow={true}>
6971
<EmbeddedMapComponent layerList={layerList} />
7072
</ExpandedRowPanel>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 { getUniqCoordinates } from './geo_utils';
9+
10+
describe('geo type utils', () => {
11+
describe('getUniqCoordinates', () => {
12+
test('should remove duplicated coordinates', () => {
13+
expect(
14+
getUniqCoordinates([
15+
{ coordinates: [0.0001, 2343], type: 'Point' },
16+
{ coordinates: [0.0001, 2343], type: 'Point' },
17+
{ coordinates: [0.0001, 2343], type: 'Point' },
18+
{ coordinates: [0.0001, 2343], type: 'Shape' },
19+
{ coordinates: [0.0001, 2343] },
20+
{ coordinates: [4321, 2343], type: 'Point' },
21+
{ coordinates: [4321, 2343], type: 'Point' },
22+
])
23+
).toMatchObject([
24+
{
25+
coordinates: [0.0001, 2343],
26+
type: 'Point',
27+
},
28+
{
29+
coordinates: [0.0001, 2343],
30+
type: 'Shape',
31+
},
32+
{
33+
coordinates: [0.0001, 2343],
34+
},
35+
{
36+
coordinates: [4321, 2343],
37+
type: 'Point',
38+
},
39+
]);
40+
expect(
41+
getUniqCoordinates([
42+
{ coordinates: [1000, 2000, 3000], type: 'Point' },
43+
{ coordinates: [1000, 2000, 3000], type: 'Point' },
44+
{ coordinates: [1000, 2000, 3000], type: 'Point' },
45+
{ coordinates: [1000, 2000, 3000, 4000], type: 'Shape' },
46+
{ coordinates: [1000, 2000, 3000, 4000] },
47+
])
48+
).toMatchObject([
49+
{
50+
coordinates: [1000, 2000, 3000],
51+
type: 'Point',
52+
},
53+
{ coordinates: [1000, 2000, 3000, 4000], type: 'Shape' },
54+
{ coordinates: [1000, 2000, 3000, 4000] },
55+
]);
56+
});
57+
});
58+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 '@kbn/ml-is-populated-object';
9+
import { GeoPointExample } from '../../../../common/types/field_request_config';
10+
import { isDefined } from './is_defined';
11+
12+
export function isGeoPointExample(arg: unknown): arg is GeoPointExample {
13+
return isPopulatedObject(arg, ['coordinates']);
14+
}
15+
16+
export function getUniqCoordinates(
17+
coordinates: Array<string | GeoPointExample | object> | undefined
18+
): GeoPointExample[] {
19+
const uniqueCoordinates: GeoPointExample[] = [];
20+
if (!isDefined(coordinates)) return uniqueCoordinates;
21+
22+
coordinates.forEach((ex) => {
23+
if (
24+
isGeoPointExample(ex) &&
25+
uniqueCoordinates.findIndex(
26+
(c) =>
27+
c.type === ex.type && ex.coordinates.every((coord, idx) => coord === c.coordinates[idx])
28+
) === -1
29+
) {
30+
uniqueCoordinates.push(ex);
31+
}
32+
});
33+
return uniqueCoordinates;
34+
}

0 commit comments

Comments
 (0)