vitess.io/vitess@v0.16.2/web/vtadmin/src/util/tablets.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 { invertBy, padStart } from 'lodash-es';
    17  import { topodata, vtadmin as pb } from '../proto/vtadmin';
    18  
    19  /**
    20   * TABLET_TYPES maps numeric tablet types back to human readable strings.
    21   * Note that topodata.TabletType allows duplicate values: specifically,
    22   * both RDONLY (new name) and BATCH (old name) share the same numeric value.
    23   * So, we make the assumption that if there are duplicate keys, we will
    24   * always take the first value.
    25   */
    26  export const TABLET_TYPES = Object.entries(invertBy(topodata.TabletType)).reduce((acc, [k, vs]) => {
    27      acc[k] = vs[0];
    28      return acc;
    29  }, {} as { [k: string]: string });
    30  
    31  /**
    32   * formatAlias formats a tablet.alias object as a single string, The Vitess Way™.
    33   */
    34  export const formatAlias = <A extends topodata.ITabletAlias>(alias: A | null | undefined) =>
    35      alias?.uid ? `${alias.cell}-${alias.uid}` : null;
    36  
    37  /**
    38   * formatAlias formats a tablet.alias object as a single string,
    39   * with the uid left-padded with zeroes.
    40   *
    41   * This function can be removed once https://github.com/vitessio/vitess/issues/8751 is complete.
    42   */
    43  export const formatPaddedAlias = <A extends topodata.ITabletAlias>(alias: A | null | undefined) =>
    44      alias?.cell && alias?.uid ? `${alias.cell}-${padStart(alias.uid.toString(), 10, '0')}` : null;
    45  
    46  export const formatType = (t: pb.Tablet) => t.tablet?.type && TABLET_TYPES[t.tablet?.type];
    47  
    48  export const formatDisplayType = (t: pb.Tablet) => {
    49      const tt = formatType(t);
    50      return tt === 'MASTER' ? 'PRIMARY' : tt;
    51  };
    52  
    53  export const SERVING_STATES = Object.keys(pb.Tablet.ServingState);
    54  
    55  export const formatState = (t: pb.Tablet) => t.state && SERVING_STATES[t.state];
    56  
    57  export const isPrimary = (t: pb.Tablet | undefined) =>
    58      Boolean(t?.tablet?.type) && t?.tablet?.type === topodata.TabletType.PRIMARY;