vitess.io/vitess@v0.16.2/go/vt/vtgate/vschema_stats.go (about)

     1  /*
     2  Copyright 2019 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 vtgate
    18  
    19  import (
    20  	"sort"
    21  
    22  	"vitess.io/vitess/go/vt/vtgate/vindexes"
    23  )
    24  
    25  // VSchemaStats contains a rollup of the VSchema stats.
    26  type VSchemaStats struct {
    27  	Error     string
    28  	Keyspaces []*VSchemaKeyspaceStats
    29  }
    30  
    31  // VSchemaKeyspaceStats contains a rollup of the VSchema stats for a keyspace.
    32  // It is used to display a table with the information in the status page.
    33  type VSchemaKeyspaceStats struct {
    34  	Keyspace    string
    35  	Sharded     bool
    36  	TableCount  int
    37  	VindexCount int
    38  	Error       string
    39  }
    40  
    41  // NewVSchemaStats returns a new VSchemaStats from a VSchema.
    42  func NewVSchemaStats(vschema *vindexes.VSchema, errorMessage string) *VSchemaStats {
    43  	stats := &VSchemaStats{
    44  		Error:     errorMessage,
    45  		Keyspaces: make([]*VSchemaKeyspaceStats, 0, len(vschema.Keyspaces)),
    46  	}
    47  	for n, k := range vschema.Keyspaces {
    48  		s := &VSchemaKeyspaceStats{
    49  			Keyspace: n,
    50  		}
    51  		if k.Keyspace != nil {
    52  			s.Sharded = k.Keyspace.Sharded
    53  			s.TableCount += len(k.Tables)
    54  			for _, t := range k.Tables {
    55  				s.VindexCount += len(t.ColumnVindexes) + len(t.Ordered) + len(t.Owned)
    56  			}
    57  		}
    58  		if k.Error != nil {
    59  			s.Error = k.Error.Error()
    60  		}
    61  		stats.Keyspaces = append(stats.Keyspaces, s)
    62  	}
    63  	sort.Slice(stats.Keyspaces, func(i, j int) bool { return stats.Keyspaces[i].Keyspace < stats.Keyspaces[j].Keyspace })
    64  
    65  	return stats
    66  }
    67  
    68  const (
    69  	// VSchemaTemplate is the HTML template to display VSchemaStats.
    70  	VSchemaTemplate = `
    71  <style>
    72    table {
    73      border-collapse: collapse;
    74    }
    75    td, th {
    76      border: 1px solid #999;
    77      padding: 0.2rem;
    78    }
    79  </style>
    80  <table>
    81    <tr>
    82      <th colspan="5">VSchema Cache <i><a href="/debug/vschema">in JSON</a></i></th>
    83    </tr>
    84  {{if .Error}}
    85    <tr>
    86      <th>Error</th>
    87      <td colspan="3">{{$.Error}}</td>
    88    </tr>
    89    <tr>
    90      <td>colspan="4"></td>
    91    </tr>
    92  {{end}}
    93    <tr>
    94      <th>Keyspace</th>
    95      <th>Sharded</th>
    96      <th>Table Count</th>
    97      <th>Vindex Count</th>
    98      <th>Error</th>
    99    </tr>
   100  {{range $i, $ks := .Keyspaces}}  <tr>
   101      <td>{{$ks.Keyspace}}</td>
   102      <td>{{if $ks.Sharded}}Yes{{else}}No{{end}}</td>
   103      <td>{{$ks.TableCount}}</td>
   104      <td>{{$ks.VindexCount}}</td>
   105      <td style="color:red">{{$ks.Error}}</td>
   106    </tr>{{end}}
   107  </table>
   108  `
   109  )