ElasticsearchManagement.jsx 6.4 KB

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