StatusTable.jsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  4. class StatusTable extends React.PureComponent {
  5. renderPreInitializedLabel() {
  6. return <span className="badge rounded-pill badge-default">――</span>;
  7. }
  8. renderConnectionStatusLabels() {
  9. const { t } = this.props;
  10. const {
  11. isErrorOccuredOnSearchService,
  12. isConnected, isConfigured,
  13. } = this.props;
  14. const errorOccuredLabel = isErrorOccuredOnSearchService
  15. ? <span className="badge rounded-pill badge-danger ml-2">{ t('full_text_search_management.connection_status_label_erroroccured') }</span>
  16. : null;
  17. let connectionStatusLabel = null;
  18. if (!isConfigured) {
  19. connectionStatusLabel = <span className="badge rounded-pill badge-default">{ t('full_text_search_management.connection_status_label_unconfigured') }</span>;
  20. }
  21. else {
  22. connectionStatusLabel = isConnected
  23. // eslint-disable-next-line max-len
  24. ? <span data-testid="connection-status-badge-connected" className="badge rounded-pill badge-success">{ t('full_text_search_management.connection_status_label_connected') }</span>
  25. : <span className="badge rounded-pill badge-danger">{ t('full_text_search_management.connection_status_label_disconnected') }</span>;
  26. }
  27. return (
  28. <>
  29. {connectionStatusLabel}{errorOccuredLabel}
  30. </>
  31. );
  32. }
  33. renderIndicesStatusLabel() {
  34. const { t, isNormalized } = this.props;
  35. return isNormalized
  36. ? <span className="badge rounded-pill badge-info">{ t('full_text_search_management.indices_status_label_normalized') }</span>
  37. : <span className="badge rounded-pill badge-warning">{ t('full_text_search_management.indices_status_label_unnormalized') }</span>;
  38. }
  39. renderIndexInfoPanel(indexName, body = {}, aliases = []) {
  40. const collapseId = `collapse-${indexName}`;
  41. const aliasLabels = aliases.map((aliasName) => {
  42. return (
  43. <span key={`badge-${indexName}-${aliasName}`} className="badge rounded-pill badge-primary mr-2">
  44. <i className="icon-tag"></i> {aliasName}
  45. </span>
  46. );
  47. });
  48. return (
  49. <div className="card">
  50. <div className="card-header">
  51. <a role="button" className="text-nowrap mr-2" data-toggle="collapse" href={`#${collapseId}`} aria-expanded="true" aria-controls={collapseId}>
  52. <i className="fa fa-fw fa-database"></i> {indexName}
  53. </a>
  54. <span className="ml-md-3">{aliasLabels}</span>
  55. </div>
  56. <div id={collapseId} className="collapse">
  57. <div className="card-body">
  58. <pre>
  59. {JSON.stringify(body, null, 2)}
  60. </pre>
  61. </div>
  62. </div>
  63. </div>
  64. );
  65. }
  66. renderIndexInfoPanels() {
  67. const {
  68. indicesData,
  69. aliasesData,
  70. } = this.props;
  71. // data is null
  72. if (indicesData == null) {
  73. return null;
  74. }
  75. /*
  76. "indices": {
  77. "growi": {
  78. ...
  79. }
  80. },
  81. */
  82. const indexNameToDataMap = {};
  83. for (const [indexName, indexData] of Object.entries(indicesData)) {
  84. indexNameToDataMap[indexName] = indexData;
  85. }
  86. // no indices
  87. if (indexNameToDataMap.length === 0) {
  88. return null;
  89. }
  90. /*
  91. "aliases": {
  92. "growi": {
  93. "aliases": {
  94. "growi-alias": {}
  95. }
  96. }
  97. },
  98. */
  99. const indexNameToAliasMap = {};
  100. for (const [indexName, aliasData] of Object.entries(aliasesData)) {
  101. indexNameToAliasMap[indexName] = Object.keys(aliasData.aliases);
  102. }
  103. return (
  104. <div className="row">
  105. { Object.keys(indexNameToDataMap).map((indexName) => {
  106. return (
  107. <div key={`col-${indexName}`} className="col-md-6">
  108. { this.renderIndexInfoPanel(indexName, indexNameToDataMap[indexName], indexNameToAliasMap[indexName]) }
  109. </div>
  110. );
  111. }) }
  112. </div>
  113. );
  114. }
  115. render() {
  116. const { t } = this.props;
  117. const {
  118. isInitialized,
  119. } = this.props;
  120. return (
  121. <table className="table table-bordered">
  122. <tbody>
  123. <tr>
  124. <th className="w-25">{t('full_text_search_management.connection_status')}</th>
  125. <td className="w-75">{ isInitialized ? this.renderConnectionStatusLabels() : this.renderPreInitializedLabel() }</td>
  126. </tr>
  127. <tr>
  128. <th className="w-25">{t('full_text_search_management.indices_status')}</th>
  129. <td className="w-75">{ isInitialized ? this.renderIndicesStatusLabel() : this.renderPreInitializedLabel() }</td>
  130. </tr>
  131. <tr>
  132. <th className="w-25">{t('full_text_search_management.indices_summary')}</th>
  133. <td className="p-4 w-75">{ isInitialized && this.renderIndexInfoPanels() }</td>
  134. </tr>
  135. </tbody>
  136. </table>
  137. );
  138. }
  139. }
  140. const StatusTableWrapperFC = (props) => {
  141. const { t } = useTranslation('admin');
  142. return <StatusTable t={t} {...props} />;
  143. };
  144. StatusTable.propTypes = {
  145. t: PropTypes.func.isRequired, // i18next
  146. isInitialized: PropTypes.bool,
  147. isErrorOccuredOnSearchService: PropTypes.bool,
  148. isConnected: PropTypes.bool,
  149. isConfigured: PropTypes.bool,
  150. isNormalized: PropTypes.bool,
  151. indicesData: PropTypes.object,
  152. aliasesData: PropTypes.object,
  153. };
  154. export default StatusTableWrapperFC;