Skip to content

Commit

Permalink
[Uptime] Remove unused code in monitor list API (#84312)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
shahzad31 and kibanamachine authored Dec 8, 2020
1 parent 58d1df1 commit 3216fce
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 62 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ describe('MonitorList component', () => {
nextPagePagination: null,
prevPagePagination: null,
summaries: [testFooSummary, testBarSummary],
totalSummaryCount: 2,
};
};

Expand Down Expand Up @@ -164,7 +163,6 @@ describe('MonitorList component', () => {
summaries: [],
nextPagePagination: null,
prevPagePagination: null,
totalSummaryCount: 0,
},
loading: true,
}}
Expand Down Expand Up @@ -236,7 +234,6 @@ describe('MonitorList component', () => {
sortOrder: SortOrder.ASC,
}),
summaries: [testFooSummary, testBarSummary],
totalSummaryCount: 2,
};
});

Expand Down Expand Up @@ -265,7 +262,6 @@ describe('MonitorList component', () => {
summaries: [],
nextPagePagination: null,
prevPagePagination: null,
totalSummaryCount: 0,
},
loading: false,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"monitorStates": {
"prevPagePagination": null,
"nextPagePagination": null,
"totalSummaryCount": 147428,
"summaries": [
{
"monitor_id": "andrewvc-com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export const mockStore = {
prevPagePagination: null,
nextPagePagination: null,
summaries: [],
totalSummaryCount: 0,
},
loading: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const initialState: MonitorList = {
nextPagePagination: null,
prevPagePagination: null,
summaries: [],
totalSummaryCount: 0,
},
loading: false,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('state selectors', () => {
prevPagePagination: null,
nextPagePagination: null,
summaries: [],
totalSummaryCount: 0,
},
loading: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,15 +43,15 @@ const statusCount = async (context: QueryContext): Promise<Snapshot> => {
});

return (
res.aggregations?.counts?.value ?? {
(res.aggregations?.counts?.value as Snapshot) ?? {
total: 0,
up: 0,
down: 0,
}
);
};

const statusCountBody = (filters: any): any => {
const statusCountBody = (filters: ESFilter[]) => {
return {
size: 0,
query: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
''
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { set } from '@elastic/safer-lodash-set';
import { get } from 'lodash';
import { QueryContext } from './query_context';

/**
Expand All @@ -21,9 +20,10 @@ export const findPotentialMatches = async (
) => {
const { body: queryResult } = await query(queryContext, searchAfter, size);
const monitorIds: string[] = [];
get<any>(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 {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,7 @@ export class QueryContext {
hasTimespanCache?: boolean;

constructor(
database: any,
database: UptimeESClient,
dateRangeStart: string,
dateRangeEnd: string,
pagination: CursorPagination,
Expand All @@ -38,15 +39,16 @@ export class QueryContext {
this.statusFilter = statusFilter;
}

async search(params: any): Promise<any> {
return this.callES.search({ body: params.body });
async search<TParams>(params: TParams) {
return this.callES.search(params);
}

async count(params: any): Promise<any> {
return this.callES.count(params);
const { body } = await this.callES.count(params);
return body;
}

async dateAndCustomFilters(): Promise<any[]> {
async dateAndCustomFilters(): Promise<ESFilter[]> {
const clauses = [await this.dateRangeFilter()];
if (this.filterClause) {
clauses.push(this.filterClause);
Expand Down
36 changes: 16 additions & 20 deletions x-pack/plugins/uptime/server/rest_api/monitors/monitor_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface ExpectedMonitorStatesPage {
absFrom: number;
absTo: number;
size: number;
totalCount: number;
prevPagination: null | string;
nextPagination: null | string;
}
Expand All @@ -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(
Expand All @@ -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);
}
Expand Down Expand Up @@ -84,7 +81,6 @@ export default function ({ getService }: FtrProviderContext) {
absFrom,
absTo,
size: 1,
totalCount: 2000,
prevPagination: null,
nextPagination: null,
});
Expand Down Expand Up @@ -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}`;
Expand All @@ -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)
Expand All @@ -496,7 +490,6 @@ export default function ({ getService }: FtrProviderContext) {
absFrom,
absTo,
size,
totalCount,
});
}
}
Expand Down Expand Up @@ -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"}}',
Expand Down

0 comments on commit 3216fce

Please sign in to comment.