2017-03-25 02:41:36 +00:00
|
|
|
package webcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-03-27 07:16:12 +00:00
|
|
|
"sort"
|
2017-03-25 02:41:36 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-03-27 07:16:12 +00:00
|
|
|
type TaskListItem struct {
|
|
|
|
ref string
|
|
|
|
start int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type TaskList []TaskListItem
|
|
|
|
|
|
|
|
func (tl TaskList) Len() int {
|
|
|
|
return len(tl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tl TaskList) Swap(i, j int) {
|
|
|
|
tl[i], tl[j] = tl[j], tl[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tl TaskList) Less(i, j int) bool {
|
|
|
|
return tl[i].start < tl[j].start
|
|
|
|
}
|
|
|
|
|
2017-03-25 02:41:36 +00:00
|
|
|
func (this *App) Serve_Tasks(w http.ResponseWriter) {
|
|
|
|
w.Header().Set("Content-Type", "text/html;charset=UTF-8")
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
this.ServePartial_Header(w, "/tasks")
|
|
|
|
|
|
|
|
fmt.Fprint(w, `
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Task</th>
|
|
|
|
<th>Started</th>
|
|
|
|
<th>State</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
`)
|
|
|
|
|
|
|
|
this.tasksMtx.RLock()
|
|
|
|
defer this.tasksMtx.RUnlock()
|
2017-03-27 07:16:12 +00:00
|
|
|
|
|
|
|
tl := make(TaskList, 0, len(this.tasks))
|
2017-03-25 02:41:36 +00:00
|
|
|
for ref, t := range this.tasks {
|
2017-03-27 07:16:12 +00:00
|
|
|
tl = append(tl, TaskListItem{ref, t.started})
|
|
|
|
}
|
|
|
|
sort.Sort(tl)
|
|
|
|
|
|
|
|
for _, tlRef := range tl {
|
|
|
|
ref := tlRef.ref
|
|
|
|
t := this.tasks[ref]
|
|
|
|
|
|
|
|
startTime := time.Unix(t.started, 0)
|
|
|
|
|
2017-03-25 02:41:36 +00:00
|
|
|
fmt.Fprintf(w,
|
|
|
|
`<tr>
|
|
|
|
<td><a href="/task/%s">%s</td>
|
2017-03-27 07:16:22 +00:00
|
|
|
<td><span title="%s">%s</span></td>
|
2017-03-25 02:41:36 +00:00
|
|
|
<td>
|
|
|
|
`,
|
|
|
|
hesc(ref), hesc(ref),
|
2017-03-27 07:16:22 +00:00
|
|
|
hesc(startTime.Format(time.RFC3339)),
|
|
|
|
hesc(startTime.Format(time.RFC822)),
|
2017-03-25 02:41:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if t.Finished() {
|
|
|
|
fmt.Fprint(w, `<span class="task-state-finished">Finished</span>`)
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(w, `<span class="task-state-running">Running</span>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(w, `
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(w, `
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<form method="POST" action="/x-clear-completed-tasks">
|
|
|
|
<input type="submit" value="Clear completed tasks »">
|
|
|
|
</form>
|
|
|
|
`)
|
|
|
|
this.ServePartial_Footer(w)
|
|
|
|
}
|