All files / src/components/supervisor-statistics-table supervisor-statistics-table.tsx

52.63% Statements 30/57
18.75% Branches 3/16
26.32% Functions 5/19
50% Lines 24/48

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                                    9x 9x 9x 9x   9x 9x 9x 9x   9x                                                             9x             1x 1x       1x                   1x 1x                 9x 1x     9x                 9x                                                                                                                 9x 2x 2x 2x                                   9x  
/*
 * 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, ButtonGroup } from '@blueprintjs/core';
import axios from 'axios';
import React from 'react';
import ReactTable, { Column } from 'react-table';
 
import { UrlBaser } from '../../singletons/url-baser';
import { QueryManager } from '../../utils';
import { deepGet } from '../../utils/object-change';
import { Loader } from '../loader/loader';
 
import './supervisor-statistics-table.scss';
 
interface TaskSummary {
  totals: Record<string, StatsEntry>;
  movingAverages: Record<string, Record<string, StatsEntry>>;
}
 
interface StatsEntry {
  processed?: number;
  processedWithError?: number;
  thrownAway?: number;
  unparseable?: number;
  [key: string]: number | undefined;
}
 
interface TableRow {
  taskId: string;
  summary: TaskSummary;
}
 
export interface SupervisorStatisticsTableProps {
  endpoint: string;
  downloadFilename?: string;
}
 
export interface SupervisorStatisticsTableState {
  data?: TableRow[];
  loading: boolean;
  error?: string;
}
 
export class SupervisorStatisticsTable extends React.PureComponent<
  SupervisorStatisticsTableProps,
  SupervisorStatisticsTableState
> {
  private supervisorStatisticsQueryManager: QueryManager<null, TableRow[]>;
 
  constructor(props: SupervisorStatisticsTableProps, context: any) {
    super(props, context);
    this.state = {
      loading: true,
    };
 
    this.supervisorStatisticsQueryManager = new QueryManager({
      processQuery: async () => {
        const { endpoint } = this.props;
        const resp = await axios.get(endpoint);
        const data: Record<string, Record<string, TaskSummary>> = resp.data;
 
        return Object.values(data).flatMap(v =>
          Object.keys(v).map(k => ({ taskId: k, summary: v[k] })),
        );
      },
      onStateChange: ({ result, loading, error }) => {
        this.setState({
          data: result,
          error,
          loading,
        });
      },
    });
  }
 
  componentDidMount(): void {
    this.supervisorStatisticsQueryManager.runQuery(null);
  }
 
  renderCell(data: StatsEntry | undefined) {
    if (!data) {
      return <div>No data found</div>;
    }
    return Object.keys(data)
      .sort()
      .map(key => <div key={key}>{`${key}: ${Number(data[key]).toFixed(1)}`}</div>);
  }
 
  renderTable(error?: string) {
    const { data } = this.state;
 
    let columns: Column<TableRow>[] = [
      {
        Header: 'Task ID',
        id: 'task_id',
        accessor: d => d.taskId,
      },
      {
        Header: 'Totals',
        id: 'total',
        accessor: d => {
          return deepGet(d, 'summary.totals.buildSegments') as StatsEntry;
        },
        Cell: d => {
          return this.renderCell(d.value ? d.value : undefined);
        },
      },
    ];
 
    const movingAveragesBuildSegments = deepGet(
      data as any,
      '0.summary.movingAverages.buildSegments',
    );
    if (movingAveragesBuildSegments) {
      columns = columns.concat(
        Object.keys(movingAveragesBuildSegments)
          .sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))
          .map(
            (interval: string): Column<TableRow> => {
              return {
                Header: interval,
                id: interval,
                accessor: d => {
                  return deepGet(d, `summary.movingAverages.buildSegments.${interval}`);
                },
                Cell: d => {
                  return this.renderCell(d.value ? d.value : null);
                },
              };
            },
          ),
      );
    }
 
    return (
      <ReactTable
        data={this.state.data ? this.state.data : []}
        showPagination={false}
        defaultPageSize={6}
        columns={columns}
        noDataText={error ? error : 'No statistics data found'}
      />
    );
  }
 
  render(): JSX.Element {
    const { endpoint } = this.props;
    const { loading, error } = this.state;
    return (
      <div className="supervisor-statistics-table">
        <div className="top-actions">
          <ButtonGroup className="right-buttons">
            <Button
              text="View raw"
              disabled={loading}
              minimal
              onClick={() => window.open(UrlBaser.base(endpoint), '_blank')}
            />
          </ButtonGroup>
        </div>
        <div className="main-area">
          {loading ? <Loader loadingText="" loading /> : this.renderTable(error)}
        </div>
      </div>
    );
  }
}