move UI code into pkg/dashboard

This commit is contained in:
Bobby Brennan
2019-03-15 13:25:19 +00:00
parent a3e5d60ffe
commit deacad7724
3 changed files with 34 additions and 30 deletions

View File

@@ -10,6 +10,9 @@ import (
"github.com/reactiveops/fairwinds/pkg/validator"
)
const TEMPLATE_NAME = "dashboard.gohtml"
const TEMPLATE_FILE = "pkg/dashboard/templates/" + TEMPLATE_NAME
// TemplateData represents data in a format that's template friendly.
type TemplateData struct {
ClusterSummary *validator.ResultSummary
@@ -23,9 +26,31 @@ func MainHandler(w http.ResponseWriter, r *http.Request, c conf.Configuration, k
http.Error(w, "Error Fetching Deployments", 500)
return
}
tmpl := template.Must(template.ParseFiles("pkg/dashboard/templates/dashboard.gohtml"))
tmpl.Execute(w, templateData)
tmpl, err := template.New(TEMPLATE_NAME).Funcs(template.FuncMap{
"getWarningWidth": func(rs validator.ResultSummary, fullWidth int) uint {
return uint(float64(rs.Successes+rs.Warnings) / float64(rs.Successes+rs.Warnings+rs.Failures) * float64(fullWidth))
},
"getSuccessWidth": func(rs validator.ResultSummary, fullWidth int) uint {
return uint(float64(rs.Successes) / float64(rs.Successes+rs.Warnings+rs.Failures) * float64(fullWidth))
},
"getCharCode": func(rm validator.ResultMessage) string {
switch rm.Type {
case "success":
return "9745"
case "warning":
return "9888"
default:
return "9746"
}
},
}).ParseFiles(TEMPLATE_FILE)
if err != nil {
panic(err)
}
err = template.Must(tmpl.Clone()).Execute(w, templateData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// EndpointHandler gets template data and renders json with it.

View File

@@ -53,8 +53,8 @@
<div class="extra">
<h4>Pod:</h4>
<ul>
{{ range .Messages}}
<li class="{{ .Type }}"><span>&#{{ .HTMLSpecialCharCode }};</span> {{ .Message }}</li>
{{ range $message := .Messages}}
<li class="{{ .Type }}"><span>&#{{ getCharCode $message }};</span> {{ .Message }}</li>
{{ end }}
</ul>
</div>
@@ -62,8 +62,8 @@
<div class="extra">
<h4>Container: {{ .Name }}</h4>
<ul>
{{ range .Messages}}
<li class="{{ .Type }}"><span>&#{{ .HTMLSpecialCharCode }};</span> {{ .Message }}</li>
{{ range $message := .Messages}}
<li class="{{ .Type }}"><span>&#{{ getCharCode $message }};</span> {{ .Message }}</li>
{{ end }}
</ul>
</div>
@@ -73,8 +73,8 @@
<td class="status-bar">
<div class="status">
<div class="failing">
<div class="warning" style="width: {{ .Summary.WarningWidth 200 }}px;">
<div class="passing" style="width: {{ .Summary.SuccessWidth 200 }}px;"></div>
<div class="warning" style="width: {{ getWarningWidth .Summary 200 }}px;">
<div class="passing" style="width: {{ getSuccessWidth .Summary 200 }}px;"></div>
</div>
</div>
</div>

View File

@@ -49,24 +49,3 @@ func (rs *ResultSummary) Score() uint {
return uint(float64(rs.Successes) / float64(rs.Successes+rs.Warnings+rs.Failures) * 100)
}
// WarningWidth is a UI specific helper that helps determine the width of a progress bar.
func (rs *ResultSummary) WarningWidth(fullWidth uint) uint {
return uint(float64(rs.Successes+rs.Warnings) / float64(rs.Successes+rs.Warnings+rs.Failures) * float64(fullWidth))
}
// SuccessWidth is a UI specific helper that helps determine the width of a progress bar.
func (rs *ResultSummary) SuccessWidth(fullWidth uint) uint {
return uint(float64(rs.Successes) / float64(rs.Successes+rs.Warnings+rs.Failures) * float64(fullWidth))
}
// HTMLSpecialCharCode is a UI specific helper that provides an HTML char code.
func (rm *ResultMessage) HTMLSpecialCharCode() string {
switch rm.Type {
case "success":
return "9745"
case "warning":
return "9888"
default:
return "9746"
}
}