vitess.io/vitess@v0.16.2/web/vtadmin/src/util/keyspaces.ts (about)

     1  /**
     2   * Copyright 2021 The Vitess Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  import { groupBy } from 'lodash-es';
    17  import { vtadmin as pb, vtctldata } from '../proto/vtadmin';
    18  
    19  export enum ShardState {
    20      serving = 'serving',
    21      nonserving = 'nonserving',
    22  }
    23  
    24  export type ShardsByState = { [k in ShardState]: vtctldata.IShard[] };
    25  
    26  export const getShardsByState = <K extends pb.IKeyspace>(keyspace: K | null | undefined): ShardsByState => {
    27      const grouped = groupBy(Object.values(keyspace?.shards || {}), (s) =>
    28          s.shard?.is_primary_serving ? ShardState.serving : ShardState.nonserving
    29      );
    30  
    31      // Add exhaustive enum keys (since groupBy only returns a dictionary), as well as define defaults
    32      return {
    33          [ShardState.serving]: grouped.serving || [],
    34          [ShardState.nonserving]: grouped.nonserving || [],
    35      };
    36  };
    37  
    38  export interface ShardRange {
    39      start: number;
    40      end: number;
    41  }
    42  
    43  /**
    44   * getShardSortRange returns the start and end described by the shard name,
    45   * in base 10. Ranges at the start/end of the shard range are parsed as
    46   * minimum/maximum integer values. Useful for sorting shard names numerically.
    47   */
    48  export const getShardSortRange = (shardName: string): ShardRange => {
    49      if (shardName === '0' || shardName === '-') {
    50          return {
    51              start: Number.MIN_VALUE,
    52              end: Number.MAX_VALUE,
    53          };
    54      }
    55  
    56      const parsed = shardName.split('-');
    57      if (parsed.length !== 2) {
    58          throw Error(`could not parse sortable range from shard ${shardName}`);
    59      }
    60  
    61      // Parse the hexadecimal values into base 10 integers, and normalize the
    62      // start and end of the ranges.
    63      const start = parsed[0] === '' ? Number.MIN_VALUE : parseInt(parsed[0], 16);
    64      const end = parsed[1] === '' ? Number.MAX_VALUE : parseInt(parsed[1], 16);
    65  
    66      if (isNaN(start) || isNaN(end)) {
    67          throw Error(`could not parse sortable range from shard ${shardName}`);
    68      }
    69  
    70      return { start, end };
    71  };