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

[Stack Monitoring] Adding alerts to react app #114029

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1c1c70e
[Stack Monitoring] Adding alerts to react app
simianhacker Oct 5, 2021
0be936a
Merge branch 'master' of github.com:elastic/kibana into issue-111774-…
simianhacker Oct 5, 2021
bd9071c
Fixing global state context path
simianhacker Oct 6, 2021
eb13d50
adding alerts to pages; adding alerts model to cluster_overview; remo…
simianhacker Oct 6, 2021
6c4dbdc
Fixing request for enable alerts
simianhacker Oct 6, 2021
45a10a8
remove loadAlerts from page template
simianhacker Oct 6, 2021
e71f91c
Adding request error handlers
simianhacker Oct 6, 2021
ba3c7ff
Merge branch 'master' of github.com:elastic/kibana into issue-111774-…
simianhacker Oct 6, 2021
924aa88
removing redundent error handling
simianhacker Oct 6, 2021
344852b
Changing useRequestErrorHandler function to be async due to error.res…
simianhacker Oct 6, 2021
716a1dd
removing old comment
simianhacker Oct 6, 2021
58d2888
Fixing contexts paths
simianhacker Oct 6, 2021
19b6fb1
Converting ajaxRequestErrorHandler to useRequestErrorHandler
simianhacker Oct 6, 2021
a85787c
Merge branch 'master' of github.com:elastic/kibana into issue-111774-…
simianhacker Oct 7, 2021
ba92c70
Refactoring error handler for page template and setup mode
simianhacker Oct 7, 2021
d19b722
Merge branch 'master' of github.com:elastic/kibana into issue-111774-…
simianhacker Oct 7, 2021
6d733c6
Removing unnecessary async/await
simianhacker Oct 7, 2021
40a6d2e
Removing unnecessary async/await in useClusters
simianhacker Oct 7, 2021
0c059ad
adding alertTypeIds to each page
simianhacker Oct 7, 2021
c1de323
fixing instance count
simianhacker Oct 7, 2021
6c5110e
Adding alertTypeIds to index page
simianhacker Oct 7, 2021
225ee7e
Adding alert filters for specific pages
simianhacker Oct 7, 2021
3a126f2
Adding alerts to Logstash nodes
simianhacker Oct 7, 2021
0e73f5a
Merge branch 'master' into issue-111774-add-alerts-to-sm-react
kibanamachine Oct 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions x-pack/plugins/monitoring/public/alerts/alerts_dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import {
import { i18n } from '@kbn/i18n';
import React, { useState } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { Legacy } from '../legacy_shims';
import { useKibana } from '../../../../../src/plugins/kibana_react/public';
import { MonitoringStartPluginDependencies } from '../types';
import { useAlertsModal } from '../application/hooks/use_alerts_modal';

export const AlertsDropdown: React.FC<{}> = () => {
const $injector = Legacy.shims.getAngularInjector();
const alertsEnableModalProvider: any = $injector.get('enableAlertsModal');
const alertsEnableModalProvider = useAlertsModal();
const { navigateToApp } =
useKibana<MonitoringStartPluginDependencies['core']>().services.application;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/
import React, { createContext } from 'react';
import { GlobalState } from '../url_state';
import { MonitoringStartPluginDependencies } from '../types';
import { TimeRange, RefreshInterval } from '../../../../../src/plugins/data/public';
import { Legacy } from '../legacy_shims';
import { GlobalState } from '../../url_state';
import { MonitoringStartPluginDependencies } from '../../types';
import { TimeRange, RefreshInterval } from '../../../../../../src/plugins/data/public';
import { Legacy } from '../../legacy_shims';

interface GlobalStateProviderProps {
query: MonitoringStartPluginDependencies['data']['query'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { AppMountParameters } from 'kibana/public';

interface ContextProps {
setHeaderActionMenu?: AppMountParameters['setHeaderActionMenu'];
}

export const HeaderActionMenuContext = React.createContext<ContextProps>({});
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import { showAlertsToast } from '../../alerts/lib/alerts_toast';
import { ajaxErrorHandlersProvider } from '../../lib/ajax_error_handler';
import { useRequestErrorHandler } from './use_request_error_handler';

export const useAlertsModal = () => {
const { services } = useKibana();
const handleRequestError = useRequestErrorHandler();

function shouldShowAlertsModal(alerts: {}) {
const modalHasBeenShown =
Expand All @@ -28,12 +29,11 @@ export const useAlertsModal = () => {

async function enableAlerts() {
try {
const { data } = await services.http?.post('../api/monitoring/v1/alerts/enable', {});
const response = await services.http?.post('../api/monitoring/v1/alerts/enable', {});
window.localStorage.setItem('ALERTS_MODAL_DECISION_MADE', 'true');
showAlertsToast(data);
showAlertsToast(response);
} catch (err) {
const ajaxErrorHandlers = ajaxErrorHandlersProvider();
return ajaxErrorHandlers(err);
await handleRequestError(err);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { useState, useEffect } from 'react';
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import { fetchClusters } from '../../lib/fetch_clusters';
import { useRequestErrorHandler } from './use_request_error_handler';

export function useClusters(clusterUuid?: string | null, ccs?: any, codePaths?: string[]) {
const { services } = useKibana<{ data: any }>();
Expand All @@ -17,6 +18,7 @@ export function useClusters(clusterUuid?: string | null, ccs?: any, codePaths?:

const [clusters, setClusters] = useState([] as any);
const [loaded, setLoaded] = useState<boolean | null>(false);
const handleRequestError = useRequestErrorHandler();

useEffect(() => {
async function makeRequest() {
Expand All @@ -34,13 +36,13 @@ export function useClusters(clusterUuid?: string | null, ccs?: any, codePaths?:
setClusters(response);
}
} catch (e) {
// TODO: Handle errors
await handleRequestError(e);
} finally {
setLoaded(true);
}
}
makeRequest();
}, [clusterUuid, ccs, services.http, codePaths, min, max]);
}, [handleRequestError, clusterUuid, ccs, services.http, codePaths, min, max]);

return { clusters, loaded };
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useCallback, useState, useContext } from 'react';
import createContainer from 'constate';
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import { Legacy } from '../../legacy_shims';
import { GlobalStateContext } from '../../application/global_state_context';
import { GlobalStateContext } from '../contexts/global_state_context';

interface TimeOptions {
from: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { useCallback } from 'react';
import { includes } from 'lodash';
import { IHttpFetchError } from 'kibana/public';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiButton, EuiSpacer, EuiText } from '@elastic/eui';
import { formatMsg } from '../../../../../../src/plugins/kibana_legacy/public';
import { toMountPoint, useKibana } from '../../../../../../src/plugins/kibana_react/public';
import { MonitoringStartPluginDependencies } from '../../types';

export async function formatMonitoringError(err: IHttpFetchError) {
if (err.response?.status && err.response?.status !== -1) {
const body = await err.response?.json();
simianhacker marked this conversation as resolved.
Show resolved Hide resolved
return (
<EuiText>
<p>{body.message}</p>
<EuiText size="xs">
<FormattedMessage
id="xpack.monitoring.ajaxErrorHandler.httpErrorMessage"
defaultMessage="HTTP {errStatus}"
values={{ errStatus: err.response?.status }}
/>
</EuiText>
</EuiText>
);
}

return formatMsg(err);
}

export const useRequestErrorHandler = () => {
const { services } = useKibana<MonitoringStartPluginDependencies>();
return useCallback(
async (err: IHttpFetchError) => {
if (err.response?.status === 403) {
// redirect to error message view
history.replaceState(null, '', '#/access-denied');
} else if (err.response?.status === 404 && !includes(window.location.hash, 'no-data')) {
// pass through if this is a 404 and we're already on the no-data page
const formattedError = await formatMonitoringError(err);
services.notifications?.toasts.addDanger({
title: toMountPoint(
<FormattedMessage
id="xpack.monitoring.ajaxErrorHandler.requestFailedNotificationTitle"
defaultMessage="Monitoring Request Failed"
/>
),
text: toMountPoint(
<div>
{formattedError}
<EuiSpacer />
<EuiButton size="s" color="danger" onClick={() => window.location.reload()}>
<FormattedMessage
id="xpack.monitoring.ajaxErrorHandler.requestFailedNotification.retryButtonLabel"
defaultMessage="Retry"
/>
</EuiButton>
</div>
),
});
} else {
services.notifications?.toasts.addDanger({
title: toMountPoint(
<FormattedMessage
id="xpack.monitoring.ajaxErrorHandler.requestErrorNotificationTitle"
defaultMessage="Monitoring Request Error"
/>
),
text: toMountPoint(formatMonitoringError(err)),
});
}
},
[services.notifications]
);
};
Loading