/
dashboard.go
207 lines (176 loc) · 4.59 KB
/
dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package dkron
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"time"
"github.com/distribworks/dkron/v3/dkron/assets"
"github.com/distribworks/dkron/v3/dkron/templates"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
const (
dashboardPathPrefix = "dashboard"
assetsPrefix = "static"
)
type commonDashboardData struct {
Version string
LeaderName string
MemberName string
AssetsPath string
Path string
APIPath string
Name string
Time string
}
func newCommonDashboardData(a *Agent, nodeName, path string) *commonDashboardData {
leaderName := ""
l, err := a.leaderMember()
if err == nil {
leaderName = l.Name
}
return &commonDashboardData{
Version: Version,
LeaderName: leaderName,
MemberName: nodeName,
AssetsPath: fmt.Sprintf("%s%s", path, assetsPrefix),
Path: fmt.Sprintf("%s%s", path, dashboardPathPrefix),
APIPath: fmt.Sprintf("%s%s", path, apiPathPrefix),
Name: Name,
Time: time.Now().UTC().Format("15:04:05 UTC"),
}
}
// DashboardRoutes registers dashboard specific routes on the gin RouterGroup.
func (a *Agent) DashboardRoutes(r *gin.RouterGroup) {
// If we are visiting from a browser redirect to the dashboard
r.GET("/", func(c *gin.Context) {
switch c.NegotiateFormat(gin.MIMEHTML) {
case gin.MIMEHTML:
c.Redirect(http.StatusMovedPermanently, "/"+dashboardPathPrefix+"/")
default:
c.AbortWithStatus(http.StatusNotFound)
}
})
r.StaticFS("static", assets.Assets)
dashboard := r.Group("/" + dashboardPathPrefix)
dashboard.GET("/", a.dashboardIndexHandler)
dashboard.GET("/jobs", a.dashboardJobsHandler)
dashboard.GET("/jobs/:job/executions", a.dashboardExecutionsHandler)
dashboard.GET("/busy", a.dashboardBusyHandler)
}
func (a *Agent) dashboardIndexHandler(c *gin.Context) {
data := struct {
Common *commonDashboardData
}{
Common: newCommonDashboardData(a, a.config.NodeName, "../"),
}
c.HTML(http.StatusOK, "index", data)
}
func (a *Agent) dashboardJobsHandler(c *gin.Context) {
data := struct {
Common *commonDashboardData
}{
Common: newCommonDashboardData(a, a.config.NodeName, "../"),
}
c.HTML(http.StatusOK, "jobs", data)
}
func (a *Agent) dashboardExecutionsHandler(c *gin.Context) {
jobName := c.Param("job")
// Get the job's timezone
var jobLocation *time.Location
if job, err := a.Store.GetJob(jobName, nil); err == nil {
jobLocation = job.GetTimeLocation()
}
groups, byGroup, err := a.Store.GetGroupedExecutions(jobName,
&ExecutionOptions{
Timezone: jobLocation,
},
)
if err != nil {
a.logger.Error(err)
}
var count int
for _, v := range groups {
count = count + len(v)
}
data := struct {
Common *commonDashboardData
Groups map[int64][]*Execution
JobName string
ByGroup int64arr
Count int
}{
Common: newCommonDashboardData(a, a.config.NodeName, "../../../"),
Groups: groups,
JobName: jobName,
ByGroup: byGroup,
Count: count,
}
c.HTML(http.StatusOK, "executions", data)
}
func (a *Agent) dashboardBusyHandler(c *gin.Context) {
data := struct {
Common *commonDashboardData
}{
Common: newCommonDashboardData(a, a.config.NodeName, "../"),
}
c.HTML(http.StatusOK, "busy", data)
}
func mustLoadTemplate(path string, log *logrus.Entry) []byte {
f, err := templates.Templates.Open(path)
if err != nil {
log.Error(err)
return nil
}
tmpl, err := ioutil.ReadAll(f)
if err != nil {
log.Error(err)
return nil
}
return tmpl
}
// CreateMyRender returns a new custom multitemplate renderer
// to use with Gin.
func CreateMyRender(l *logrus.Entry) multitemplate.Render {
r := multitemplate.New()
status := mustLoadTemplate("/status.html.tmpl", l)
dash := mustLoadTemplate("/dashboard.html.tmpl", l)
r.AddFromStringsFuncs("index", funcMap(),
string(dash),
string(status),
string(mustLoadTemplate("/index.html.tmpl", l)))
r.AddFromStringsFuncs("jobs", funcMap(),
string(dash),
string(status),
string(mustLoadTemplate("/jobs.html.tmpl", l)))
r.AddFromStringsFuncs("executions", funcMap(),
string(dash),
string(status),
string(mustLoadTemplate("/executions.html.tmpl", l)))
r.AddFromStringsFuncs("busy", funcMap(),
string(dash),
string(status),
string(mustLoadTemplate("/busy.html.tmpl", l)))
return r
}
func funcMap() template.FuncMap {
funcs := template.FuncMap{
"toString": func(value []byte) string {
return string(template.HTML(value))
},
// Now unicode compliant
"truncate": func(s string) string {
var numRunes = 0
for index := range s {
numRunes++
if numRunes > 25 {
return s[:index]
}
}
return s
},
}
return funcs
}