-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.go
269 lines (244 loc) · 7.29 KB
/
console.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package mapreduce
import (
"html/template"
"net/http"
"strconv"
"strings"
"time"
"github.com/pendo-io/appwrap"
"google.golang.org/appengine"
)
const head = `<html><head><title>MapReduce Console</title>`
const main = `$(function() {
// call the tablesorter plugin
$("table").tablesorter({
headers: { 0: { sorter: 'integer' },
//1: { sorter: false },
2: { sorter: false },
3: { sorter: 'isoDatetime' },
4: { sorter: 'isoDatetime' },
5: { sorter: 'duration' },
6: { sorter: false },
},
sortList: [[3,1]],
//debug: true,
cancelSelection: false,
widgets: ['filter', 'zebra'],
widthFixed: true,
});
});
</script>
</head>
<body>
<h1>MapReduce Console</h1>
<h2>Jobs</h2>
<table>
<thead>
<tr>
<th align="center">Id</th>
<th align="center">Url<br /><input onchange='$(this).parents("table").trigger("applyWidgets")'></input></th>
<th align="center">Stage<br/><select multiple onchange='$(this).parents("table").trigger("applyWidgets")'></select></th>
<th align="center">Start Time</th>
<th align="center">Updated Time</th>
<th align="center">Duration</th>
<th></td>
</tr>
</thead>
<tbody>
{{range $index, $job := .Jobs}}
<tr>
{{$key := index $.Keys $index}}
<td>
{{ $id:=(KeyIntId $key) }}
<a href="job?id={{$id}}">{{$id}}</a>
</td>
<td>{{$job.UrlPrefix}}</td>
<td align="center">{{$job.Stage}}</td>
<td>{{$job.StartTime}}</td>
<td>{{$job.UpdatedAt}}</td>
<td>{{$job.Duration}}</td>
<td><button onclick="location.href='delete?id={{$id}}'">Delete</button></td>
</tr>
{{end}}
</tbody>
</table>
</body>
</html>
`
const jobPage = `$(function() {
// call the tablesorter plugin
$("table").tablesorter({
headers: { 0: { sorter: 'integer' },
1: { sorter: false },
2: { sorter: false },
3: { sorter: 'interger' },
4: { sorter: 'isoDatetime' },
5: { sorter: 'isoDatetime' },
//6: { sorter: false },
},
sortList: [[0,0]],
//debug: true,
cancelSelection: false,
widgets: ['filter', 'zebra'],
widthFixed: true,
});
});
</script>
</head>
<body>
<h1>MapReduce Task</h1>
<p>Job Id {{.Id}}</p>
<p>{{.Pending}} Pending / {{.Running}} Running / {{.Done}} Done / {{.Failed }} Failed</p>
<table>
<thead>
<tr>
<th align="center">Id</th>
<th align="center">Type<br/><select multiple onchange='$(this).parents("table").trigger("applyWidgets")'></select></th>
<th align="center">Status<br/><select multiple onchange='$(this).parents("table").trigger("applyWidgets")'></select></th>
<th align="center">Run Count<br/><select multiple onchange='$(this).parents("table").trigger("applyWidgets")'></select></th>
<th align="center">Start Time</th>
<th align="center">Update Time</th>
<th align="center">Info</th>
</tr>
</thead>
<tbody>
{{range $index, $task := .Tasks}}
<tr>
<td>
{{$key := index $.TaskKeys $index}} {{ KeyIntId $key }}
</td>
<td align="center">{{$task.Type}}</td>
<td align="center">{{$task.Status}}</td>
<td align="center">{{$task.Retries}}</td>
<td align="center">{{$task.StartTime}}</td>
<td align="center">{{$task.UpdatedAt}}</td>
<td align="center">{{$task.Info}}</td>
</tr>
{{end}}
</tbody>
</table>
</body>
</html>
`
func ConsoleHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
ds, err := appwrap.NewDatastore(c)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if strings.HasSuffix(r.URL.Path, "/job") {
id, _ := strconv.ParseInt(r.FormValue("id"), 10, 64)
var tasks []JobTask
var taskKeys []*appwrap.DatastoreKey
if job, err := GetJob(ds, id); err != nil {
http.Error(w, "Internal error reading job: "+err.Error(), http.StatusInternalServerError)
return
} else {
switch job.Stage {
case StageMapping, StageReducing:
if tl, err := GetJobTasks(ds, job); err != nil {
http.Error(w, "Internal error reading tasks: "+err.Error(), http.StatusInternalServerError)
return
} else {
tasks = tl
taskKeys = makeTaskKeys(ds, job.Id, job.FirstTaskId, job.TaskCount)
}
default:
jobKey := ds.NewKey(JobEntity, "", id, nil)
if tk, err := ds.NewQuery(TaskEntity).Filter("Job =", jobKey).GetAll(&tasks); err != nil {
http.Error(w, "Internal error: "+err.Error(), http.StatusInternalServerError)
return
} else {
taskKeys = tk
}
}
}
running := 0
pending := 0
done := 0
failed := 0
for i := range tasks {
switch tasks[i].Status {
case TaskStatusPending:
pending++
case TaskStatusRunning:
running++
case TaskStatusDone:
done++
case TaskStatusFailed:
failed++
}
}
funcMap := template.FuncMap{
"KeyIntId": appwrap.KeyIntID,
}
t := template.New("main").Funcs(funcMap)
t, err := t.Parse(head + css + js + jobPage)
if err != nil {
http.Error(w, "Internal error: "+err.Error(), http.StatusInternalServerError)
return
}
if err := t.Execute(w, struct {
Id int64
Tasks []JobTask
TaskKeys []*appwrap.DatastoreKey
Pending, Running, Done, Failed int
}{id, tasks, taskKeys, pending, running, done, failed}); err != nil {
http.Error(w, "Internal error: "+err.Error(), http.StatusInternalServerError)
return
}
} else if strings.HasSuffix(r.URL.Path, "/delete") {
id, _ := strconv.ParseInt(r.FormValue("id"), 10, 64)
if err := RemoveJob(ds, id); err != nil {
http.Error(w, "Internal error: "+err.Error(), http.StatusInternalServerError)
return
}
jobList(ds, w, r, id)
} else {
jobList(ds, w, r, 0)
}
}
func jobList(ds appwrap.Datastore, w http.ResponseWriter, r *http.Request, skipId int64) {
c := appengine.NewContext(r)
logger := appwrap.NewStackdriverLogging(c)
q := ds.NewQuery(JobEntity).Order("-UpdatedAt")
var jobs []JobInfo
keys, err := q.GetAll(&jobs)
if err != nil {
http.Error(w, "Internal error: "+err.Error(), http.StatusInternalServerError)
return
}
type annotatedJob struct {
JobInfo
Duration time.Duration
}
annotatedList := make([]annotatedJob, 0, len(keys))
for i, key := range keys {
if appwrap.KeyIntID(key) != skipId {
job := annotatedJob{JobInfo: jobs[i]}
if !jobs[i].StartTime.IsZero() {
job.Duration = jobs[i].UpdatedAt.Sub(jobs[i].StartTime)
}
annotatedList = append(annotatedList, job)
}
}
logger.Infof("%d jobs %d annotatedList", len(jobs), len(annotatedList))
funcMap := template.FuncMap{
"KeyIntId": appwrap.KeyIntID,
}
t := template.New("main").Funcs(funcMap)
t, err = t.Parse(head + css + js + main)
if err != nil {
http.Error(w, "Internal error: "+err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, struct {
Jobs []annotatedJob
Keys []*appwrap.DatastoreKey
}{annotatedList, keys})
if err != nil {
http.Error(w, "Internal error: "+err.Error(), http.StatusInternalServerError)
return
}
}