vitess.io/vitess@v0.16.2/go/vt/vtadmin/vtadminproto/tablet.go (about) 1 /* 2 Copyright 2020 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 17 package vtadminproto 18 19 import ( 20 "vitess.io/vitess/go/vt/topo/topoproto" 21 22 vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" 23 ) 24 25 // Tablets is a list of Tablet protobuf objects. 26 type Tablets []*vtadminpb.Tablet 27 28 // AliasStringList returns a list of TabletAlias strings for each tablet in the 29 // list. 30 func (tablets Tablets) AliasStringList() []string { 31 aliases := make([]string, len(tablets)) 32 33 for i, tablet := range tablets { 34 aliases[i] = topoproto.TabletAliasString(tablet.Tablet.Alias) 35 } 36 37 return aliases 38 } 39 40 // FilterTablets returns a subset of tablets (not exceeding maxResults) that 41 // satisfy the given condition. 42 // 43 // If maxResults is negative, len(tablets) is used instead. 44 func FilterTablets(condition func(tablet *vtadminpb.Tablet) bool, tablets []*vtadminpb.Tablet, maxResults int) []*vtadminpb.Tablet { 45 if maxResults < 0 { 46 maxResults = len(tablets) 47 } 48 49 results := make([]*vtadminpb.Tablet, 0, maxResults) 50 51 for _, tablet := range tablets { 52 if len(results) >= maxResults { 53 break 54 } 55 56 if condition(tablet) { 57 results = append(results, tablet) 58 } 59 } 60 61 return results 62 } 63 64 // ParseTabletServingState returns a ServingState value from the given string. 65 // If the string does not map to a valid value, this function returns UNKNOWN. 66 func ParseTabletServingState(state string) vtadminpb.Tablet_ServingState { 67 if s, ok := vtadminpb.Tablet_ServingState_value[state]; ok { 68 return vtadminpb.Tablet_ServingState(s) 69 } 70 71 return vtadminpb.Tablet_UNKNOWN 72 } 73 74 // TabletServingStateString returns a ServingState represented as a string. If 75 // the state does not map to a valid value, this function returns "UNKNOWN". 76 func TabletServingStateString(state vtadminpb.Tablet_ServingState) string { 77 if s, ok := vtadminpb.Tablet_ServingState_name[int32(state)]; ok { 78 return s 79 } 80 81 return "UNKNOWN" 82 }