ElasticsearchManagement.jsx 6.3 KB

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