All files / src/views/home-view/segments-card segments-card.tsx

60.42% Statements 29/48
33.33% Branches 5/15
50% Functions 5/10
62.5% Lines 25/40

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                                    2x 2x 2x 2x   2x   2x 2x                         2x       1x 1x           1x                                                         1x 1x                   2x 1x   1x     2x       2x 2x 2x 2x 2x 2x     2x                               2x  
/*
 * 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 { IconNames } from '@blueprintjs/icons';
import axios from 'axios';
import { sum } from 'd3-array';
import React from 'react';
 
import { pluralIfNeeded, queryDruidSql, QueryManager } from '../../../utils';
import { Capabilities } from '../../../utils/capabilities';
import { deepGet } from '../../../utils/object-change';
import { HomeViewCard } from '../home-view-card/home-view-card';
 
export interface SegmentsCardProps {
  capabilities: Capabilities;
}
 
export interface SegmentsCardState {
  segmentCountLoading: boolean;
  segmentCount: number;
  unavailableSegmentCount: number;
  segmentCountError?: string;
}
 
export class SegmentsCard extends React.PureComponent<SegmentsCardProps, SegmentsCardState> {
  private segmentQueryManager: QueryManager<Capabilities, any>;
 
  constructor(props: SegmentsCardProps, context: any) {
    super(props, context);
    this.state = {
      segmentCountLoading: false,
      segmentCount: 0,
      unavailableSegmentCount: 0,
    };
 
    this.segmentQueryManager = new QueryManager({
      processQuery: async capabilities => {
        if (capabilities.hasSql()) {
          const segments = await queryDruidSql({
            query: `SELECT
  COUNT(*) as "count",
  COUNT(*) FILTER (WHERE is_available = 0) as "unavailable"
FROM sys.segments`,
          });
          return segments.length === 1 ? segments[0] : null;
        } else if (capabilities.hasCoordinatorAccess()) {
          const loadstatusResp = await axios.get('/druid/coordinator/v1/loadstatus?simple');
          const loadstatus = loadstatusResp.data;
          const unavailableSegmentNum = sum(Object.keys(loadstatus), key => loadstatus[key]);
 
          const datasourcesMetaResp = await axios.get('/druid/coordinator/v1/datasources?simple');
          const datasourcesMeta = datasourcesMetaResp.data;
          const availableSegmentNum = sum(datasourcesMeta, (curr: any) =>
            deepGet(curr, 'properties.segments.count'),
          );
 
          return {
            count: availableSegmentNum + unavailableSegmentNum,
            unavailable: unavailableSegmentNum,
          };
        } else {
          throw new Error(`must have SQL or coordinator access`);
        }
      },
      onStateChange: ({ result, loading, error }) => {
        this.setState({
          segmentCountLoading: loading,
          segmentCount: result ? result.count : 0,
          unavailableSegmentCount: result ? result.unavailable : 0,
          segmentCountError: error,
        });
      },
    });
  }
 
  componentDidMount(): void {
    const { capabilities } = this.props;
 
    this.segmentQueryManager.runQuery(capabilities);
  }
 
  componentWillUnmount(): void {
    this.segmentQueryManager.terminate();
  }
 
  render(): JSX.Element {
    const {
      segmentCountLoading,
      segmentCountError,
      segmentCount,
      unavailableSegmentCount,
    } = this.state;
 
    return (
      <HomeViewCard
        className="segments-card"
        href={'#segments'}
        icon={IconNames.STACKED_CHART}
        title={'Segments'}
        loading={segmentCountLoading}
        error={segmentCountError}
      >
        <p>{pluralIfNeeded(segmentCount, 'segment')}</p>
        {Boolean(unavailableSegmentCount) && (
          <p>{pluralIfNeeded(unavailableSegmentCount, 'unavailable segment')}</p>
        )}
      </HomeViewCard>
    );
  }
}