-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
112 lines (96 loc) · 2.38 KB
/
web.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
package main
import (
"bufio"
"net/http"
"os"
"path"
"sort"
"strings"
"github.com/gin-gonic/gin"
"github.com/hyperpilotio/workload-profiler/jobs"
)
type FileLogs []jobs.JobSummary
func (d FileLogs) Len() int { return len(d) }
func (d FileLogs) Less(i, j int) bool {
return d[i].Create.Before(d[j].Create)
}
func (d FileLogs) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (server *Server) logUI(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"error": false,
})
}
func (server *Server) getFileLogList(c *gin.Context) {
fileLogs, err := server.getFileLogs(c)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"error": true,
"data": "",
})
return
}
c.JSON(http.StatusOK, gin.H{
"error": false,
"data": fileLogs,
})
}
func (server *Server) getFileLogContent(c *gin.Context) {
fileName := c.Param("fileName")
run, err := server.JobManager.FindJob(fileName)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"error": true,
"data": err,
})
return
}
logPath := path.Join(server.Config.GetString("filesPath"), "log", fileName+".log")
file, err := os.Open(logPath)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"error": true,
"data": "Unable to read deployment log: " + err.Error(),
})
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
lines := []string{}
// TODO: Find a way to pass io.reader to repsonse directly, to avoid copying
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
c.JSON(http.StatusOK, gin.H{
"error": false,
"data": lines,
"deployment": run.GetSummary(),
"state": run.GetState(),
})
}
func (server *Server) getFileLogs(c *gin.Context) (FileLogs, error) {
fileLogs := FileLogs{}
filterStatus := strings.ToUpper(c.Param("status"))
switch filterStatus {
case jobs.JOB_QUEUED, jobs.JOB_RESERVING, jobs.JOB_RUNNING, jobs.JOB_FINISHED:
for _, job := range server.JobManager.GetJobs() {
if job == nil {
continue
}
fileLog := job.GetSummary()
switch fileLog.Status {
case jobs.JOB_QUEUED, jobs.JOB_RESERVING, jobs.JOB_RUNNING, jobs.JOB_FINISHED:
fileLogs = append(fileLogs, job.GetSummary())
}
}
case jobs.JOB_FAILED:
for _, job := range server.JobManager.GetFailedJobs() {
if job == nil {
continue
}
fileLogs = append(fileLogs, job.GetSummary())
}
}
sort.Sort(fileLogs)
return fileLogs, nil
}