From 3216fce7160c1a75bb1f7ded882928cc81ab3120 Mon Sep 17 00:00:00 2001 From: Shahzad Date: Tue, 8 Dec 2020 17:18:37 +0100 Subject: [PATCH] [Uptime] Remove unused code in monitor list API (#84312) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../__snapshots__/monitor_list.test.tsx.snap | 6 ---- .../__tests__/monitor_list.test.tsx | 4 --- .../monitor_list_drawer/__tests__/data.json | 1 - .../public/lib/__mocks__/uptime_store.mock.ts | 1 - .../public/state/reducers/monitor_list.ts | 1 - .../state/selectors/__tests__/index.test.ts | 1 - .../lib/requests/get_snapshot_counts.ts | 5 +-- .../search/__tests__/query_context.test.ts | 13 ++++++- .../requests/search/__tests__/test_helpers.ts | 11 +++++- .../requests/search/find_potential_matches.ts | 11 ++---- .../lib/requests/search/query_context.ts | 16 +++++---- .../server/rest_api/monitors/monitor_list.ts | 36 +++++++++---------- .../uptime/rest/monitor_states_real_data.ts | 10 +----- 13 files changed, 54 insertions(+), 62 deletions(-) diff --git a/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/__snapshots__/monitor_list.test.tsx.snap b/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/__snapshots__/monitor_list.test.tsx.snap index edd901253f50..39f860f76f2b 100644 --- a/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/__snapshots__/monitor_list.test.tsx.snap +++ b/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/__snapshots__/monitor_list.test.tsx.snap @@ -92,7 +92,6 @@ exports[`MonitorList component MonitorListPagination component renders a no item "nextPagePagination": null, "prevPagePagination": null, "summaries": Array [], - "totalSummaryCount": 0, }, "loading": false, } @@ -299,7 +298,6 @@ exports[`MonitorList component MonitorListPagination component renders the pagin }, }, ], - "totalSummaryCount": 2, }, "loading": false, } @@ -403,7 +401,6 @@ exports[`MonitorList component renders a no items message when no data is provid "nextPagePagination": null, "prevPagePagination": null, "summaries": Array [], - "totalSummaryCount": 0, }, "loading": true, } @@ -611,7 +608,6 @@ exports[`MonitorList component renders error list 1`] = ` }, }, ], - "totalSummaryCount": 2, }, "loading": false, } @@ -818,7 +814,6 @@ exports[`MonitorList component renders loading state 1`] = ` }, }, ], - "totalSummaryCount": 2, }, "loading": true, } @@ -1857,7 +1852,6 @@ exports[`MonitorList component shallow renders the monitor list 1`] = ` }, }, ], - "totalSummaryCount": 2, }, "loading": false, } diff --git a/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/monitor_list.test.tsx b/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/monitor_list.test.tsx index 769df84a6b83..a158f6f67b48 100644 --- a/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/monitor_list.test.tsx +++ b/x-pack/plugins/uptime/public/components/overview/monitor_list/__tests__/monitor_list.test.tsx @@ -125,7 +125,6 @@ describe('MonitorList component', () => { nextPagePagination: null, prevPagePagination: null, summaries: [testFooSummary, testBarSummary], - totalSummaryCount: 2, }; }; @@ -164,7 +163,6 @@ describe('MonitorList component', () => { summaries: [], nextPagePagination: null, prevPagePagination: null, - totalSummaryCount: 0, }, loading: true, }} @@ -236,7 +234,6 @@ describe('MonitorList component', () => { sortOrder: SortOrder.ASC, }), summaries: [testFooSummary, testBarSummary], - totalSummaryCount: 2, }; }); @@ -265,7 +262,6 @@ describe('MonitorList component', () => { summaries: [], nextPagePagination: null, prevPagePagination: null, - totalSummaryCount: 0, }, loading: false, }} diff --git a/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_drawer/__tests__/data.json b/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_drawer/__tests__/data.json index 012280c8147d..1bbdcd4a3007 100644 --- a/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_drawer/__tests__/data.json +++ b/x-pack/plugins/uptime/public/components/overview/monitor_list/monitor_list_drawer/__tests__/data.json @@ -3,7 +3,6 @@ "monitorStates": { "prevPagePagination": null, "nextPagePagination": null, - "totalSummaryCount": 147428, "summaries": [ { "monitor_id": "andrewvc-com", diff --git a/x-pack/plugins/uptime/public/lib/__mocks__/uptime_store.mock.ts b/x-pack/plugins/uptime/public/lib/__mocks__/uptime_store.mock.ts index f985fd42f31c..0f600fcc20f6 100644 --- a/x-pack/plugins/uptime/public/lib/__mocks__/uptime_store.mock.ts +++ b/x-pack/plugins/uptime/public/lib/__mocks__/uptime_store.mock.ts @@ -79,7 +79,6 @@ export const mockStore = { prevPagePagination: null, nextPagePagination: null, summaries: [], - totalSummaryCount: 0, }, loading: false, }, diff --git a/x-pack/plugins/uptime/public/state/reducers/monitor_list.ts b/x-pack/plugins/uptime/public/state/reducers/monitor_list.ts index fed7e78995d0..c3ff90dbc3d9 100644 --- a/x-pack/plugins/uptime/public/state/reducers/monitor_list.ts +++ b/x-pack/plugins/uptime/public/state/reducers/monitor_list.ts @@ -20,7 +20,6 @@ export const initialState: MonitorList = { nextPagePagination: null, prevPagePagination: null, summaries: [], - totalSummaryCount: 0, }, loading: false, }; diff --git a/x-pack/plugins/uptime/public/state/selectors/__tests__/index.test.ts b/x-pack/plugins/uptime/public/state/selectors/__tests__/index.test.ts index 93f757b6baa8..f1a68318be86 100644 --- a/x-pack/plugins/uptime/public/state/selectors/__tests__/index.test.ts +++ b/x-pack/plugins/uptime/public/state/selectors/__tests__/index.test.ts @@ -78,7 +78,6 @@ describe('state selectors', () => { prevPagePagination: null, nextPagePagination: null, summaries: [], - totalSummaryCount: 0, }, loading: false, }, diff --git a/x-pack/plugins/uptime/server/lib/requests/get_snapshot_counts.ts b/x-pack/plugins/uptime/server/lib/requests/get_snapshot_counts.ts index 9df20c79a610..000cd2714a29 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_snapshot_counts.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_snapshot_counts.ts @@ -8,6 +8,7 @@ import { UMElasticsearchQueryFn } from '../adapters'; import { CONTEXT_DEFAULTS } from '../../../common/constants'; import { Snapshot } from '../../../common/runtime_types'; import { QueryContext } from './search'; +import { ESFilter } from '../../../../../typings/elasticsearch'; export interface GetSnapshotCountParams { dateRangeStart: string; @@ -42,7 +43,7 @@ const statusCount = async (context: QueryContext): Promise => { }); return ( - res.aggregations?.counts?.value ?? { + (res.aggregations?.counts?.value as Snapshot) ?? { total: 0, up: 0, down: 0, @@ -50,7 +51,7 @@ const statusCount = async (context: QueryContext): Promise => { ); }; -const statusCountBody = (filters: any): any => { +const statusCountBody = (filters: ESFilter[]) => { return { size: 0, query: { diff --git a/x-pack/plugins/uptime/server/lib/requests/search/__tests__/query_context.test.ts b/x-pack/plugins/uptime/server/lib/requests/search/__tests__/query_context.test.ts index b4de286a5b92..10421ecf2b81 100644 --- a/x-pack/plugins/uptime/server/lib/requests/search/__tests__/query_context.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/search/__tests__/query_context.test.ts @@ -7,6 +7,7 @@ import { QueryContext } from '../query_context'; import { CursorPagination } from '../types'; import { CursorDirection, SortOrder } from '../../../../../common/runtime_types'; +import { getUptimeESMockClient } from '../../__tests__/helper'; describe(QueryContext, () => { // 10 minute range @@ -19,7 +20,17 @@ describe(QueryContext, () => { }; let qc: QueryContext; - beforeEach(() => (qc = new QueryContext({}, rangeStart, rangeEnd, pagination, null, 10))); + beforeEach( + () => + (qc = new QueryContext( + getUptimeESMockClient().uptimeEsClient, + rangeStart, + rangeEnd, + pagination, + null, + 10 + )) + ); describe('dateRangeFilter()', () => { const expectedRange = { diff --git a/x-pack/plugins/uptime/server/lib/requests/search/__tests__/test_helpers.ts b/x-pack/plugins/uptime/server/lib/requests/search/__tests__/test_helpers.ts index 205b283d40d6..9c322fbeec41 100644 --- a/x-pack/plugins/uptime/server/lib/requests/search/__tests__/test_helpers.ts +++ b/x-pack/plugins/uptime/server/lib/requests/search/__tests__/test_helpers.ts @@ -7,6 +7,7 @@ import { CursorPagination } from '../types'; import { CursorDirection, SortOrder } from '../../../../../common/runtime_types'; import { QueryContext } from '../query_context'; +import { getUptimeESMockClient } from '../../__tests__/helper'; export const nextPagination = (key: any): CursorPagination => { return { @@ -16,5 +17,13 @@ export const nextPagination = (key: any): CursorPagination => { }; }; export const simpleQueryContext = (): QueryContext => { - return new QueryContext(undefined, '', '', nextPagination('something'), undefined, 0, ''); + return new QueryContext( + getUptimeESMockClient().uptimeEsClient, + '', + '', + nextPagination('something'), + undefined, + 0, + '' + ); }; diff --git a/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts b/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts index 2331d991e3af..2bc68b09e136 100644 --- a/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts +++ b/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts @@ -5,7 +5,6 @@ */ import { set } from '@elastic/safer-lodash-set'; -import { get } from 'lodash'; import { QueryContext } from './query_context'; /** @@ -21,9 +20,10 @@ export const findPotentialMatches = async ( ) => { const { body: queryResult } = await query(queryContext, searchAfter, size); const monitorIds: string[] = []; - get(queryResult, 'aggregations.monitors.buckets', []).forEach((b: any) => { + + (queryResult.aggregations?.monitors.buckets ?? []).forEach((b) => { const monitorId = b.key.monitor_id; - monitorIds.push(monitorId); + monitorIds.push(monitorId as string); }); return { @@ -53,11 +53,6 @@ const queryBody = async (queryContext: QueryContext, searchAfter: any, size: num size: 0, query: { bool: { filter: filters } }, aggs: { - has_timespan: { - filter: { - exists: { field: 'monitor.timespan' }, - }, - }, monitors: { composite: { size, diff --git a/x-pack/plugins/uptime/server/lib/requests/search/query_context.ts b/x-pack/plugins/uptime/server/lib/requests/search/query_context.ts index bcfb3035920f..8cb221cb5eae 100644 --- a/x-pack/plugins/uptime/server/lib/requests/search/query_context.ts +++ b/x-pack/plugins/uptime/server/lib/requests/search/query_context.ts @@ -5,13 +5,14 @@ */ import moment from 'moment'; -import { ElasticsearchClient } from 'kibana/server'; import { CursorPagination } from './types'; import { parseRelativeDate } from '../../helper'; import { CursorDirection, SortOrder } from '../../../../common/runtime_types'; +import { UptimeESClient } from '../../lib'; +import { ESFilter } from '../../../../../../typings/elasticsearch'; export class QueryContext { - callES: ElasticsearchClient; + callES: UptimeESClient; dateRangeStart: string; dateRangeEnd: string; pagination: CursorPagination; @@ -21,7 +22,7 @@ export class QueryContext { hasTimespanCache?: boolean; constructor( - database: any, + database: UptimeESClient, dateRangeStart: string, dateRangeEnd: string, pagination: CursorPagination, @@ -38,15 +39,16 @@ export class QueryContext { this.statusFilter = statusFilter; } - async search(params: any): Promise { - return this.callES.search({ body: params.body }); + async search(params: TParams) { + return this.callES.search(params); } async count(params: any): Promise { - return this.callES.count(params); + const { body } = await this.callES.count(params); + return body; } - async dateAndCustomFilters(): Promise { + async dateAndCustomFilters(): Promise { const clauses = [await this.dateRangeFilter()]; if (this.filterClause) { clauses.push(this.filterClause); diff --git a/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts b/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts index 3eac49341b2c..50fe616ae9cb 100644 --- a/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts +++ b/x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts @@ -38,33 +38,29 @@ export const createMonitorListRoute: UMRestApiRouteFactory = (libs) => ({ const decodedPagination = pagination ? JSON.parse(decodeURIComponent(pagination)) : CONTEXT_DEFAULTS.CURSOR_PAGINATION; - const [ - indexStatus, - { summaries, nextPagePagination, prevPagePagination }, - ] = await Promise.all([ - libs.requests.getIndexStatus({ uptimeEsClient }), - libs.requests.getMonitorStates({ - uptimeEsClient, - dateRangeStart, - dateRangeEnd, - pagination: decodedPagination, - pageSize, - filters, - // this is added to make typescript happy, - // this sort of reassignment used to be further downstream but I've moved it here - // because this code is going to be decomissioned soon - statusFilter: statusFilter || undefined, - }), - ]); - const totalSummaryCount = indexStatus?.docCount ?? 0; + const { + summaries, + nextPagePagination, + prevPagePagination, + } = await libs.requests.getMonitorStates({ + uptimeEsClient, + dateRangeStart, + dateRangeEnd, + pagination: decodedPagination, + pageSize, + filters, + // this is added to make typescript happy, + // this sort of reassignment used to be further downstream but I've moved it here + // because this code is going to be decomissioned soon + statusFilter: statusFilter || undefined, + }); return response.ok({ body: { summaries, nextPagePagination, prevPagePagination, - totalSummaryCount, }, }); } catch (e) { diff --git a/x-pack/test/api_integration/apis/uptime/rest/monitor_states_real_data.ts b/x-pack/test/api_integration/apis/uptime/rest/monitor_states_real_data.ts index bdc18ac831d2..6eb6851c44e4 100644 --- a/x-pack/test/api_integration/apis/uptime/rest/monitor_states_real_data.ts +++ b/x-pack/test/api_integration/apis/uptime/rest/monitor_states_real_data.ts @@ -17,7 +17,6 @@ interface ExpectedMonitorStatesPage { absFrom: number; absTo: number; size: number; - totalCount: number; prevPagination: null | string; nextPagination: null | string; } @@ -34,14 +33,13 @@ const checkMonitorStatesResponse = ({ absFrom, absTo, size, - totalCount, prevPagination, nextPagination, }: ExpectedMonitorStatesPage) => { const decoded = MonitorSummariesResultType.decode(response); expect(isRight(decoded)).to.be.ok(); if (isRight(decoded)) { - const { summaries, prevPagePagination, nextPagePagination, totalSummaryCount } = decoded.right; + const { summaries, prevPagePagination, nextPagePagination } = decoded.right; expect(summaries).to.have.length(size); expect(summaries?.map((s) => s.monitor_id)).to.eql(statesIds); expect( @@ -55,7 +53,6 @@ const checkMonitorStatesResponse = ({ expect(point.timestamp).to.be.lessThan(absTo); }); }); - expect(totalSummaryCount).to.be(totalCount); expect(prevPagePagination).to.be(prevPagination); expect(nextPagePagination).to.eql(nextPagination); } @@ -84,7 +81,6 @@ export default function ({ getService }: FtrProviderContext) { absFrom, absTo, size: 1, - totalCount: 2000, prevPagination: null, nextPagination: null, }); @@ -468,7 +464,6 @@ export default function ({ getService }: FtrProviderContext) { }, ]; - const totalCount = 2000; let pagination: string | null = null; for (let page = 1; page <= expectedPageCount; page++) { const baseUrl = `${API_URLS.MONITOR_LIST}?dateRangeStart=${from}&dateRangeEnd=${to}&pageSize=${size}`; @@ -482,7 +477,6 @@ export default function ({ getService }: FtrProviderContext) { absFrom, absTo, size, - totalCount, }); // Test to see if the previous page pagination works on every page (other than the first) @@ -496,7 +490,6 @@ export default function ({ getService }: FtrProviderContext) { absFrom, absTo, size, - totalCount, }); } } @@ -525,7 +518,6 @@ export default function ({ getService }: FtrProviderContext) { absFrom, absTo, size: LENGTH, - totalCount: 2000, prevPagination: null, nextPagination: '{"cursorDirection":"AFTER","sortOrder":"ASC","cursorKey":{"monitor_id":"0009-up"}}',