-
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.
[Stack Monitoring] Migrate indices view to React
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
x-pack/plugins/monitoring/public/application/hooks/use_local_storage.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,52 @@ | ||
/* | ||
* 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 { useState } from 'react'; | ||
import { has } from 'lodash'; | ||
|
||
interface ParsedMonitoringData { | ||
[key: string]: any; | ||
} | ||
export const useLocalStorage = <Value>(key: string, defaultValue: Value): [Value, Function] => { | ||
const localStorageMonitoringKey = 'xpack.monitoring.data'; | ||
const getMonitoringDataStorage = () => { | ||
const monitoringDataStorage = window.localStorage.getItem(localStorageMonitoringKey); | ||
let parsedData: ParsedMonitoringData = {}; | ||
try { | ||
parsedData = (monitoringDataStorage && JSON.parse(monitoringDataStorage)) || {}; | ||
} catch (e) { | ||
console.error('Monitoring UI: error parsing locally stored monitoring data', e); | ||
} | ||
return parsedData; | ||
}; | ||
const saveToStorage = (value: Value) => { | ||
const monitoringDataObj = getMonitoringDataStorage(); | ||
monitoringDataObj[key] = value; | ||
window.localStorage.setItem(localStorageMonitoringKey, JSON.stringify(monitoringDataObj)); | ||
}; | ||
const getFromStorage = (): Value | undefined => { | ||
const monitoringDataObj = getMonitoringDataStorage(); | ||
if (has(monitoringDataObj, key)) { | ||
return monitoringDataObj[key]; | ||
} | ||
}; | ||
|
||
const storedItem = getFromStorage(); | ||
if (!storedItem) { | ||
saveToStorage(defaultValue); | ||
} | ||
const toStore = storedItem || defaultValue; | ||
|
||
const [item, setItem] = useState<Value>(toStore); | ||
|
||
const saveItem = (value: Value) => { | ||
saveToStorage(value); | ||
setItem(value); | ||
}; | ||
|
||
return [item, saveItem]; | ||
}; |
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
99 changes: 99 additions & 0 deletions
99
x-pack/plugins/monitoring/public/application/pages/elasticsearch/indices_page.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,99 @@ | ||
/* | ||
* 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 { ElasticsearchIndices } from '../../../components/elasticsearch'; | ||
import { ComponentProps } from '../../route_init'; | ||
import { SetupModeRenderer } from '../../setup_mode/setup_mode_renderer'; | ||
import { SetupModeContext } from '../../../components/setup_mode/setup_mode_context'; | ||
import { useTable } from '../../hooks/use_table'; | ||
import { useLocalStorage } from '../../hooks/use_local_storage'; | ||
|
||
interface SetupModeProps { | ||
setupMode: any; | ||
flyoutComponent: any; | ||
bottomBarComponent: any; | ||
} | ||
|
||
export const ElasticsearchIndicesPage: React.FC<ComponentProps> = ({ clusters }) => { | ||
const globalState = useContext(GlobalStateContext); | ||
const { services } = useKibana<{ data: any }>(); | ||
const { getPaginationTableProps } = useTable('elasticsearch.indices'); | ||
const clusterUuid = globalState.cluster_uuid; | ||
const cluster = find(clusters, { | ||
cluster_uuid: clusterUuid, | ||
}); | ||
const [data, setData] = useState({} as any); | ||
const [showSystemIndices, setShowSystemIndices] = useLocalStorage<boolean>( | ||
'showSystemIndices', | ||
false | ||
); | ||
|
||
const title = i18n.translate('xpack.monitoring.elasticsearch.indices.routeTitle', { | ||
defaultMessage: 'Elasticsearch - Indices', | ||
}); | ||
|
||
const pageTitle = i18n.translate('xpack.monitoring.elasticsearch.indices.pageTitle', { | ||
defaultMessage: 'Elasticsearch indices', | ||
}); | ||
|
||
const toggleShowSystemIndices = useCallback( | ||
() => setShowSystemIndices(!showSystemIndices), | ||
[showSystemIndices, setShowSystemIndices] | ||
); | ||
|
||
const getPageData = useCallback(async () => { | ||
const bounds = services.data?.query.timefilter.timefilter.getBounds(); | ||
const url = `../api/monitoring/v1/clusters/${clusterUuid}/elasticsearch/indices`; | ||
const response = await services.http?.fetch(url, { | ||
method: 'POST', | ||
query: { | ||
show_system_indices: showSystemIndices, | ||
}, | ||
body: JSON.stringify({ | ||
timeRange: { | ||
min: bounds.min.toISOString(), | ||
max: bounds.max.toISOString(), | ||
}, | ||
}), | ||
}); | ||
setData(response); | ||
}, [showSystemIndices, clusterUuid, services.data?.query.timefilter.timefilter, services.http]); | ||
|
||
return ( | ||
<ElasticsearchTemplate | ||
title={title} | ||
pageTitle={pageTitle} | ||
getPageData={getPageData} | ||
data-test-subj="elasticsearchOverviewPage" | ||
cluster={cluster} | ||
> | ||
<div data-test-subj="elasticsearchNodesListingPage"> | ||
<SetupModeRenderer | ||
render={({ flyoutComponent, bottomBarComponent }: SetupModeProps) => ( | ||
<SetupModeContext.Provider value={{ setupModeSupported: true }}> | ||
{flyoutComponent} | ||
<ElasticsearchIndices | ||
clusterStatus={data.clusterStatus} | ||
indices={data.indices} | ||
alerts={{}} | ||
showSystemIndices={showSystemIndices} | ||
toggleShowSystemIndices={toggleShowSystemIndices} | ||
{...getPaginationTableProps()} | ||
/> | ||
{bottomBarComponent} | ||
</SetupModeContext.Provider> | ||
)} | ||
/> | ||
</div> | ||
</ElasticsearchTemplate> | ||
); | ||
}; |
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