Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Uptime] replace fetch with kibana http #59881

Merged
merged 12 commits into from
Mar 18, 2020
Merged
18 changes: 18 additions & 0 deletions x-pack/legacy/plugins/uptime/common/constants/rest_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 enum API_URLS {
INDEX_PATTERN = `/api/uptime/index_pattern`,
MONITOR_LOCATIONS = `/api/uptime/monitor/locations`,
MONITOR_DURATION = `/api/uptime/monitor/duration`,
MONITOR_DETAILS = `/api/uptime/monitor/details`,
MONITOR_SELECTED = `/api/uptime/monitor/selected`,
MONITOR_STATUS = `/api/uptime/monitor/status`,
PINGS = '/api/uptime/pings',
PING_HISTOGRAM = `/api/uptime/ping/histogram`,
SNAPSHOT_COUNT = `/api/uptime/snapshot/count`,
FILTERS = `/api/uptime/filters`,
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/uptime/public/lib/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
export { combineFiltersAndUserSearch } from './combine_filters_and_user_search';
export { convertMicrosecondsToMilliseconds } from './convert_measurements';
export * from './observability_integration';
export { getApiPath } from './get_api_path';
export { getChartDateLabel } from './charts';
export { parameterizeValues } from './parameterize_values';
export { seriesHasDownValues } from './series_has_down_values';
export { stringifyKueries } from './stringify_kueries';
export { UptimeUrlParams, getSupportedUrlParams } from './url_params';

This file was deleted.

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 @@ -5,17 +5,19 @@
*/

import { fetchSnapshotCount } from '../snapshot';
import { apiService } from '../utils';
import { HttpFetchError } from '../../../../../../../../src/core/public/http/http_fetch_error';

describe('snapshot API', () => {
let fetchMock: jest.SpyInstance<Promise<Partial<Response>>>;
let mockResponse: Partial<Response>;

beforeEach(() => {
fetchMock = jest.spyOn(window, 'fetch');
mockResponse = {
ok: true,
json: () => new Promise(r => r({ up: 3, down: 12, total: 15 })),
apiService.http = {
get: jest.fn(),
};
fetchMock = jest.spyOn(apiService.http, 'get');
mockResponse = { up: 3, down: 12, total: 15 };
});

afterEach(() => {
Expand All @@ -31,14 +33,19 @@ describe('snapshot API', () => {
filters: 'monitor.id:"auto-http-0X21EE76EAC459873F"',
statusFilter: 'up',
});
expect(fetchMock).toHaveBeenCalledWith(
'/api/uptime/snapshot/count?dateRangeStart=now-15m&dateRangeEnd=now&filters=monitor.id%3A%22auto-http-0X21EE76EAC459873F%22&statusFilter=up'
);
expect(fetchMock).toHaveBeenCalledWith('/api/uptime/snapshot/count', {
query: {
dateRangeEnd: 'now',
dateRangeStart: 'now-15m',
filters: 'monitor.id:"auto-http-0X21EE76EAC459873F"',
statusFilter: 'up',
},
});
expect(resp).toEqual({ up: 3, down: 12, total: 15 });
});

it(`throws when server response doesn't correspond to expected type`, async () => {
mockResponse = { ok: true, json: () => new Promise(r => r({ foo: 'bar' })) };
mockResponse = { foo: 'bar' };
fetchMock.mockReturnValue(new Promise(r => r(mockResponse)));
let error: Error | undefined;
try {
Expand All @@ -56,8 +63,8 @@ describe('snapshot API', () => {
});

it('throws an error when response is not ok', async () => {
mockResponse = { ok: false, statusText: 'There was an error fetching your data.' };
fetchMock.mockReturnValue(new Promise(r => r(mockResponse)));
mockResponse = new HttpFetchError('There was an error fetching your data.', 'error', {});
fetchMock.mockReturnValue(mockResponse);
let error: Error | undefined;
try {
await fetchSnapshotCount({
Expand Down
17 changes: 4 additions & 13 deletions x-pack/legacy/plugins/uptime/public/state/api/index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getApiPath } from '../../lib/helper';
import { API_URLS } from '../../../common/constants/rest_api';
import { apiService } from './utils';

interface APIParams {
basePath: string;
}

export const fetchIndexPattern = async ({ basePath }: APIParams) => {
const url = getApiPath(`/api/uptime/index_pattern`, basePath);

const response = await fetch(url);
if (!response.ok) {
throw new Error(response.statusText);
}
return await response.json();
export const fetchIndexPattern = async () => {
return await apiService.get(API_URLS.INDEX_PATTERN);
};
33 changes: 8 additions & 25 deletions x-pack/legacy/plugins/uptime/public/state/api/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { PathReporter } from 'io-ts/lib/PathReporter';
import { getApiPath } from '../../lib/helper';
import { BaseParams } from './types';
import {
MonitorDetailsType,
Expand All @@ -14,61 +13,45 @@ import {
MonitorLocationsType,
} from '../../../common/runtime_types';
import { QueryParams } from '../actions/types';
import { apiService } from './utils';
import { API_URLS } from '../../../common/constants/rest_api';

interface ApiRequest {
monitorId: string;
basePath: string;
}

export type MonitorQueryParams = BaseParams & ApiRequest;

export const fetchMonitorDetails = async ({
monitorId,
basePath,
dateStart,
dateEnd,
}: MonitorQueryParams): Promise<MonitorDetails> => {
const url = getApiPath(`/api/uptime/monitor/details`, basePath);
const params = {
monitorId,
dateStart,
dateEnd,
};
const urlParams = new URLSearchParams(params).toString();
const response = await fetch(`${url}?${urlParams}`);
const response = await apiService.get(API_URLS.MONITOR_DETAILS, params);

if (!response.ok) {
throw new Error(response.statusText);
}
return response.json().then(data => {
PathReporter.report(MonitorDetailsType.decode(data));
return data;
});
PathReporter.report(MonitorDetailsType.decode(response));
return response;
};

type ApiParams = QueryParams & ApiRequest;

export const fetchMonitorLocations = async ({
monitorId,
basePath,
dateStart,
dateEnd,
}: ApiParams): Promise<MonitorLocations> => {
const url = getApiPath(`/api/uptime/monitor/locations`, basePath);

const params = {
dateStart,
dateEnd,
monitorId,
};
const urlParams = new URLSearchParams(params).toString();
const response = await fetch(`${url}?${urlParams}`);
const response = await apiService.get(API_URLS.MONITOR_LOCATIONS, params);

if (!response.ok) {
throw new Error(response.statusText);
}
return response.json().then(data => {
PathReporter.report(MonitorLocationsType.decode(data));
return data;
});
PathReporter.report(MonitorLocationsType.decode(response));
return response;
};
23 changes: 5 additions & 18 deletions x-pack/legacy/plugins/uptime/public/state/api/monitor_duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { stringify } from 'query-string';

import { getApiPath } from '../../lib/helper';
import { BaseParams } from './types';
import { apiService } from './utils';
import { API_URLS } from '../../../common/constants/rest_api';

export const fetchMonitorDuration = async ({
basePath,
monitorId,
dateStart,
dateEnd,
}: BaseParams) => {
const url = getApiPath(`/api/uptime/monitor/duration`, basePath);

const params = {
export const fetchMonitorDuration = async ({ monitorId, dateStart, dateEnd }: BaseParams) => {
const queryParams = {
monitorId,
dateStart,
dateEnd,
};
const urlParams = stringify(params);

const response = await fetch(`${url}?${urlParams}`);
if (!response.ok) {
throw new Error(response.statusText);
}
return await response.json();
return await apiService.get(API_URLS.MONITOR_DURATION, queryParams);
};
30 changes: 9 additions & 21 deletions x-pack/legacy/plugins/uptime/public/state/api/monitor_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,34 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getApiPath } from '../../lib/helper';
import { QueryParams } from '../actions/types';
import { Ping } from '../../../common/graphql/types';
import { apiService } from './utils';
import { API_URLS } from '../../../common/constants/rest_api';

export interface APIParams {
basePath: string;
monitorId: string;
}

export const fetchSelectedMonitor = async ({ basePath, monitorId }: APIParams): Promise<Ping> => {
const url = getApiPath(`/api/uptime/monitor/selected`, basePath);
const params = {
export const fetchSelectedMonitor = async ({ monitorId }: APIParams): Promise<Ping> => {
const queryParams = {
monitorId,
};
const urlParams = new URLSearchParams(params).toString();
const response = await fetch(`${url}?${urlParams}`);
if (!response.ok) {
throw new Error(response.statusText);
}
const responseData = await response.json();
return responseData;

return await apiService.get(API_URLS.MONITOR_SELECTED, queryParams);
};

export const fetchMonitorStatus = async ({
basePath,
monitorId,
dateStart,
dateEnd,
}: QueryParams & APIParams): Promise<Ping> => {
const url = getApiPath(`/api/uptime/monitor/status`, basePath);
const params = {
const queryParams = {
monitorId,
dateStart,
dateEnd,
};
const urlParams = new URLSearchParams(params).toString();
const response = await fetch(`${url}?${urlParams}`);
if (!response.ok) {
throw new Error(response.statusText);
}
const responseData = await response.json();
return responseData;

return await apiService.get(API_URLS.MONITOR_STATUS, queryParams);
};
Loading