From cb906ebfcfc92f19946f64456bc62d5664b888db Mon Sep 17 00:00:00 2001 From: shahzad Date: Mon, 9 Dec 2019 18:49:27 +0100 Subject: [PATCH] added filters into expanded list query --- .../monitor_list_drawer.tsx | 24 ++++++++++++++++--- .../uptime/public/state/actions/monitor.ts | 11 +++++---- .../uptime/public/state/actions/types.ts | 13 ++++++++++ .../uptime/public/state/api/monitor.ts | 22 ++++++++++++++--- .../plugins/uptime/public/state/api/types.ts | 13 ++++++++++ .../uptime/public/state/effects/monitor.ts | 18 ++++++++++++-- .../lib/adapters/monitors/adapter_types.ts | 9 ++++++- .../elasticsearch_monitors_adapter.ts | 16 ++++++++----- .../rest_api/monitors/monitors_details.ts | 13 ++++++++-- 9 files changed, 118 insertions(+), 21 deletions(-) create mode 100644 x-pack/legacy/plugins/uptime/public/state/actions/types.ts create mode 100644 x-pack/legacy/plugins/uptime/public/state/api/types.ts diff --git a/x-pack/legacy/plugins/uptime/public/components/functional/monitor_list/monitor_list_drawer/monitor_list_drawer.tsx b/x-pack/legacy/plugins/uptime/public/components/functional/monitor_list/monitor_list_drawer/monitor_list_drawer.tsx index 86e60508a4078..179020eb4aa89 100644 --- a/x-pack/legacy/plugins/uptime/public/components/functional/monitor_list/monitor_list_drawer/monitor_list_drawer.tsx +++ b/x-pack/legacy/plugins/uptime/public/components/functional/monitor_list/monitor_list_drawer/monitor_list_drawer.tsx @@ -16,6 +16,8 @@ import { MostRecentError } from './most_recent_error'; import { getMonitorDetails } from '../../../../state/selectors'; import { MonitorStatusList } from './monitor_status_list'; import { MonitorDetails } from '../../../../../common/runtime_types'; +import { useUrlParams } from '../../../../hooks'; +import { MonitorDetailsActionPayload } from '../../../../state/actions/types'; const ContainerDiv = styled.div` padding: 10px; @@ -51,9 +53,24 @@ export function MonitorListDrawerComponent({ if (!summary || !summary.state.checks) { return null; } + const { monitor_id: monitorId } = summary; + const [getUrlParams] = useUrlParams(); + const { + dateRangeStart: dateStart, + dateRangeEnd: dateEnd, + statusFilter, + filters, + } = getUrlParams(); + useEffect(() => { - loadMonitorDetails(summary.monitor_id); - }, []); + loadMonitorDetails({ + dateStart, + dateEnd, + statusFilter, + filters, + monitorId, + }); + }, [dateStart, dateEnd, statusFilter, filters]); const monitorUrl: string | undefined = get(summary.state.url, 'full', undefined); @@ -85,7 +102,8 @@ const mapStateToProps = (state: AppState, { summary }: any) => ({ }); const mapDispatchToProps = (dispatch: any) => ({ - loadMonitorDetails: (monitorId: string) => dispatch(fetchMonitorDetails(monitorId)), + loadMonitorDetails: (actionPayload: MonitorDetailsActionPayload) => + dispatch(fetchMonitorDetails(actionPayload)), }); export const MonitorListDrawer = connect( diff --git a/x-pack/legacy/plugins/uptime/public/state/actions/monitor.ts b/x-pack/legacy/plugins/uptime/public/state/actions/monitor.ts index 40738740e5841..1b6f82116d48a 100644 --- a/x-pack/legacy/plugins/uptime/public/state/actions/monitor.ts +++ b/x-pack/legacy/plugins/uptime/public/state/actions/monitor.ts @@ -4,18 +4,21 @@ * you may not use this file except in compliance with the Elastic License. */ +import { MonitorDetailsActionPayload } from './types'; +import { MonitorError } from '../../../common/runtime_types'; + export const FETCH_MONITOR_DETAILS = 'FETCH_MONITOR_DETAILS'; export const FETCH_MONITOR_DETAILS_SUCCESS = 'FETCH_MONITOR_DETAILS_SUCCESS'; export const FETCH_MONITOR_DETAILS_FAIL = 'FETCH_MONITOR_DETAILS_FAIL'; export interface MonitorDetailsState { monitorId: string; - error: Error; + error: MonitorError; } interface GetMonitorDetailsAction { type: typeof FETCH_MONITOR_DETAILS; - payload: string; + payload: MonitorDetailsActionPayload; } interface GetMonitorDetailsSuccessAction { @@ -28,10 +31,10 @@ interface GetMonitorDetailsFailAction { payload: any; } -export function fetchMonitorDetails(monitorId: string): GetMonitorDetailsAction { +export function fetchMonitorDetails(payload: MonitorDetailsActionPayload): GetMonitorDetailsAction { return { type: FETCH_MONITOR_DETAILS, - payload: monitorId, + payload, }; } diff --git a/x-pack/legacy/plugins/uptime/public/state/actions/types.ts b/x-pack/legacy/plugins/uptime/public/state/actions/types.ts new file mode 100644 index 0000000000000..186ddd68620b1 --- /dev/null +++ b/x-pack/legacy/plugins/uptime/public/state/actions/types.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export interface MonitorDetailsActionPayload { + monitorId: string; + dateStart: string; + dateEnd: string; + filters?: string; + statusFilter?: string; +} diff --git a/x-pack/legacy/plugins/uptime/public/state/api/monitor.ts b/x-pack/legacy/plugins/uptime/public/state/api/monitor.ts index d043cf7119472..fd8f5ce0bc85e 100644 --- a/x-pack/legacy/plugins/uptime/public/state/api/monitor.ts +++ b/x-pack/legacy/plugins/uptime/public/state/api/monitor.ts @@ -7,18 +7,34 @@ import { ThrowReporter } from 'io-ts/lib/ThrowReporter'; import { getApiPath } from '../../lib/helper'; import { MonitorDetailsType, MonitorDetails } from '../../../common/runtime_types'; +import { BaseParams } from './types'; interface ApiRequest { monitorId: string; basePath: string; } +export type MonitorQueryParams = BaseParams & ApiRequest; + export const fetchMonitorDetails = async ({ monitorId, basePath, -}: ApiRequest): Promise => { - const url = getApiPath(`/api/uptime/monitor/details?monitorId=${monitorId}`, basePath); - const response = await fetch(url); + dateStart, + dateEnd, + filters, + statusFilter, +}: MonitorQueryParams): Promise => { + const url = getApiPath(`/api/uptime/monitor/details`, basePath); + const params = { + monitorId, + dateStart, + dateEnd, + ...(filters && { filters }), + ...(statusFilter && { statusFilter }), + }; + const urlParams = new URLSearchParams(params).toString(); + const response = await fetch(`${url}?${urlParams}`); + if (!response.ok) { throw new Error(response.statusText); } diff --git a/x-pack/legacy/plugins/uptime/public/state/api/types.ts b/x-pack/legacy/plugins/uptime/public/state/api/types.ts new file mode 100644 index 0000000000000..6a9cac1025b56 --- /dev/null +++ b/x-pack/legacy/plugins/uptime/public/state/api/types.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export interface BaseParams { + basePath: string; + dateStart: string; + dateEnd: string; + filters?: string; + statusFilter?: string; +} diff --git a/x-pack/legacy/plugins/uptime/public/state/effects/monitor.ts b/x-pack/legacy/plugins/uptime/public/state/effects/monitor.ts index 529b9041c9093..becf875bdb437 100644 --- a/x-pack/legacy/plugins/uptime/public/state/effects/monitor.ts +++ b/x-pack/legacy/plugins/uptime/public/state/effects/monitor.ts @@ -13,12 +13,26 @@ import { } from '../actions/monitor'; import { fetchMonitorDetails } from '../api'; import { getBasePath } from '../selectors'; +import { MonitorDetailsActionPayload } from '../actions/types'; function* monitorDetailsEffect(action: Action) { - const monitorId: string = action.payload; + const { + monitorId, + dateStart, + dateEnd, + statusFilter, + filters, + }: MonitorDetailsActionPayload = action.payload; try { const basePath = yield select(getBasePath); - const response = yield call(fetchMonitorDetails, { monitorId, basePath }); + const response = yield call(fetchMonitorDetails, { + monitorId, + basePath, + dateStart, + dateEnd, + statusFilter, + filters, + }); yield put({ type: FETCH_MONITOR_DETAILS_SUCCESS, payload: response }); } catch (error) { yield put({ type: FETCH_MONITOR_DETAILS_FAIL, payload: error.message }); diff --git a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/adapter_types.ts b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/adapter_types.ts index f6ac587b0ceec..f3a035a0b1e39 100644 --- a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/adapter_types.ts +++ b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/adapter_types.ts @@ -16,5 +16,12 @@ export interface UMMonitorsAdapter { ): Promise; getFilterBar(request: any, dateRangeStart: string, dateRangeEnd: string): Promise; getMonitorPageTitle(request: any, monitorId: string): Promise; - getMonitorDetails(request: any, monitorId: string): Promise; + getMonitorDetails( + request: any, + monitorId: string, + dateStart: string, + dateEnd: string, + filters?: string, + statusFilter?: string + ): Promise; } diff --git a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts index c7ebc77af3567..62cab859c391c 100644 --- a/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts +++ b/x-pack/legacy/plugins/uptime/server/lib/adapters/monitors/elasticsearch_monitors_adapter.ts @@ -273,7 +273,14 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter { }; } - public async getMonitorDetails(request: any, monitorId: string): Promise { + public async getMonitorDetails( + request: any, + monitorId: string, + dateStart: string, + dateEnd: string, + filters?: string, + statusFilter?: string + ): Promise { const params = { index: INDEX_NAMES.HEARTBEAT, body: { @@ -289,11 +296,8 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter { }, ], filter: [ - { - term: { - 'monitor.id': monitorId, - }, - }, + { range: { '@timestamp': { gte: dateStart, lte: dateEnd } } }, + { term: { 'monitor.id': monitorId } }, ], }, }, diff --git a/x-pack/legacy/plugins/uptime/server/rest_api/monitors/monitors_details.ts b/x-pack/legacy/plugins/uptime/server/rest_api/monitors/monitors_details.ts index 00860248ff153..a5aea272412b4 100644 --- a/x-pack/legacy/plugins/uptime/server/rest_api/monitors/monitors_details.ts +++ b/x-pack/legacy/plugins/uptime/server/rest_api/monitors/monitors_details.ts @@ -20,10 +20,19 @@ export const createGetMonitorDetailsRoute: UMRestApiRouteCreator = (libs: UMServ tags: ['access:uptime'], }, handler: async (_context, request, response): Promise => { - const { monitorId } = request.query; + const { monitorId, dateStart, dateEnd, filters, statusFilter } = request.query; return response.ok({ - body: { ...(await libs.monitors.getMonitorDetails(request, monitorId)) }, + body: { + ...(await libs.monitors.getMonitorDetails( + request, + monitorId, + dateStart, + dateEnd, + filters, + statusFilter + )), + }, }); }, });