From 8856be8e39a32b395f84da07ddc667c1f212068b Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 31 Jan 2019 13:30:05 -0500 Subject: [PATCH] Address PR feedback with filter typing to clean up code. --- .../elasticsearch_monitors_adapter.ts | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts b/x-pack/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts index ff6530986483e..c0c5404101c1d 100644 --- a/x-pack/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts +++ b/x-pack/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts @@ -294,44 +294,45 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter { }; const queryResult = await this.database.search(request, params); const aggBuckets: any[] = get(queryResult, 'aggregations.hosts.buckets', []); - const latestMonitors: Array = aggBuckets.map(bucket => { - if ( - statusFilter && - get(bucket, 'latest.hits.hits[0]._source.monitor.status', undefined) !== statusFilter - ) { - return undefined; - } - const key: string = get(bucket, 'key.id'); - const url: string | null = get(bucket, 'key.url', null); - const upSeries: MonitorSeriesPoint[] = []; - const downSeries: MonitorSeriesPoint[] = []; - const histogramBuckets: any[] = get(bucket, 'histogram.buckets', []); - const ping: Ping = get(bucket, 'latest.hits.hits[0]._source'); - const timestamp: string = get(bucket, 'latest.hits.hits[0]._source.@timestamp'); - histogramBuckets.forEach(histogramBucket => { - const status = get(histogramBucket, 'status.buckets', []); - // @ts-ignore TODO update typings and remove this comment - const up = status.find(f => f.key === 'up'); - // @ts-ignore TODO update typings and remove this comment - const down = status.find(f => f.key === 'down'); - // @ts-ignore TODO update typings and remove this comment - upSeries.push({ x: histogramBucket.key, y: up ? up.doc_count : null }); - // @ts-ignore TODO update typings and remove this comment - downSeries.push({ x: histogramBucket.key, y: down ? down.doc_count : null }); - }); - return { - id: { key, url }, - ping: { - ...ping, - timestamp, - }, - upSeries, - downSeries, - }; - }); + const latestMonitors: LatestMonitor[] = aggBuckets + .filter( + bucket => + statusFilter && + get(bucket, 'latest.hits.hits[0]._source.monitor.status', undefined) !== statusFilter + ) + .map( + (bucket): LatestMonitor => { + const key: string = get(bucket, 'key.id'); + const url: string | null = get(bucket, 'key.url', null); + const upSeries: MonitorSeriesPoint[] = []; + const downSeries: MonitorSeriesPoint[] = []; + const histogramBuckets: any[] = get(bucket, 'histogram.buckets', []); + const ping: Ping = get(bucket, 'latest.hits.hits[0]._source'); + const timestamp: string = get(bucket, 'latest.hits.hits[0]._source.@timestamp'); + histogramBuckets.forEach(histogramBucket => { + const status = get(histogramBucket, 'status.buckets', []); + // @ts-ignore TODO update typings and remove this comment + const up = status.find(f => f.key === 'up'); + // @ts-ignore TODO update typings and remove this comment + const down = status.find(f => f.key === 'down'); + // @ts-ignore TODO update typings and remove this comment + upSeries.push({ x: histogramBucket.key, y: up ? up.doc_count : null }); + // @ts-ignore TODO update typings and remove this comment + downSeries.push({ x: histogramBucket.key, y: down ? down.doc_count : null }); + }); + return { + id: { key, url }, + ping: { + ...ping, + timestamp, + }, + upSeries, + downSeries, + }; + } + ); - // @ts-ignore undefined entries are filtered out - return latestMonitors.filter(monitor => monitor !== undefined); + return latestMonitors; } /**