package api
import (
"html/template"
"net/http"
)
type statsApi struct {
ctx *ApiContext
mux *http.ServeMux
}
func (a *statsApi) Name() string {
return "Stats"
}
func (a *statsApi) Domains() []string {
return []string{"bridge.sati.ac"}
}
func (a *statsApi) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.mux.ServeHTTP(w, r)
}
var statsTemplate = func() *template.Template {
template, err := template.New("stats").Parse(`
sati bridge
domain | handler |
{{range $domain, $handler := .Domains}}
{{$domain}} | {{$handler}} |
{{end}}
task registry |
Total | {{.Registry.Total}} |
Success | {{.Registry.Success}} |
Error | {{.Registry.Error}} |
Processing | {{.Registry.Processing}} |
`)
if err != nil {
panic(err)
}
return template
}()
func (a *statsApi) stats(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
domains := make(map[string]string, len(a.ctx.Server.domains))
for domain, handler := range a.ctx.Server.domains {
domains[domain] = handler.Name()
}
statsTemplate.Execute(w, &struct {
Domains map[string]string
Registry RegistryStats
}{domains, a.ctx.Registry.Stats()})
}
func newStatsApi(ctx *ApiContext) ApiHandler {
api := &statsApi{ctx, http.NewServeMux()}
api.mux.HandleFunc("/", api.stats)
return api
}