Skip to content

Commit

Permalink
added filters into expanded list query
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Dec 9, 2019
1 parent 3d36356 commit cb906eb
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions x-pack/legacy/plugins/uptime/public/state/actions/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
};
}

Expand Down
13 changes: 13 additions & 0 deletions x-pack/legacy/plugins/uptime/public/state/actions/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 19 additions & 3 deletions x-pack/legacy/plugins/uptime/public/state/api/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MonitorDetails> => {
const url = getApiPath(`/api/uptime/monitor/details?monitorId=${monitorId}`, basePath);
const response = await fetch(url);
dateStart,
dateEnd,
filters,
statusFilter,
}: MonitorQueryParams): Promise<MonitorDetails> => {
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);
}
Expand Down
13 changes: 13 additions & 0 deletions x-pack/legacy/plugins/uptime/public/state/api/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 16 additions & 2 deletions x-pack/legacy/plugins/uptime/public/state/effects/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) {
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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@ export interface UMMonitorsAdapter {
): Promise<MonitorChart>;
getFilterBar(request: any, dateRangeStart: string, dateRangeEnd: string): Promise<any>;
getMonitorPageTitle(request: any, monitorId: string): Promise<MonitorPageTitle | null>;
getMonitorDetails(request: any, monitorId: string): Promise<any>;
getMonitorDetails(
request: any,
monitorId: string,
dateStart: string,
dateEnd: string,
filters?: string,
statusFilter?: string
): Promise<any>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,14 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter {
};
}

public async getMonitorDetails(request: any, monitorId: string): Promise<MonitorDetails> {
public async getMonitorDetails(
request: any,
monitorId: string,
dateStart: string,
dateEnd: string,
filters?: string,
statusFilter?: string
): Promise<MonitorDetails> {
const params = {
index: INDEX_NAMES.HEARTBEAT,
body: {
Expand All @@ -289,11 +296,8 @@ export class ElasticsearchMonitorsAdapter implements UMMonitorsAdapter {
},
],
filter: [
{
term: {
'monitor.id': monitorId,
},
},
{ range: { '@timestamp': { gte: dateStart, lte: dateEnd } } },
{ term: { 'monitor.id': monitorId } },
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ export const createGetMonitorDetailsRoute: UMRestApiRouteCreator = (libs: UMServ
tags: ['access:uptime'],
},
handler: async (_context, request, response): Promise<any> => {
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
)),
},
});
},
});

0 comments on commit cb906eb

Please sign in to comment.