vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/schema/schemaz.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 schema
    18  
    19  import (
    20  	"html/template"
    21  	"net/http"
    22  	"sort"
    23  
    24  	"vitess.io/vitess/go/acl"
    25  	"vitess.io/vitess/go/vt/log"
    26  	"vitess.io/vitess/go/vt/logz"
    27  )
    28  
    29  var (
    30  	schemazHeader = []byte(`
    31  		<tr>
    32  			<th>Table</th>
    33  			<th>Fields</th>
    34  			<th>Primary Key</th>
    35  			<th>Type</th>
    36  			<th>Metadata</th>
    37  		</tr>
    38  	`)
    39  	schemazTmpl = template.Must(template.New("example").Parse(`
    40  	{{$top := .}}{{with .Table}}<tr class="low">
    41  			<td>{{.Name}}</td>
    42  			<td>{{range .Fields}}{{.Name}}: {{.Type}}<br>{{end}}</td>
    43  			<td>{{range .PKColumns}}{{with index $top.Table.Fields .}}{{.Name}}{{end}}<br>{{end}}</td>
    44  			<td>{{index $top.Type .Type}}</td>
    45  			<td>{{.SequenceInfo}}{{.MessageInfo}}</td>
    46  		</tr>{{end}}
    47  	`))
    48  )
    49  
    50  type schemazSorter struct {
    51  	rows []*Table
    52  	less func(row1, row2 *Table) bool
    53  }
    54  
    55  func (sorter *schemazSorter) Len() int {
    56  	return len(sorter.rows)
    57  }
    58  
    59  func (sorter *schemazSorter) Swap(i, j int) {
    60  	sorter.rows[i], sorter.rows[j] = sorter.rows[j], sorter.rows[i]
    61  }
    62  
    63  func (sorter *schemazSorter) Less(i, j int) bool {
    64  	return sorter.less(sorter.rows[i], sorter.rows[j])
    65  }
    66  
    67  func schemazHandler(tables map[string]*Table, w http.ResponseWriter, r *http.Request) {
    68  	if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
    69  		acl.SendError(w, err)
    70  		return
    71  	}
    72  	logz.StartHTMLTable(w)
    73  	defer logz.EndHTMLTable(w)
    74  	w.Write(schemazHeader)
    75  
    76  	tableList := make([]*Table, 0, len(tables))
    77  	for _, t := range tables {
    78  		tableList = append(tableList, t)
    79  	}
    80  
    81  	sorter := schemazSorter{
    82  		rows: tableList,
    83  		less: func(row1, row2 *Table) bool {
    84  			return row1.Name.String() > row2.Name.String()
    85  		},
    86  	}
    87  	sort.Sort(&sorter)
    88  	envelope := struct {
    89  		Type  []string
    90  		Table *Table
    91  	}{
    92  		Type: TypeNames,
    93  	}
    94  	for _, Value := range sorter.rows {
    95  		envelope.Table = Value
    96  		if err := schemazTmpl.Execute(w, envelope); err != nil {
    97  			log.Errorf("schemaz: couldn't execute template: %v", err)
    98  		}
    99  	}
   100  }