ElasticsearchManagement.jsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'next-i18next';
  4. import AdminSocketIoContainer from '~/client/services/AdminSocketIoContainer';
  5. import AppContainer from '~/client/services/AppContainer';
  6. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  7. import { apiv3Get, apiv3Post, apiv3Put } from '~/client/util/apiv3-client';
  8. import { withUnstatedContainers } from '../../UnstatedUtils';
  9. import NormalizeIndicesControls from './NormalizeIndicesControls';
  10. import RebuildIndexControls from './RebuildIndexControls';
  11. import ReconnectControls from './ReconnectControls';
  12. import StatusTable from './StatusTable';
  13. class ElasticsearchManagement extends React.Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. isInitialized: false,
  18. isConnected: false,
  19. isConfigured: false,
  20. isReconnectingProcessing: false,
  21. isRebuildingProcessing: false,
  22. isRebuildingCompleted: false,
  23. isNormalized: null,
  24. indicesData: null,
  25. aliasesData: null,
  26. };
  27. this.reconnect = this.reconnect.bind(this);
  28. this.normalizeIndices = this.normalizeIndices.bind(this);
  29. this.rebuildIndices = this.rebuildIndices.bind(this);
  30. }
  31. async UNSAFE_UNSAFE_componentWillMount() {
  32. this.retrieveIndicesStatus();
  33. }
  34. componentDidMount() {
  35. this.initWebSockets();
  36. }
  37. initWebSockets() {
  38. const socket = this.props.adminSocketIoContainer.getSocket();
  39. socket.on('addPageProgress', (data) => {
  40. this.setState({
  41. isRebuildingProcessing: true,
  42. });
  43. });
  44. socket.on('finishAddPage', async(data) => {
  45. await this.retrieveIndicesStatus();
  46. this.setState({
  47. isRebuildingProcessing: false,
  48. isRebuildingCompleted: true,
  49. });
  50. });
  51. socket.on('rebuildingFailed', (data) => {
  52. toastError(new Error(data.error), 'Rebuilding Index has failed.');
  53. });
  54. }
  55. async retrieveIndicesStatus() {
  56. const { appContainer } = this.props;
  57. try {
  58. const { data } = await apiv3Get('/search/indices');
  59. const { info } = data;
  60. this.setState({
  61. isConnected: true,
  62. isConfigured: true,
  63. indicesData: info.indices,
  64. aliasesData: info.aliases,
  65. isNormalized: info.isNormalized,
  66. });
  67. }
  68. catch (errors) {
  69. this.setState({ isConnected: false });
  70. // evaluate whether configured or not
  71. for (const error of errors) {
  72. if (error.code === 'search-service-unconfigured') {
  73. this.setState({ isConfigured: false });
  74. }
  75. }
  76. toastError(errors);
  77. }
  78. finally {
  79. this.setState({ isInitialized: true });
  80. }
  81. }
  82. async reconnect() {
  83. const { appContainer } = this.props;
  84. this.setState({ isReconnectingProcessing: true });
  85. try {
  86. await apiv3Post('/search/connection');
  87. }
  88. catch (e) {
  89. toastError(e);
  90. return;
  91. }
  92. // reload
  93. window.location.reload();
  94. }
  95. async normalizeIndices() {
  96. const { appContainer } = this.props;
  97. try {
  98. await apiv3Put('/search/indices', { operation: 'normalize' });
  99. }
  100. catch (e) {
  101. toastError(e);
  102. }
  103. await this.retrieveIndicesStatus();
  104. toastSuccess('Normalizing has succeeded');
  105. }
  106. async rebuildIndices() {
  107. const { appContainer } = this.props;
  108. this.setState({ isRebuildingProcessing: true });
  109. try {
  110. await apiv3Put('/search/indices', { operation: 'rebuild' });
  111. toastSuccess('Rebuilding is requested');
  112. }
  113. catch (e) {
  114. toastError(e);
  115. }
  116. await this.retrieveIndicesStatus();
  117. }
  118. render() {
  119. const { t, appContainer } = this.props;
  120. const {
  121. isInitialized,
  122. isConnected, isConfigured, isReconnectingProcessing, isRebuildingProcessing, isRebuildingCompleted,
  123. isNormalized, indicesData, aliasesData,
  124. } = this.state;
  125. const isErrorOccuredOnSearchService = !appContainer.config.isSearchServiceReachable;
  126. const isReconnectBtnEnabled = !isReconnectingProcessing && (!isInitialized || !isConnected || isErrorOccuredOnSearchService);
  127. return (
  128. <>
  129. <div className="row">
  130. <div className="col-md-12">
  131. <StatusTable
  132. isInitialized={isInitialized}
  133. isErrorOccuredOnSearchService={isErrorOccuredOnSearchService}
  134. isConnected={isConnected}
  135. isConfigured={isConfigured}
  136. isNormalized={isNormalized}
  137. indicesData={indicesData}
  138. aliasesData={aliasesData}
  139. />
  140. </div>
  141. </div>
  142. <hr />
  143. {/* Controls */}
  144. <div className="row">
  145. <label className="col-md-3 col-form-label text-left text-md-right">{ t('full_text_search_management.reconnect') }</label>
  146. <div className="col-md-6">
  147. <ReconnectControls
  148. isEnabled={isReconnectBtnEnabled}
  149. isProcessing={isReconnectingProcessing}
  150. onReconnectingRequested={this.reconnect}
  151. />
  152. </div>
  153. </div>
  154. <hr />
  155. <div className="row">
  156. <label className="col-md-3 col-form-label text-left text-md-right">{ t('full_text_search_management.normalize') }</label>
  157. <div className="col-md-6">
  158. <NormalizeIndicesControls
  159. isRebuildingProcessing={isRebuildingProcessing}
  160. isRebuildingCompleted={isRebuildingCompleted}
  161. isNormalized={isNormalized}
  162. onNormalizingRequested={this.normalizeIndices}
  163. />
  164. </div>
  165. </div>
  166. <hr />
  167. <div className="row">
  168. <label className="col-md-3 col-form-label text-left text-md-right">{ t('full_text_search_management.rebuild') }</label>
  169. <div className="col-md-6">
  170. <RebuildIndexControls
  171. isRebuildingProcessing={isRebuildingProcessing}
  172. isRebuildingCompleted={isRebuildingCompleted}
  173. isNormalized={isNormalized}
  174. onRebuildingRequested={this.rebuildIndices}
  175. />
  176. </div>
  177. </div>
  178. </>
  179. );
  180. }
  181. }
  182. const ElasticsearchManagementWrapperFC = (props) => {
  183. const { t } = useTranslation();
  184. return <ElasticsearchManagement t={t} {...props} />;
  185. };
  186. /**
  187. * Wrapper component for using unstated
  188. */
  189. const ElasticsearchManagementWrapper = withUnstatedContainers(ElasticsearchManagementWrapperFC, [AppContainer, AdminSocketIoContainer]);
  190. ElasticsearchManagement.propTypes = {
  191. t: PropTypes.func.isRequired, // i18next
  192. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  193. adminSocketIoContainer: PropTypes.instanceOf(AdminSocketIoContainer).isRequired,
  194. };
  195. export default ElasticsearchManagementWrapper;