StatusTable.jsx 5.3 KB

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