StatusTable.jsx 5.3 KB

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