Skip to content

Commit d83764b

Browse files
[Uptime] Handle locations with names but no geo data (#55234) (#55853)
* Handle locations with names but no geo data. * Fix broken types, add a comment explaining some weird ts-related code. Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent ab58a71 commit d83764b

4 files changed

Lines changed: 28 additions & 20 deletions

File tree

x-pack/legacy/plugins/uptime/public/components/functional/location_map/embeddables/embedded_map.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { start } from '../../../../../../../../../src/legacy/core_plugins/embedd
1212
import * as i18n from './translations';
1313
// @ts-ignore
1414
import { MAP_SAVED_OBJECT_TYPE } from '../../../../../../maps/common/constants';
15+
import { Location } from '../../../../../common/runtime_types';
1516

1617
import { MapEmbeddable } from './types';
1718
import { getLayerList } from './map_config';
@@ -22,10 +23,7 @@ export interface EmbeddedMapProps {
2223
downPoints: LocationPoint[];
2324
}
2425

25-
export interface LocationPoint {
26-
lat: string;
27-
lon: string;
28-
}
26+
export type LocationPoint = Required<Location>;
2927

3028
const EmbeddedPanel = styled.div`
3129
z-index: auto;

x-pack/legacy/plugins/uptime/public/components/functional/location_map/embeddables/map_config.ts

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

77
import lowPolyLayerFeatures from './low_poly_layer.json';
8-
import { LocationPoint } from './embedded_map';
8+
import { LocationPoint } from './embedded_map.js';
99
import { UptimeAppColors } from '../../../../uptime_app';
1010

1111
/**
@@ -16,7 +16,7 @@ import { UptimeAppColors } from '../../../../uptime_app';
1616
export const getLayerList = (
1717
upPoints: LocationPoint[],
1818
downPoints: LocationPoint[],
19-
{ gray, danger }: Pick<UptimeAppColors, 'gray' | 'danger'>
19+
{ danger }: Pick<UptimeAppColors, 'danger'>
2020
) => {
2121
return [getLowPolyLayer(), getDownPointsLayer(downPoints, danger), getUpPointsLayer(upPoints)];
2222
};

x-pack/legacy/plugins/uptime/public/components/functional/location_map/location_map.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import styled from 'styled-components';
99
import { EuiFlexGroup, EuiFlexItem, EuiErrorBoundary } from '@elastic/eui';
1010
import { LocationStatusTags } from './location_status_tags';
1111
import { EmbeddedMap, LocationPoint } from './embeddables/embedded_map';
12-
import { MonitorLocations } from '../../../../common/runtime_types';
12+
import { MonitorLocations, MonitorLocation } from '../../../../common/runtime_types';
1313
import { UNNAMED_LOCATION } from '../../../../common/constants';
1414
import { LocationMissingWarning } from './location_missing';
1515

@@ -32,15 +32,23 @@ export const LocationMap = ({ monitorLocations }: LocationMapProps) => {
3232
let isGeoInfoMissing = false;
3333

3434
if (monitorLocations?.locations) {
35-
monitorLocations.locations.forEach((item: any) => {
36-
if (item.geo?.name !== UNNAMED_LOCATION) {
37-
if (item.summary.down === 0) {
38-
upPoints.push(item.geo.location);
35+
monitorLocations.locations.forEach((item: MonitorLocation) => {
36+
if (item.geo?.name === UNNAMED_LOCATION || !item.geo?.location) {
37+
isGeoInfoMissing = true;
38+
} else if (
39+
item.geo?.name !== UNNAMED_LOCATION &&
40+
!!item.geo.location.lat &&
41+
!!item.geo.location.lon
42+
) {
43+
// TypeScript doesn't infer that the above checks in this block's condition
44+
// ensure that lat and lon are defined when we try to pass the location object directly,
45+
// but if we destructure the values it does. Improvement to this block is welcome.
46+
const { lat, lon } = item.geo.location;
47+
if (item?.summary?.down === 0) {
48+
upPoints.push({ lat, lon });
3949
} else {
40-
downPoints.push(item.geo.location);
50+
downPoints.push({ lat, lon });
4151
}
42-
} else if (item.geo?.name === UNNAMED_LOCATION) {
43-
isGeoInfoMissing = true;
4452
}
4553
});
4654
}

x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,18 @@ export const elasticsearchMonitorsAdapter: UMMonitorsAdapter = {
346346
const result = await callES('search', params);
347347
const locations = result?.aggregations?.location?.buckets ?? [];
348348

349-
const getGeo = (locGeo: any) => {
349+
const getGeo = (locGeo: { name: string; location?: string }) => {
350350
if (locGeo) {
351351
const { name, location } = locGeo;
352-
const latLon = location.trim().split(',');
352+
const latLon = location?.trim().split(',');
353353
return {
354354
name,
355-
location: {
356-
lat: latLon[0],
357-
lon: latLon[1],
358-
},
355+
location: latLon
356+
? {
357+
lat: latLon[0],
358+
lon: latLon[1],
359+
}
360+
: undefined,
359361
};
360362
} else {
361363
return {

0 commit comments

Comments
 (0)