RebuildIndex.jsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { createSubscribedElement } from '../../UnstatedUtils';
  5. import AppContainer from '../../../services/AppContainer';
  6. import WebsocketContainer from '../../../services/WebsocketContainer';
  7. import { toastSuccess, toastError } from '../../../util/apiNotification';
  8. import ProgressBar from '../Common/ProgressBar';
  9. class RebuildIndex extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. isNormalized: undefined,
  14. indicesData: null,
  15. aliasesData: null,
  16. isProcessing: false,
  17. isCompleted: false,
  18. total: 0,
  19. current: 0,
  20. skip: 0,
  21. };
  22. this.normalizeIndices = this.normalizeIndices.bind(this);
  23. this.rebuildIndices = this.rebuildIndices.bind(this);
  24. }
  25. async componentWillMount() {
  26. this.retrieveIndicesStatus();
  27. }
  28. componentDidMount() {
  29. this.initWebSockets();
  30. }
  31. initWebSockets() {
  32. const socket = this.props.websocketContainer.getWebSocket();
  33. socket.on('admin:addPageProgress', (data) => {
  34. this.setState({
  35. isProcessing: true,
  36. ...data,
  37. });
  38. });
  39. socket.on('admin:finishAddPage', (data) => {
  40. this.setState({
  41. isProcessing: false,
  42. isCompleted: true,
  43. ...data,
  44. });
  45. });
  46. socket.on('admin:rebuildingFailed', (data) => {
  47. toastError(new Error(data.error), 'Rebuilding Index has failed.');
  48. });
  49. }
  50. async retrieveIndicesStatus() {
  51. const { appContainer } = this.props;
  52. try {
  53. const { info } = await appContainer.apiv3Get('/search/indices');
  54. this.setState({
  55. indicesData: info.indices,
  56. aliasesData: info.aliases,
  57. isNormalized: info.isNormalized,
  58. });
  59. }
  60. catch (e) {
  61. toastError(e);
  62. }
  63. }
  64. async normalizeIndices() {
  65. const { appContainer } = this.props;
  66. try {
  67. await appContainer.apiv3Put('/search/indices', { operation: 'normalize' });
  68. }
  69. catch (e) {
  70. toastError(e);
  71. }
  72. await this.retrieveIndicesStatus();
  73. toastSuccess('Normalizing has succeeded');
  74. }
  75. async rebuildIndices() {
  76. const { appContainer } = this.props;
  77. try {
  78. await appContainer.apiv3Put('/search/indices', { operation: 'rebuild' });
  79. this.setState({ isProcessing: true });
  80. toastSuccess('Rebuilding is requested');
  81. }
  82. catch (e) {
  83. toastError(e);
  84. }
  85. await this.retrieveIndicesStatus();
  86. }
  87. renderIndexInfoPanel(indexName, body = {}, aliases = []) {
  88. const collapseId = `collapse-${indexName}`;
  89. const aliasLabels = aliases.map((aliasName) => {
  90. return (
  91. <span key={`label-${indexName}-${aliasName}`} className="label label-primary mr-2">
  92. <i className="icon-tag"></i> {aliasName}
  93. </span>
  94. );
  95. });
  96. return (
  97. <div className="panel panel-default">
  98. <div className="panel-heading" role="tab">
  99. <h4 className="panel-title">
  100. <a role="button" data-toggle="collapse" data-parent="#accordion" href={`#${collapseId}`} aria-expanded="true" aria-controls={collapseId}>
  101. <i className="fa fa-fw fa-database"></i> {indexName}
  102. </a>
  103. <span className="ml-3">{aliasLabels}</span>
  104. </h4>
  105. </div>
  106. <div id={collapseId} className="panel-collapse collapse" role="tabpanel">
  107. <div className="panel-body">
  108. <pre>
  109. {JSON.stringify(body, null, 2)}
  110. </pre>
  111. </div>
  112. </div>
  113. </div>
  114. );
  115. }
  116. renderIndexInfoPanels() {
  117. const {
  118. indicesData,
  119. aliasesData,
  120. } = this.state;
  121. // data is null
  122. if (indicesData == null) {
  123. return null;
  124. }
  125. /*
  126. "indices": {
  127. "growi": {
  128. ...
  129. }
  130. },
  131. */
  132. const indexNameToDataMap = {};
  133. for (const [indexName, indexData] of Object.entries(indicesData)) {
  134. indexNameToDataMap[indexName] = indexData;
  135. }
  136. // no indices
  137. if (indexNameToDataMap.length === 0) {
  138. return null;
  139. }
  140. /*
  141. "aliases": {
  142. "growi": {
  143. "aliases": {
  144. "growi-alias": {}
  145. }
  146. }
  147. },
  148. */
  149. const indexNameToAliasMap = {};
  150. for (const [indexName, aliasData] of Object.entries(aliasesData)) {
  151. indexNameToAliasMap[indexName] = Object.keys(aliasData.aliases);
  152. }
  153. return (
  154. <div className="row">
  155. { Object.keys(indexNameToDataMap).map((indexName) => {
  156. return (
  157. <div key={`col-${indexName}`} className="col-xs-6">
  158. { this.renderIndexInfoPanel(indexName, indexNameToDataMap[indexName], indexNameToAliasMap[indexName]) }
  159. </div>
  160. );
  161. }) }
  162. </div>
  163. );
  164. }
  165. renderProgressBar() {
  166. const {
  167. total, current, skip, isProcessing, isCompleted,
  168. } = this.state;
  169. const showProgressBar = isProcessing || isCompleted;
  170. if (!showProgressBar) {
  171. return null;
  172. }
  173. const header = isCompleted ? 'Completed' : `Processing.. (${skip} skips)`;
  174. return (
  175. <ProgressBar
  176. header={header}
  177. currentCount={current}
  178. totalCount={total}
  179. />
  180. );
  181. }
  182. renderNormalizeControls() {
  183. const { t } = this.props;
  184. const isEnabled = !this.state.isNormalized && !this.state.isProcessing;
  185. return (
  186. <>
  187. <button
  188. type="submit"
  189. className={`btn btn-outline ${isEnabled ? 'btn-info' : 'btn-default'}`}
  190. onClick={this.normalizeIndices}
  191. disabled={!isEnabled}
  192. >
  193. { t('full_text_search_management.normalize_button') }
  194. </button>
  195. <p className="help-block">
  196. { t('full_text_search_management.normalize_description') }<br />
  197. </p>
  198. </>
  199. );
  200. }
  201. renderRebuildControls() {
  202. const { t } = this.props;
  203. const isEnabled = this.state.isNormalized && !this.state.isProcessing;
  204. return (
  205. <>
  206. { this.renderProgressBar() }
  207. <button
  208. type="submit"
  209. className="btn btn-inverse"
  210. onClick={this.rebuildIndices}
  211. disabled={!isEnabled}
  212. >
  213. { t('full_text_search_management.rebuild_button') }
  214. </button>
  215. <p className="help-block">
  216. { t('full_text_search_management.rebuild_description_1') }<br />
  217. { t('full_text_search_management.rebuild_description_2') }<br />
  218. </p>
  219. </>
  220. );
  221. }
  222. render() {
  223. const { t } = this.props;
  224. const { isNormalized } = this.state;
  225. let statusLabel = <span className="label label-default">――</span>;
  226. if (isNormalized != null) {
  227. statusLabel = isNormalized
  228. ? <span className="label label-info">{ t('full_text_search_management.indices_status_label_normalized') }</span>
  229. : <span className="label label-warning">{ t('full_text_search_management.indices_status_label_unnormalized') }</span>;
  230. }
  231. return (
  232. <>
  233. <div className="row">
  234. <div className="col-xs-12">
  235. <table className="table table-bordered">
  236. <tbody>
  237. <tr>
  238. <th>{ t('full_text_search_management.indices_status') }</th>
  239. <td>{statusLabel}</td>
  240. </tr>
  241. <tr>
  242. <th className="col-sm-4">{ t('full_text_search_management.indices_summary') }</th>
  243. <td className="p-4">
  244. { this.renderIndexInfoPanels() }
  245. </td>
  246. </tr>
  247. </tbody>
  248. </table>
  249. </div>
  250. </div>
  251. <hr />
  252. {/* Controls */}
  253. <div className="row">
  254. <label className="col-xs-3 control-label">{ t('full_text_search_management.normalize') }</label>
  255. <div className="col-xs-6">
  256. { this.renderNormalizeControls() }
  257. </div>
  258. </div>
  259. <hr />
  260. <div className="row">
  261. <label className="col-xs-3 control-label">{ t('full_text_search_management.rebuild') }</label>
  262. <div className="col-xs-6">
  263. { this.renderRebuildControls() }
  264. </div>
  265. </div>
  266. </>
  267. );
  268. }
  269. }
  270. /**
  271. * Wrapper component for using unstated
  272. */
  273. const RebuildIndexWrapper = (props) => {
  274. return createSubscribedElement(RebuildIndex, props, [AppContainer, WebsocketContainer]);
  275. };
  276. RebuildIndex.propTypes = {
  277. t: PropTypes.func.isRequired, // i18next
  278. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  279. websocketContainer: PropTypes.instanceOf(WebsocketContainer).isRequired,
  280. };
  281. export default withTranslation()(RebuildIndexWrapper);