Commit 6e2c354b authored by ale's avatar ale
Browse files

Add HTML debug pages

parent cb36cb32
package tabacco
import (
"html/template"
"net/http"
)
var (
headerTpl = `<!doctype html>
<html lang="en">
<head>
<title>Tabacco - Debug</title>
<style type="text/css">
body { background: white; font-family: "Helvetica", sans-serif; }
.table th { text-align: left; font-weight: bold; }
.table td { text-align: left; }
.table thead tr { border-bottom: 2px solid #333; }
.error { color: #a00; }
</style>
</head>
<body>
`
stateManagerDebugTpl = `{{template "header"}}
{{define "job_status"}}
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Status</th>
<th>Started At</th>
<th>Completed At</th>
<th>Error</th>
</tr>
</thead>
<tbody>
{{range .}}
<tr>
<td>{{.ID}}</td>
<td>{{.Job.Key}}</td>
<td>{{.Status}}</td>
<td>{{if not .StartedAt.IsZero}}{{.StartedAt}}{{end}}</td>
<td>{{if not .CompletedAt.IsZero}}{{.CompletedAt}}{{end}}</td>
<td><span class="error">{{.Err}}</span></td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
<h1>Jobs</h1>
<h3>Running ({{.NumRunning}})</h3>
{{template "job_status" .Running}}
<h3>Pending ({{.NumPending}})</h3>
{{template "job_status" .Pending}}
<h3>Done ({{.NumDone}})</h3>
{{template "job_status" .Done}}
{{template "footer"}}
`
schedulerDebugTpl = `{{template "header"}}
{{define "schedule_status"}}
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Handler</th>
<th>Schedule</th>
<th>Last Run</th>
<th>Next Run</th>
</tr>
</thead>
<tbody>
{{range .}}
<tr>
<td>{{.Spec.Name}}</td>
<td>{{.Spec.Handler}}</td>
<td>{{.Spec.Schedule}}</td>
<td>{{if not .Prev.IsZero}}{{.Prev}}{{end}}</td>
<td>{{if not .Next.IsZero}}{{.Next}}{{end}}</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
<h1>Schedule</h1>
{{template "schedule_status" .Schedule}}
{{template "footer"}}
`
footerTpl = `
</body>
</html>`
debugTpl *template.Template
)
func init() {
debugTpl = template.New("")
template.Must(debugTpl.New("header").Parse(headerTpl))
template.Must(debugTpl.New("footer").Parse(footerTpl))
template.Must(debugTpl.New("state_manager_debug_page").Parse(stateManagerDebugTpl))
template.Must(debugTpl.New("scheduler_debug_page").Parse(schedulerDebugTpl))
}
// ServeHTTP implements the job status debug handler, by making the
// stateManager object match the http.Handler interface.
func (j *stateManager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pending, running, done := j.getJobsStatus()
w.Header().Set("Content-Type", "text/html")
debugTpl.Lookup("state_manager_debug_page").Execute(w, map[string]interface{}{
"Pending": pending,
"NumPending": len(pending),
"Running": running,
"NumRunning": len(running),
"Done": done,
"NumDone": len(done),
})
}
// ServeHTTP implements the job status debug handler, by making the
// stateManager object match the http.Handler interface.
func (s *Scheduler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
debugTpl.Lookup("scheduler_debug_page").Execute(w, map[string]interface{}{
"Schedule": s.getStatus(),
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment