-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…113953) * [Stack Monitoring] Covnert cluster listing view to React * Fixing localStorage Co-authored-by: Chris Cowan <chris@chriscowan.us>
- Loading branch information
1 parent
6d68541
commit 2997a69
Showing
7 changed files
with
244 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
x-pack/plugins/monitoring/public/application/hooks/use_alerts_modal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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 { useKibana } from '../../../../../../src/plugins/kibana_react/public'; | ||
import { showAlertsToast } from '../../alerts/lib/alerts_toast'; | ||
import { ajaxErrorHandlersProvider } from '../../lib/ajax_error_handler'; | ||
|
||
export const useAlertsModal = () => { | ||
const { services } = useKibana(); | ||
|
||
function shouldShowAlertsModal(alerts: {}) { | ||
const modalHasBeenShown = | ||
window.sessionStorage.getItem('ALERTS_MODAL_HAS_BEEN_SHOWN') === 'true'; | ||
const decisionMade = window.localStorage.getItem('ALERTS_MODAL_DECISION_MADE') === 'true'; | ||
|
||
if (Object.keys(alerts).length > 0) { | ||
window.localStorage.setItem('ALERTS_MODAL_DECISION_MADE', 'true'); | ||
return false; | ||
} else if (!modalHasBeenShown && !decisionMade) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
async function enableAlerts() { | ||
try { | ||
const { data } = await services.http?.post('../api/monitoring/v1/alerts/enable', {}); | ||
window.localStorage.setItem('ALERTS_MODAL_DECISION_MADE', 'true'); | ||
showAlertsToast(data); | ||
} catch (err) { | ||
const ajaxErrorHandlers = ajaxErrorHandlersProvider(); | ||
return ajaxErrorHandlers(err); | ||
} | ||
} | ||
|
||
function notAskAgain() { | ||
window.localStorage.setItem('ALERTS_MODAL_DECISION_MADE', 'true'); | ||
} | ||
|
||
function hideModalForSession() { | ||
window.sessionStorage.setItem('ALERTS_MODAL_HAS_BEEN_SHOWN', 'true'); | ||
} | ||
|
||
return { | ||
shouldShowAlertsModal, | ||
enableAlerts, | ||
notAskAgain, | ||
hideModalForSession, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
x-pack/plugins/monitoring/public/application/pages/home/cluster_listing.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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, useContext, useEffect, useState } from 'react'; | ||
import { Redirect } from 'react-router-dom'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; | ||
// @ts-ignore | ||
import { Listing } from '../../../components/cluster/listing'; | ||
import { EnableAlertsModal } from '../../../alerts/enable_alerts_modal'; | ||
import { GlobalStateContext } from '../../global_state_context'; | ||
import { ExternalConfigContext } from '../../external_config_context'; | ||
import { ComponentProps } from '../../route_init'; | ||
import { useTable } from '../../hooks/use_table'; | ||
import { PageTemplate, TabMenuItem } from '../page_template'; | ||
import { BreadcrumbContainer } from '../../hooks/use_breadcrumbs'; | ||
import { fetchClusters } from '../../../lib/fetch_clusters'; | ||
|
||
const pageTitle = i18n.translate('xpack.monitoring.cluster.listing.pageTitle', { | ||
defaultMessage: 'Cluster listing', | ||
}); | ||
|
||
const tabTitle = i18n.translate('xpack.monitoring.cluster.listing.tabTitle', { | ||
defaultMessage: 'Clusters', | ||
}); | ||
|
||
const getAlerts = (clusters: any[]) => { | ||
return clusters.reduce( | ||
(alerts, cluster) => ({ ...alerts, ...((cluster.alerts && cluster.alerts.list) || {}) }), | ||
{} | ||
); | ||
}; | ||
|
||
export const ClusterListing: React.FC<ComponentProps> = () => { | ||
const globalState = useContext(GlobalStateContext); | ||
const externalConfig = useContext(ExternalConfigContext); | ||
const { services } = useKibana<{ data: any }>(); | ||
const [clusters, setClusters] = useState([] as any); | ||
const { update: updateBreadcrumbs } = useContext(BreadcrumbContainer.Context); | ||
|
||
const fakeScope = { | ||
$evalAsync: (fn: () => void) => fn(), | ||
filterQuery: '', // replace with something | ||
}; | ||
const { getPaginationTableProps } = useTable('clusters'); | ||
const { sorting, pagination, onTableChange } = getPaginationTableProps(); | ||
|
||
useEffect(() => { | ||
updateBreadcrumbs([ | ||
{ | ||
'data-test-subj': 'clusterListingBreadcrumb', | ||
text: tabTitle, | ||
}, | ||
]); | ||
}, [updateBreadcrumbs]); | ||
|
||
const tabs: TabMenuItem[] = [ | ||
{ | ||
id: 'clusters', | ||
label: tabTitle, | ||
testSubj: 'clusterListingTab', | ||
route: '/home', | ||
}, | ||
]; | ||
|
||
const getPageData = useCallback(async () => { | ||
const bounds = services.data?.query.timefilter.timefilter.getBounds(); | ||
try { | ||
if (services.http?.fetch) { | ||
const response = await fetchClusters({ | ||
fetch: services.http.fetch, | ||
timeRange: { | ||
min: bounds.min.toISOString(), | ||
max: bounds.max.toISOString(), | ||
}, | ||
ccs: globalState.ccs, | ||
codePaths: ['all'], | ||
}); | ||
setClusters(response); | ||
} | ||
} catch (err) { | ||
// TODO: handle errors | ||
} | ||
}, [globalState, services.data?.query.timefilter.timefilter, services.http]); | ||
|
||
if (globalState.save && clusters.length === 1) { | ||
globalState.cluster_uuid = clusters[0].cluster_uuid; | ||
globalState.save(); | ||
} | ||
|
||
return ( | ||
<PageTemplate tabs={tabs} title={pageTitle} pageTitle={pageTitle} getPageData={getPageData}> | ||
{clusters.length === 1 && <Redirect to={{ pathname: '/overview' }} />} | ||
<Listing | ||
clusters={clusters} | ||
angular={{ | ||
scope: fakeScope, | ||
globalState, | ||
storage: { | ||
get: (key: string) => window.localStorage.getItem(key), | ||
set: (key: string, value: string) => window.localStorage.setItem(key, value), | ||
}, | ||
showLicenseExpiration: externalConfig.showLicenseExpiration, | ||
}} | ||
sorting={sorting} | ||
pagination={pagination} | ||
onTableChange={onTableChange} | ||
/> | ||
<EnableAlertsModal alerts={getAlerts(clusters)} /> | ||
</PageTemplate> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 { HttpHandler } from 'kibana/public'; | ||
import { STANDALONE_CLUSTER_CLUSTER_UUID } from '../../common/constants'; | ||
|
||
interface Params { | ||
timeRange: { min: string; max: string }; | ||
fetch: HttpHandler; | ||
clusterUuid?: string | null; | ||
ccs?: boolean; | ||
codePaths?: string[]; | ||
} | ||
|
||
export function formatClusters(clusters: any) { | ||
return clusters.map(formatCluster); | ||
} | ||
|
||
export function formatCluster(cluster: any) { | ||
if (cluster.cluster_uuid === STANDALONE_CLUSTER_CLUSTER_UUID) { | ||
cluster.cluster_name = 'Standalone Cluster'; | ||
} | ||
return cluster; | ||
} | ||
|
||
export const fetchClusters = async ({ clusterUuid, timeRange, fetch, ccs, codePaths }: Params) => { | ||
let url = '../api/monitoring/v1/clusters'; | ||
if (clusterUuid) { | ||
url += `/${clusterUuid}`; | ||
} | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
ccs, | ||
timeRange, | ||
codePaths, | ||
}), | ||
}); | ||
|
||
return formatClusters(response); | ||
}; |