Skip to content

Commit

Permalink
Make Client an http.Handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurin committed Mar 7, 2018
1 parent e2376fc commit 4ecbf0c
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 0 deletions.
74 changes: 74 additions & 0 deletions b2/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ package b2

import (
"fmt"
"html/template"
"math"
"net/http"
"sort"
"sync"
"time"

"github.com/kurin/blazer/internal/b2assets"
)

// StatusInfo reports information about a client.
Expand Down Expand Up @@ -147,6 +152,26 @@ func (mi *MethodInfo) CountByCode() map[int]int {
return t
}

// CountByMethodAndCode returns the number of calls broken down by method and
// status code.
func (mi *MethodInfo) CountByMethodAndCode() map[string]map[int]int {
mi.mu.Lock()
defer mi.mu.Unlock()

t := make(map[string]map[int]int)
for method, codes := range mi.data {
if t[method] == nil {
t[method] = make(map[int]int)
}
for code, h := range codes {
for i := range h {
t[method][code] += h[i]
}
}
}
return t
}

// Histogram returns a slice of bins for call latencies. The bin contains the
// number of requests that finished within 1ms, and the second 1-3ms, the third
// 3-7ms, etc. The width of the bin in index i is 2^i milliseconds wide, and
Expand Down Expand Up @@ -262,3 +287,52 @@ func (c *Client) removeReader(r *Reader) {

delete(c.sReaders, fmt.Sprintf("%s/%s", r.o.b.Name(), r.name))
}

var (
funcMap = template.FuncMap{
"inc": func(i int) int { return i + 1 },
"lookUp": func(m map[string]int, s string) int { return m[s] },
"pRange": func(i int) string {
f := float64(i)
min := int(math.Pow(2, f)) - 1
max := min + int(math.Pow(2, f))
return fmt.Sprintf("%v - %v", time.Duration(min)*time.Millisecond, time.Duration(max)*time.Millisecond)
},
"allCodes": func(mi *MethodInfo) []int {
var codes []int
for c := range mi.CountByCode() {
codes = append(codes, c)
}
sort.Ints(codes)
return codes
},
"allMethods": func(mi *MethodInfo) []string {
var meths []string
for m := range mi.CountByMethod() {
meths = append(meths, m)
}
sort.Strings(meths)
return meths
},
"getCount": func(mi *MethodInfo, method string, code int) int {
cmap := mi.CountByMethodAndCode()
codes, ok := cmap[method]
if !ok {
return 0
}
return codes[code]
},
}
statusTemplate = template.Must(template.New("status").Funcs(funcMap).Parse(string(b2assets.MustAsset("data/status.html"))))
)

// ServeHTTP satisfies the http.Handler interface. This means that a Client
// can be passed directly to a path via http.Handle (or on a custom ServeMux or
// a custom http.Server).
//
// ServeHTTP serves diagnostic information about the current state of the
// client; essentially everything available from Client.Status()
func (c *Client) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
info := c.Status()
statusTemplate.Execute(rw, info)
}
237 changes: 237 additions & 0 deletions internal/b2assets/b2assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions internal/b2assets/data/status.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<title>b2 client status</title>
</head>
<body>
<h1>count by code</h1>
<table>
<tr>
<th>method</th>
{{range $code := allCodes .MethodInfo}}
<th>{{$code}}</th>
{{end}}
</tr>
{{range $method := allMethods .MethodInfo}}
<tr>
<td>{{$method}}</td>
{{range $code := allCodes $.MethodInfo}}
<td>{{getCount $.MethodInfo $method $code}}</td>
{{end}}
</tr>
{{end}}
</tr>
</table>
<h1>uploads</h1>
{{range $name, $val := .Writers}}
<h2>{{ $name }}</h2>
{{range $id, $prog := $val.Progress}}
{{inc $id}} <progress value="{{$prog}}" max="1"></progress><br />
{{end}}
{{end}}
<h1>downloads</h1>
{{range $name, $val := .Readers}}
<h2>{{ $name }}</h2>
{{range $id, $prog := $val.Progress}}
{{inc $id}} <progress value="{{$prog}}" max="1"></progress><br />
{{end}}
{{end}}
</body>
</html>
18 changes: 18 additions & 0 deletions internal/b2assets/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2018, Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package b2assets contains data required by other libraries in blazer.
package b2assets

//go:generate go-bindata -pkg $GOPACKAGE -o b2assets.go data/

0 comments on commit 4ecbf0c

Please sign in to comment.