All files / src/components/show-log show-log.tsx

58.97% Statements 46/78
32.35% Branches 11/34
42.86% Functions 6/14
55.56% Lines 35/63

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                                    29x 29x 29x 29x   29x 29x 29x 29x   29x                                               29x 29x     1x       1x 1x         1x                   1x 1x 1x           1x                 29x 1x   1x 1x     1x     29x       29x 1x 1x         29x           1x                           29x 2x 2x   2x                                                                                                       29x  
/*
 * 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 { AnchorButton, Button, ButtonGroup, Checkbox, Intent } from '@blueprintjs/core';
import axios from 'axios';
import copy from 'copy-to-clipboard';
import React from 'react';
 
import { Loader } from '../../components';
import { AppToaster } from '../../singletons/toaster';
import { UrlBaser } from '../../singletons/url-baser';
import { QueryManager } from '../../utils';
 
import './show-log.scss';
 
function removeFirstPartialLine(log: string): string {
  const lines = log.split('\n');
  if (lines.length > 1) {
    lines.shift();
  }
  return lines.join('\n');
}
 
export interface ShowLogProps {
  endpoint: string;
  downloadFilename?: string;
  tailOffset?: number;
  status?: string;
}
 
export interface ShowLogState {
  logValue?: string;
  loading: boolean;
  error?: string;
  tail: boolean;
}
 
export class ShowLog extends React.PureComponent<ShowLogProps, ShowLogState> {
  static CHECK_INTERVAL = 2500;
 
  private showLogQueryManager: QueryManager<null, string>;
  private log = React.createRef<HTMLTextAreaElement>();
  private interval: number | undefined;
 
  constructor(props: ShowLogProps, context: any) {
    super(props, context);
    this.state = {
      tail: true,
      loading: true,
    };
 
    this.showLogQueryManager = new QueryManager({
      processQuery: async () => {
        const { endpoint, tailOffset } = this.props;
        const resp = await axios.get(endpoint + (tailOffset ? `?offset=-${tailOffset}` : ''));
        const data = resp.data;
 
        let logValue = typeof data === 'string' ? data : JSON.stringify(data, undefined, 2);
        if (tailOffset) logValue = removeFirstPartialLine(logValue);
        return logValue;
      },
      onStateChange: ({ result, loading, error }) => {
        const { tail } = this.state;
        Iif (result && tail) {
          const { current } = this.log;
          if (current) {
            current.scrollTop = current.scrollHeight;
          }
        }
        this.setState({
          logValue: result,
          loading,
          error,
        });
      },
    });
  }
 
  componentDidMount(): void {
    const { status } = this.props;
 
    Eif (status === 'RUNNING') {
      this.addTailer();
    }
 
    this.showLogQueryManager.runQuery(null);
  }
 
  componentWillUnmount(): void {
    this.removeTailer();
  }
 
  addTailer() {
    Iif (this.interval) return;
    this.interval = Number(
      setInterval(() => this.showLogQueryManager.rerunLastQuery(true), ShowLog.CHECK_INTERVAL),
    );
  }
 
  removeTailer() {
    if (!this.interval) return;
    clearInterval(this.interval);
    delete this.interval;
  }
 
  private handleCheckboxChange = () => {
    const { tail } = this.state;
 
    const nextTail = !tail;
    this.setState({
      tail: nextTail,
    });
    if (nextTail) {
      this.addTailer();
    } else {
      this.removeTailer();
    }
  };
 
  render(): JSX.Element {
    const { endpoint, downloadFilename, status } = this.props;
    const { logValue, error, loading } = this.state;
 
    return (
      <div className="show-log">
        <div className="top-actions">
          {status === 'RUNNING' && (
            <Checkbox
              label="Tail log"
              checked={this.state.tail}
              onChange={this.handleCheckboxChange}
            />
          )}
          <ButtonGroup className="right-buttons">
            {downloadFilename && (
              <AnchorButton
                text="Save"
                minimal
                download={downloadFilename}
                href={UrlBaser.base(endpoint)}
              />
            )}
            <Button
              text="Copy"
              minimal
              onClick={() => {
                copy(logValue ? logValue : '', { format: 'text/plain' });
                AppToaster.show({
                  message: 'Log copied to clipboard',
                  intent: Intent.SUCCESS,
                });
              }}
            />
            <Button
              text="View full log"
              minimal
              onClick={() => window.open(UrlBaser.base(endpoint), '_blank')}
            />
          </ButtonGroup>
        </div>
        <div className="main-area">
          {loading ? (
            <Loader loadingText="" loading />
          ) : (
            <textarea
              className="bp3-input"
              readOnly
              value={logValue ? logValue : error}
              ref={this.log}
            />
          )}
        </div>
      </div>
    );
  }
}