This commit is contained in:
Shengzhe Yao 2015-04-17 20:47:00 -07:00
Родитель 0199cf0de2
Коммит 699f376d5b
3 изменённых файлов: 116 добавлений и 31 удалений

Просмотреть файл

@ -391,6 +391,12 @@ func (rqsc *realQueryServiceControl) registerStreamQueryzHandlers() {
}) })
} }
func (rqsc *realQueryServiceControl) registerSchemazHandler() {
http.HandleFunc("/schemaz", func(w http.ResponseWriter, r *http.Request) {
schemazHandler(rqsc.sqlQueryRPCService.qe.schemaInfo.GetSchema(), w, r)
})
}
func buildFmter(logger *streamlog.StreamLogger) func(url.Values, interface{}) string { func buildFmter(logger *streamlog.StreamLogger) func(url.Values, interface{}) string {
type formatter interface { type formatter interface {
Format(url.Values) string Format(url.Values) string

Просмотреть файл

@ -50,37 +50,34 @@ func (sorter *schemazSorter) Less(i, j int) bool {
return sorter.less(sorter.rows[i], sorter.rows[j]) return sorter.less(sorter.rows[i], sorter.rows[j])
} }
func (rqsc *realQueryServiceControl) registerSchemazHandler() { func schemazHandler(tables []*schema.Table, w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/schemaz", func(w http.ResponseWriter, r *http.Request) { if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil { acl.SendError(w, err)
acl.SendError(w, err) return
return }
} startHTMLTable(w)
startHTMLTable(w) defer endHTMLTable(w)
defer endHTMLTable(w) w.Write(schemazHeader)
w.Write(schemazHeader)
tables := rqsc.sqlQueryRPCService.qe.schemaInfo.GetSchema() sorter := schemazSorter{
sorter := schemazSorter{ rows: tables,
rows: tables, less: func(row1, row2 *schema.Table) bool {
less: func(row1, row2 *schema.Table) bool { return row1.Name > row2.Name
return row1.Name > row2.Name },
}, }
sort.Sort(&sorter)
envelope := struct {
ColumnCategory []string
CacheType []string
Table *schema.Table
}{
ColumnCategory: []string{"other", "number", "varbinary"},
CacheType: []string{"none", "read-write", "write-only"},
}
for _, Value := range sorter.rows {
envelope.Table = Value
if err := schemazTmpl.Execute(w, envelope); err != nil {
log.Errorf("schemaz: couldn't execute template: %v", err)
} }
sort.Sort(&sorter) }
envelope := struct {
ColumnCategory []string
CacheType []string
Table *schema.Table
}{
ColumnCategory: []string{"other", "number", "varbinary"},
CacheType: []string{"none", "read-write", "write-only"},
}
for _, Value := range sorter.rows {
envelope.Table = Value
if err := schemazTmpl.Execute(w, envelope); err != nil {
log.Errorf("schemaz: couldn't execute template: %v", err)
}
}
})
} }

Просмотреть файл

@ -0,0 +1,82 @@
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/schema"
)
func TestSchamazHandler(t *testing.T) {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/schemaz", nil)
tableA := schema.NewTable("a")
tableB := schema.NewTable("b")
tableC := schema.NewTable("c")
tableA.AddColumn("column1", "int", sqltypes.MakeNumeric([]byte("0")), "auto_increment")
tableA.AddIndex("index1").AddColumn("index_column", 1000)
tableA.CacheType = schema.CACHE_RW
tableB.AddColumn("column2", "string", sqltypes.MakeString([]byte("NULL")), "")
tableB.AddIndex("index2").AddColumn("index_column2", 200)
tableB.CacheType = schema.CACHE_W
tableC.AddColumn("column3", "string", sqltypes.MakeString([]byte("")), "")
tableC.AddIndex("index3").AddColumn("index_column3", 500)
tableC.CacheType = schema.CACHE_NONE
tables := []*schema.Table{
tableA, tableB, tableC,
}
schemazHandler(tables, resp, req)
body, _ := ioutil.ReadAll(resp.Body)
tableCPattern := []string{
`<td>c</td>`,
`<td>column3: other, , <br></td>`,
`<td>index3: \(index_column3,\), \(500,\)<br></td>`,
`<td>none</td>`,
}
matched, err := regexp.Match(strings.Join(tableCPattern, `\s*`), body)
if err != nil {
t.Fatalf("schemaz page does not contain table C with error: %v", err)
}
if !matched {
t.Fatalf("schemaz page does not contain table C")
}
tableBPattern := []string{
`<td>b</td>`,
`<td>column2: other, , NULL<br></td>`,
`<td>index2: \(index_column2,\), \(200,\)<br></td>`,
`<td>write-only</td>`,
}
matched, err = regexp.Match(strings.Join(tableBPattern, `\s*`), body)
if err != nil {
t.Fatalf("schemaz page does not contain table B with error: %v", err)
}
if !matched {
t.Fatalf("schemaz page does not contain table B")
}
tableAPattern := []string{
`<td>a</td>`,
`<td>column1: number, autoinc, <br></td>`,
`<td>index1: \(index_column,\), \(1000,\)<br></td>`,
`<td>read-write</td>`,
}
matched, err = regexp.Match(strings.Join(tableAPattern, `\s*`), body)
if err != nil {
t.Fatalf("schemaz page does not contain table A with error: %v", err)
}
if !matched {
t.Fatalf("schemaz page does not contain table A")
}
}