{"size": 3981, "ext": "js", "max_stars_repo_path": "src/components/ClusterOverview/Health/Health.js", "max_stars_repo_name": "pcbailey/web-ui-components", "max_stars_repo_stars_event_min_datetime": "2018-09-20T09:40:46.000Z", "max_stars_repo_stars_event_max_datetime": "2020-04-14T03:41:48.000Z", "max_issues_repo_path": "src/components/ClusterOverview/Health/Health.js", "max_issues_repo_name": "pcbailey/web-ui-components", "max_issues_repo_issues_event_min_datetime": "2018-09-13T13:30:56.000Z", "max_issues_repo_issues_event_max_datetime": "2020-04-26T03:06:39.000Z", "max_forks_repo_path": "src/components/ClusterOverview/Health/Health.js", "max_forks_repo_name": "pcbailey/web-ui-components", "max_forks_repo_forks_event_min_datetime": "2018-09-18T14:36:38.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-03T09:25:53.000Z"}
{"max_stars_count": 6, "max_issues_count": 510, "max_forks_count": 21, "avg_line_length": 30.6230769231, "max_line_length": 116, "alphanum_fraction": 0.6729464959}
import React from 'react';
import PropTypes from 'prop-types';

import {
  DashboardCard,
  DashboardCardBody,
  DashboardCardHeader,
  DashboardCardTitle,
  DashboardCardTitleSeeAll,
} from '../../Dashboard/DashboardCard';
import { ClusterOverviewContext } from '../ClusterOverviewContext';
import { InlineLoading } from '../../Loading';
import { SubsystemHealth } from '../../Dashboard/SubsystemHealth';
import { HealthItem, OK_STATE, ERROR_STATE, WARNING_STATE, LOADING_STATE } from '../../Dashboard/Health/HealthItem';
import { HealthBody } from '../../Dashboard/Health/HealthBody';
import { getK8sHealthState, getKubevirtHealthState, getOCSHealthState } from '../../Dashboard/Health/utils';

const getClusterHealth = subsystemStates => {
  let healthState = { state: OK_STATE, message: 'Cluster is healthy' };
  const sortedStates = {
    errorStates: [],
    warningStates: [],
    loadingStates: [],
  };

  subsystemStates.forEach(state => {
    switch (state.state) {
      case ERROR_STATE:
        sortedStates.errorStates.push(state);
        break;
      case WARNING_STATE:
        sortedStates.warningStates.push(state);
        break;
      case LOADING_STATE:
        sortedStates.loadingStates.push(state);
        break;
      default:
        break;
    }
  });

  if (sortedStates.loadingStates.length > 0) {
    healthState = { state: LOADING_STATE };
  } else if (sortedStates.errorStates.length > 0) {
    healthState =
      sortedStates.errorStates.length === 1
        ? sortedStates.errorStates[0]
        : { state: ERROR_STATE, message: 'Multiple errors', details: 'Cluster health is degraded' };
  } else if (sortedStates.warningStates.length > 0) {
    healthState =
      sortedStates.warningStates.length === 1
        ? sortedStates.warningStates[0]
        : { state: WARNING_STATE, message: 'Multiple warnings', details: 'Cluster health is degraded' };
  }

  return healthState;
};

export class Health extends React.PureComponent {
  render() {
    const { k8sHealth, kubevirtHealth, cephHealth, LoadingComponent } = this.props;
    const k8sHealthState = getK8sHealthState(k8sHealth);
    const kubevirtHealthState = getKubevirtHealthState(kubevirtHealth);
    const cephHealthState = getOCSHealthState(cephHealth);

    const healthState = getClusterHealth([k8sHealthState, kubevirtHealthState, cephHealthState]);

    return (
      <DashboardCard>
        <DashboardCardHeader>
          <DashboardCardTitle>Cluster Health</DashboardCardTitle>
          <DashboardCardTitleSeeAll title="Subsystem health">
            <SubsystemHealth
              k8sHealth={k8sHealthState}
              kubevirtHealth={kubevirtHealthState}
              cephHealth={cephHealthState}
              LoadingComponent={LoadingComponent}
            />
          </DashboardCardTitleSeeAll>
        </DashboardCardHeader>
        <DashboardCardBody>
          <HealthBody>
            <HealthItem
              state={healthState.state}
              message={healthState.message}
              details={healthState.details}
              LoadingComponent={LoadingComponent}
            />
          </HealthBody>
        </DashboardCardBody>
      </DashboardCard>
    );
  }
}

Health.defaultProps = {
  k8sHealth: null,
  kubevirtHealth: null,
  cephHealth: null,
  LoadingComponent: InlineLoading,
};

Health.propTypes = {
  k8sHealth: PropTypes.object,
  kubevirtHealth: PropTypes.object,
  cephHealth: PropTypes.object,
  LoadingComponent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
};

export const HealthConnected = () => (
  <ClusterOverviewContext.Consumer>
    {props => (
      <Health
        k8sHealth={props.k8sHealth}
        kubevirtHealth={props.kubevirtHealth}
        cephHealth={props.cephHealth}
        LoadingComponent={props.LoadingComponent}
      />
    )}
  </ClusterOverviewContext.Consumer>
);

HealthConnected.propTypes = {
  ...Health.propTypes,
};

HealthConnected.defaultProps = {
  ...Health.defaultProps,
};
