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

63.79% Statements 37/58
41.94% Branches 13/31
46.15% Functions 6/13
66.67% Lines 32/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                                    2x 2x 2x   2x   2x                                     2x 2x           8x       8x             1x 1x                       1x                                                   1x 1x                               2x 1x   1x     2x       2x 2x 2x 2x 2x 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 React from 'react';
 
import { compact, lookupBy, pluralIfNeeded, queryDruidSql, QueryManager } from '../../../utils';
import { Capabilities } from '../../../utils/capabilities';
import { HomeViewCard } from '../home-view-card/home-view-card';
 
export interface ServicesCardProps {
  capabilities: Capabilities;
}
 
export interface ServicesCardState {
  serviceCountLoading: boolean;
  coordinatorCount: number;
  overlordCount: number;
  routerCount: number;
  brokerCount: number;
  historicalCount: number;
  middleManagerCount: number;
  peonCount: number;
  indexerCount: number;
  serviceCountError?: string;
}
 
export class ServicesCard extends React.PureComponent<ServicesCardProps, ServicesCardState> {
  static renderPluralIfNeededPair(
    count1: number,
    singular1: string,
    count2: number,
    singular2: string,
  ): JSX.Element | undefined {
    const text = compact([
      count1 ? pluralIfNeeded(count1, singular1) : undefined,
      count2 ? pluralIfNeeded(count2, singular2) : undefined,
    ]).join(', ');
    Eif (!text) return;
    return <p>{text}</p>;
  }
 
  private serviceQueryManager: QueryManager<Capabilities, any>;
 
  constructor(props: ServicesCardProps, context: any) {
    super(props, context);
    this.state = {
      serviceCountLoading: false,
      coordinatorCount: 0,
      overlordCount: 0,
      routerCount: 0,
      brokerCount: 0,
      historicalCount: 0,
      middleManagerCount: 0,
      peonCount: 0,
      indexerCount: 0,
    };
 
    this.serviceQueryManager = new QueryManager({
      processQuery: async capabilities => {
        if (capabilities.hasSql()) {
          const serviceCountsFromQuery: {
            service_type: string;
            count: number;
          }[] = await queryDruidSql({
            query: `SELECT server_type AS "service_type", COUNT(*) as "count" FROM sys.servers GROUP BY 1`,
          });
          return lookupBy(serviceCountsFromQuery, x => x.service_type, x => x.count);
        } else if (capabilities.hasCoordinatorAccess()) {
          const services = (await axios.get('/druid/coordinator/v1/servers?simple')).data;
 
          const middleManager = capabilities.hasOverlordAccess()
            ? (await axios.get('/druid/indexer/v1/workers')).data
            : [];
 
          return {
            historical: services.filter((s: any) => s.type === 'historical').length,
            middle_manager: middleManager.length,
            peon: services.filter((s: any) => s.type === 'indexer-executor').length,
          };
        } else {
          throw new Error(`must have SQL or coordinator access`);
        }
      },
      onStateChange: ({ result, loading, error }) => {
        this.setState({
          serviceCountLoading: loading,
          coordinatorCount: result ? result.coordinator : 0,
          overlordCount: result ? result.overlord : 0,
          routerCount: result ? result.router : 0,
          brokerCount: result ? result.broker : 0,
          historicalCount: result ? result.historical : 0,
          middleManagerCount: result ? result.middle_manager : 0,
          peonCount: result ? result.peon : 0,
          indexerCount: result ? result.indexer : 0,
          serviceCountError: error,
        });
      },
    });
  }
 
  componentDidMount(): void {
    const { capabilities } = this.props;
 
    this.serviceQueryManager.runQuery(capabilities);
  }
 
  componentWillUnmount(): void {
    this.serviceQueryManager.terminate();
  }
 
  render(): JSX.Element {
    const {
      serviceCountLoading,
      coordinatorCount,
      overlordCount,
      routerCount,
      brokerCount,
      historicalCount,
      middleManagerCount,
      peonCount,
      indexerCount,
      serviceCountError,
    } = this.state;
    return (
      <HomeViewCard
        className="services-card"
        href={'#services'}
        icon={IconNames.DATABASE}
        title={'Services'}
        loading={serviceCountLoading}
        error={serviceCountError}
      >
        {ServicesCard.renderPluralIfNeededPair(
          overlordCount,
          'overlord',
          coordinatorCount,
          'coordinator',
        )}
        {ServicesCard.renderPluralIfNeededPair(routerCount, 'router', brokerCount, 'broker')}
        {ServicesCard.renderPluralIfNeededPair(
          historicalCount,
          'historical',
          middleManagerCount,
          'middle manager',
        )}
        {ServicesCard.renderPluralIfNeededPair(peonCount, 'peon', indexerCount, 'indexer')}
      </HomeViewCard>
    );
  }
}