RebuildIndex.jsx 8.3 KB

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