ElasticsearchManagement.jsx 6.4 KB

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