All files / src/dialogs/doctor-dialog doctor-dialog.tsx

40.58% Statements 28/69
9.09% Branches 3/33
35.71% Functions 5/14
42.31% Lines 22/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                    30x 30x 30x   30x   30x   30x                                   30x 1x     1x 1x     30x 1x     30x       30x                                                                                                                   30x 1x   1x                                                                               1x                               30x 1x   1x                                     30x  
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { Button, Callout, Classes, Dialog, Intent } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import React from 'react';
 
import { delay, pluralIfNeeded } from '../../utils';
 
import { DOCTOR_CHECKS } from './doctor-checks';
 
import './doctor-dialog.scss';
 
interface Diagnosis {
  type: 'suggestion' | 'issue';
  check: string;
  message: string;
}
 
export interface DoctorDialogProps {
  onClose: () => void;
}
 
export interface DoctorDialogState {
  currentCheckIndex?: number;
  diagnoses?: Diagnosis[];
  earlyTermination?: string;
}
 
export class DoctorDialog extends React.PureComponent<DoctorDialogProps, DoctorDialogState> {
  private mounted = false;
 
  constructor(props: DoctorDialogProps, context: any) {
    super(props, context);
    this.state = {};
  }
 
  componentDidMount(): void {
    this.mounted = true;
  }
 
  componentWillUnmount(): void {
    this.mounted = false;
  }
 
  async doChecks() {
    this.setState({ currentCheckIndex: 0, diagnoses: [] });
 
    const addToDiagnoses = (diagnosis: Diagnosis) => {
      if (!this.mounted) return;
      this.setState(oldState => ({
        diagnoses: (oldState.diagnoses || []).concat(diagnosis),
      }));
    };
 
    for (let i = 0; i < DOCTOR_CHECKS.length; i++) {
      if (!this.mounted) return;
      this.setState({ currentCheckIndex: i });
      const check = DOCTOR_CHECKS[i];
      let terminateChecks = false;
 
      try {
        await Promise.all([
          await delay(450), // Make sure that a test takes at least this long so that the user can read the test name in the GUI,
          check.check({
            addSuggestion: (message: string) => {
              addToDiagnoses({
                type: 'suggestion',
                check: check.name,
                message,
              });
            },
            addIssue: (message: string) => {
              addToDiagnoses({
                type: 'issue',
                check: check.name,
                message,
              });
            },
            terminateChecks: () => {
              if (!this.mounted) return;
              this.setState({
                earlyTermination: `The check "${check.name}" early terminated the check suite as it has encountered a condition that would make the rest of the tests meaningless.`,
              });
              terminateChecks = true;
            },
          }),
        ]);
      } catch (e) {
        addToDiagnoses({
          type: 'issue',
          check: check.name,
          message: `The check "${check.name}" encountered an unhandled exception. Please report this issue. Message: ${e.message}`,
        });
      }
 
      if (terminateChecks) break;
    }
 
    if (!this.mounted) return;
    this.setState({ currentCheckIndex: undefined });
  }
 
  renderContent() {
    const { diagnoses, currentCheckIndex, earlyTermination } = this.state;
 
    Iif (diagnoses) {
      let note: string;
      if (typeof currentCheckIndex === 'number') {
        note = `Running check ${currentCheckIndex + 1}/${DOCTOR_CHECKS.length}: ${
          DOCTOR_CHECKS[currentCheckIndex].name
        }`;
      } else if (earlyTermination) {
        note = `Checks stopped abruptly`;
      } else {
        note = `All ${pluralIfNeeded(DOCTOR_CHECKS.length, 'check')} completed`;
      }
 
      return (
        <>
          <Callout className="diagnosis">{note}</Callout>
          {diagnoses.map((diagnosis, i) => {
            return (
              <Callout
                key={i}
                className="diagnosis"
                icon={diagnosis.type === 'suggestion' ? IconNames.FLAG : IconNames.WARNING_SIGN}
                intent={diagnosis.type === 'suggestion' ? Intent.NONE : Intent.WARNING}
              >
                {diagnosis.message}
              </Callout>
            );
          })}
          {earlyTermination && (
            <Callout className="diagnosis" intent={Intent.DANGER}>
              {earlyTermination}
            </Callout>
          )}
          {!earlyTermination && currentCheckIndex == null && diagnoses.length === 0 && (
            <Callout className="diagnosis" intent={Intent.SUCCESS}>
              No issues detected
            </Callout>
          )}
        </>
      );
    } else {
      return (
        <div className="analyze-bar">
          <Callout className="diagnosis">
            Automated checks to troubleshoot issues with the cluster.
          </Callout>
          <Button
            text="Analyze Druid cluster"
            intent={Intent.PRIMARY}
            fill
            onClick={() => this.doChecks()}
          />
        </div>
      );
    }
  }
 
  render(): JSX.Element {
    const { onClose } = this.props;
 
    return (
      <Dialog
        className="doctor-dialog"
        icon={IconNames.PULSE}
        onClose={onClose}
        title="Druid Doctor"
        isOpen
        canEscapeKeyClose={false}
        canOutsideClickClose={false}
      >
        <div className={Classes.DIALOG_BODY}>{this.renderContent()}</div>
        <div className={Classes.DIALOG_FOOTER}>
          <div className={Classes.DIALOG_FOOTER_ACTIONS}>
            <Button onClick={onClose}>Close</Button>
          </div>
        </div>
      </Dialog>
    );
  }
}