Skip to content

Commit

Permalink
[Uptime] Handle locations with names but no geo data (elastic#55234)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
justinkambic and elasticmachine committed Jan 24, 2020
1 parent 6943bca commit e2b7e40
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { start } from '../../../../../../../../../src/legacy/core_plugins/embedd
import * as i18n from './translations';
// @ts-ignore
import { MAP_SAVED_OBJECT_TYPE } from '../../../../../../maps/common/constants';
import { Location } from '../../../../../common/runtime_types';

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

export interface LocationPoint {
lat: string;
lon: string;
}
export type LocationPoint = Required<Location>;

const EmbeddedPanel = styled.div`
z-index: auto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import lowPolyLayerFeatures from './low_poly_layer.json';
import { LocationPoint } from './embedded_map';
import { LocationPoint } from './embedded_map.js';
import { UptimeAppColors } from '../../../../uptime_app';

/**
Expand All @@ -16,7 +16,7 @@ import { UptimeAppColors } from '../../../../uptime_app';
export const getLayerList = (
upPoints: LocationPoint[],
downPoints: LocationPoint[],
{ gray, danger }: Pick<UptimeAppColors, 'gray' | 'danger'>
{ danger }: Pick<UptimeAppColors, 'danger'>
) => {
return [getLowPolyLayer(), getDownPointsLayer(downPoints, danger), getUpPointsLayer(upPoints)];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import styled from 'styled-components';
import { EuiFlexGroup, EuiFlexItem, EuiErrorBoundary } from '@elastic/eui';
import { LocationStatusTags } from './location_status_tags';
import { EmbeddedMap, LocationPoint } from './embeddables/embedded_map';
import { MonitorLocations } from '../../../../common/runtime_types';
import { MonitorLocations, MonitorLocation } from '../../../../common/runtime_types';
import { UNNAMED_LOCATION } from '../../../../common/constants';
import { LocationMissingWarning } from './location_missing';

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

if (monitorLocations?.locations) {
monitorLocations.locations.forEach((item: any) => {
if (item.geo?.name !== UNNAMED_LOCATION) {
if (item.summary.down === 0) {
upPoints.push(item.geo.location);
monitorLocations.locations.forEach((item: MonitorLocation) => {
if (item.geo?.name === UNNAMED_LOCATION || !item.geo?.location) {
isGeoInfoMissing = true;
} else if (
item.geo?.name !== UNNAMED_LOCATION &&
!!item.geo.location.lat &&
!!item.geo.location.lon
) {
// TypeScript doesn't infer that the above checks in this block's condition
// ensure that lat and lon are defined when we try to pass the location object directly,
// but if we destructure the values it does. Improvement to this block is welcome.
const { lat, lon } = item.geo.location;
if (item?.summary?.down === 0) {
upPoints.push({ lat, lon });
} else {
downPoints.push(item.geo.location);
downPoints.push({ lat, lon });
}
} else if (item.geo?.name === UNNAMED_LOCATION) {
isGeoInfoMissing = true;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,18 @@ export const elasticsearchMonitorsAdapter: UMMonitorsAdapter = {
const result = await callES('search', params);
const locations = result?.aggregations?.location?.buckets ?? [];

const getGeo = (locGeo: any) => {
const getGeo = (locGeo: { name: string; location?: string }) => {
if (locGeo) {
const { name, location } = locGeo;
const latLon = location.trim().split(',');
const latLon = location?.trim().split(',');
return {
name,
location: {
lat: latLon[0],
lon: latLon[1],
},
location: latLon
? {
lat: latLon[0],
lon: latLon[1],
}
: undefined,
};
} else {
return {
Expand Down

0 comments on commit e2b7e40

Please sign in to comment.