-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] Elasticsearch Overview view migration #111941
Merged
estermv
merged 11 commits into
elastic:master
from
estermv:elasticsearch-views-migration
Sep 15, 2021
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aadf10b
Elasticsearch overview first version
estermv e081a1e
Fix timezone in react components
estermv de9759f
Extraxt on brush behaviour from elasticsearch overview page
estermv 5277ec9
Fix overview component type
estermv b2bb932
Add elasticsearch pages template with some tabs
estermv 2171118
Conditionally add some tabs for elasticsearch pages
estermv 80166cc
Merge remote-tracking branch 'upstream/master' into elasticsearch-vie…
estermv 8ba3588
fix import and types
estermv 326d9ef
Filter angular errors in react
estermv 0eeaa78
remove disabled property from tabs
estermv 06f02c4
Add comment
estermv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
x-pack/plugins/monitoring/public/application/hooks/use_charts.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,75 @@ | ||
/* | ||
* 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 moment from 'moment'; | ||
import { useContext, useState, useEffect, useRef } from 'react'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { useKibana } from '../../../../../../src/plugins/kibana_react/public'; | ||
import { MonitoringTimeContainer } from '../hooks/use_monitoring_time'; | ||
|
||
export function useCharts() { | ||
const { services } = useKibana<{ data: any }>(); | ||
const history = useHistory(); | ||
const { handleTimeChange } = useContext(MonitoringTimeContainer.Context); | ||
|
||
const [zoomInLevel, setZoomInLevel] = useState(0); | ||
const [onBrushHappened, _setOnBrushHappened] = useState(false); | ||
|
||
const onBrushHappenedRef = useRef(onBrushHappened); | ||
|
||
const setOnBrushHappened = (data: boolean) => { | ||
onBrushHappenedRef.current = data; | ||
_setOnBrushHappened(data); | ||
}; | ||
|
||
useEffect(() => { | ||
const popstateHandler = () => { | ||
if (onBrushHappenedRef.current) { | ||
setOnBrushHappened(false); | ||
} else { | ||
setZoomInLevel((currentZoomInLevel) => { | ||
if (currentZoomInLevel > 0) { | ||
return currentZoomInLevel - 1; | ||
} | ||
return 0; | ||
}); | ||
} | ||
}; | ||
|
||
window.addEventListener('popstate', popstateHandler); | ||
return () => window.removeEventListener('popstate', popstateHandler); | ||
}, []); | ||
|
||
const onBrush = ({ xaxis }: any) => { | ||
const { to, from } = xaxis; | ||
const timezone = services.uiSettings?.get('dateFormat:tz'); | ||
const offset = getOffsetInMS(timezone); | ||
const fromTime = moment(from - offset); | ||
const toTime = moment(to - offset); | ||
handleTimeChange(fromTime.toISOString(), toTime.toISOString()); | ||
setOnBrushHappened(true); | ||
setZoomInLevel(zoomInLevel + 1); | ||
}; | ||
|
||
const zoomInfo = { | ||
zoomOutHandler: () => history.goBack(), | ||
showZoomOutBtn: () => zoomInLevel > 0, | ||
}; | ||
|
||
return { | ||
onBrush, | ||
zoomInfo, | ||
}; | ||
} | ||
|
||
const getOffsetInMS = (timezone: string) => { | ||
if (timezone === 'Browser') { | ||
return 0; | ||
} | ||
const offsetInMinutes = moment.tz(timezone).utcOffset(); | ||
const offsetInMS = offsetInMinutes * 1 * 60 * 1000; | ||
return offsetInMS; | ||
}; |
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
69 changes: 69 additions & 0 deletions
69
x-pack/plugins/monitoring/public/application/pages/elasticsearch/elasticsearch_template.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,69 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { includes } from 'lodash'; | ||
import { PageTemplate } from '../page_template'; | ||
import { TabMenuItem, PageTemplateProps } from '../page_template'; | ||
import { ML_SUPPORTED_LICENSES } from '../../../../common/constants'; | ||
|
||
interface ElasticsearchTemplateProps extends PageTemplateProps { | ||
cluster: any; | ||
} | ||
|
||
export const ElasticsearchTemplate: React.FC<ElasticsearchTemplateProps> = ({ | ||
cluster, | ||
...props | ||
}) => { | ||
const tabs: TabMenuItem[] = [ | ||
{ | ||
id: 'overview', | ||
label: i18n.translate('xpack.monitoring.esNavigation.overviewLinkText', { | ||
defaultMessage: 'Overview', | ||
}), | ||
route: '/elasticsearch', | ||
}, | ||
{ | ||
id: 'nodes', | ||
label: i18n.translate('xpack.monitoring.esNavigation.nodesLinkText', { | ||
defaultMessage: 'Nodes', | ||
}), | ||
route: '/elasticsearch/nodes', | ||
}, | ||
{ | ||
id: 'indices', | ||
label: i18n.translate('xpack.monitoring.esNavigation.indicesLinkText', { | ||
defaultMessage: 'Indices', | ||
}), | ||
route: '/elasticsearch/indices', | ||
}, | ||
]; | ||
|
||
if (mlIsSupported(cluster.license)) { | ||
tabs.push({ | ||
id: 'ml', | ||
label: i18n.translate('xpack.monitoring.esNavigation.jobsLinkText', { | ||
defaultMessage: 'Machine learning jobs', | ||
}), | ||
route: '/elasticsearch/ml_jobs', | ||
}); | ||
} | ||
|
||
if (cluster.isCcrEnabled) { | ||
tabs.push({ | ||
id: 'ccr', | ||
label: i18n.translate('xpack.monitoring.esNavigation.ccrLinkText', { | ||
defaultMessage: 'CCR', | ||
}), | ||
route: '/elasticsearch/ccr', | ||
}); | ||
} | ||
|
||
return <PageTemplate {...props} tabs={tabs} product="elasticsearch" />; | ||
}; | ||
|
||
const mlIsSupported = (license: any) => includes(ML_SUPPORTED_LICENSES, license.type); |
97 changes: 97 additions & 0 deletions
97
x-pack/plugins/monitoring/public/application/pages/elasticsearch/overview.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,97 @@ | ||
/* | ||
* 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, { useContext, useState, useCallback } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { find } from 'lodash'; | ||
import { ElasticsearchTemplate } from './elasticsearch_template'; | ||
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public'; | ||
import { GlobalStateContext } from '../../global_state_context'; | ||
import { ElasticsearchOverview } from '../../../components/elasticsearch'; | ||
import { ComponentProps } from '../../route_init'; | ||
import { useCharts } from '../../hooks/use_charts'; | ||
|
||
export const ElasticsearchOverviewPage: React.FC<ComponentProps> = ({ clusters }) => { | ||
const globalState = useContext(GlobalStateContext); | ||
const { zoomInfo, onBrush } = useCharts(); | ||
const { services } = useKibana<{ data: any }>(); | ||
const clusterUuid = globalState.cluster_uuid; | ||
const ccs = globalState.ccs; | ||
const cluster = find(clusters, { | ||
cluster_uuid: clusterUuid, | ||
}); | ||
const [data, setData] = useState(null); | ||
const [showShardActivityHistory, setShowShardActivityHistory] = useState(false); | ||
const toggleShardActivityHistory = () => { | ||
setShowShardActivityHistory(!showShardActivityHistory); | ||
}; | ||
const filterShardActivityData = (shardActivity: any) => { | ||
return shardActivity.filter((row: any) => { | ||
return showShardActivityHistory || row.stage !== 'DONE'; | ||
}); | ||
}; | ||
|
||
const title = i18n.translate('xpack.monitoring.elasticsearch.overview.title', { | ||
defaultMessage: 'Elasticsearch', | ||
}); | ||
|
||
const pageTitle = i18n.translate('xpack.monitoring.elasticsearch.overview.pageTitle', { | ||
defaultMessage: 'Elasticsearch overview', | ||
}); | ||
|
||
const getPageData = useCallback(async () => { | ||
const bounds = services.data?.query.timefilter.timefilter.getBounds(); | ||
const url = `../api/monitoring/v1/clusters/${clusterUuid}/elasticsearch`; | ||
|
||
const response = await services.http?.fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
ccs, | ||
timeRange: { | ||
min: bounds.min.toISOString(), | ||
max: bounds.max.toISOString(), | ||
}, | ||
}), | ||
}); | ||
|
||
setData(response); | ||
}, [ccs, clusterUuid, services.data?.query.timefilter.timefilter, services.http]); | ||
|
||
const renderOverview = (overviewData: any) => { | ||
if (overviewData === null) { | ||
return null; | ||
} | ||
const { clusterStatus, metrics, shardActivity, logs } = overviewData || {}; | ||
const shardActivityData = shardActivity && filterShardActivityData(shardActivity); // no filter on data = null | ||
|
||
return ( | ||
<ElasticsearchOverview | ||
clusterStatus={clusterStatus} | ||
metrics={metrics} | ||
logs={logs} | ||
cluster={cluster} | ||
shardActivity={shardActivityData} | ||
onBrush={onBrush} | ||
showShardActivityHistory={showShardActivityHistory} | ||
toggleShardActivityHistory={toggleShardActivityHistory} | ||
zoomInfo={zoomInfo} | ||
data-test-subj="elasticsearchOverviewPage" | ||
/> | ||
); | ||
}; | ||
|
||
return ( | ||
<ElasticsearchTemplate | ||
title={title} | ||
pageTitle={pageTitle} | ||
getPageData={getPageData} | ||
data-test-subj="elasticsearchOverviewPage" | ||
cluster={cluster} | ||
> | ||
<div data-test-subj="elasticsearchOverviewPage">{renderOverview(data)}</div> | ||
</ElasticsearchTemplate> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pop state event is also fired when the onBrush event is fired, and I couldn't find out why this happens. So I added a flag to know when the onBrush event is fired to prevent changing the zoom level at the wrong moment.
In Angular, this was handled by removing the listener before updating the state and adding it again after some milliseconds, but the same trick didn't work in React.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Might be good to capture this in a code comment.