Skip to content

Commit

Permalink
[Stack Monitoring] Migrate indices view to React
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Sep 28, 2021
1 parent 9e95786 commit 8cbc453
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
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];
};
8 changes: 8 additions & 0 deletions x-pack/plugins/monitoring/public/application/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ElasticsearchOverviewPage } from './pages/elasticsearch/overview';
import { BeatsOverviewPage } from './pages/beats/overview';
import { CODE_PATH_ELASTICSEARCH, CODE_PATH_BEATS } from '../../common/constants';
import { ElasticsearchNodesPage } from './pages/elasticsearch/nodes_page';
import { ElasticsearchIndicesPage } from './pages/elasticsearch/indices_page';
import { MonitoringTimeContainer } from './hooks/use_monitoring_time';
import { BreadcrumbContainer } from './hooks/use_breadcrumbs';

Expand Down Expand Up @@ -79,6 +80,13 @@ const MonitoringApp: React.FC<{
/>

{/* ElasticSearch Views */}
<RouteInit
path="/elasticsearch/indices"
component={ElasticsearchIndicesPage}
codePaths={[CODE_PATH_ELASTICSEARCH]}
fetchAllClusters={false}
/>

<RouteInit
path="/elasticsearch/nodes"
component={ElasticsearchNodesPage}
Expand Down
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>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export const ElasticsearchOverview: FunctionComponent<Props>;
export const ElasticsearchNodes: FunctionComponent<Props>;
export const ElasticsearchIndices: FunctionComponent<Props>;

0 comments on commit 8cbc453

Please sign in to comment.