Skip to content

Commit 2341dec

Browse files
authored
Merge pull request #55 from factorysh/features/better-logs
Better logs output
2 parents fee0d10 + 679258a commit 2341dec

File tree

8 files changed

+70
-63
lines changed

8 files changed

+70
-63
lines changed

application/application.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ func New(cfg *conf.Conf) (*Application, error) {
163163
r.Get("/", a.TaskHandler(false))
164164
r.Get("/volumes/*", a.VolumesHandler(6, false))
165165
r.Get("/logs", a.TaskLogsHandler(false))
166-
r.Get("/logz", a.TaskLogzHandler(false))
167166
})
168167
r.Group(func(r chi.Router) {
169168
r.Use(a.RefererMiddleware)
@@ -177,7 +176,6 @@ func New(cfg *conf.Conf) (*Application, error) {
177176
r.Get("/", a.TaskHandler(true))
178177
r.Get("/volumes/*", a.VolumesHandler(6, true))
179178
r.Get("/logs", a.TaskLogsHandler(true))
180-
r.Get("/logz", a.TaskLogzHandler(true))
181179
})
182180
r.Group(func(r chi.Router) {
183181
r.Use(a.RefererMiddleware)

application/logs.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

application/task.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package application
22

33
import (
4+
"bytes"
5+
"context"
46
"encoding/json"
7+
"fmt"
8+
"html/template"
59
"net/http"
610
"net/url"
711
"os"
812
"time"
913

1014
"github.com/docker/docker/pkg/stdcopy"
1115
_claims "github.com/factorysh/microdensity/claims"
16+
"github.com/factorysh/microdensity/html"
1217
"github.com/factorysh/microdensity/task"
1318
"github.com/go-chi/chi/v5"
1419
"github.com/go-chi/render"
1520
"github.com/google/uuid"
21+
"github.com/robert-nix/ansihtml"
1622
"go.uber.org/zap"
1723
)
1824

@@ -182,8 +188,8 @@ func (a *Application) TaskIDHandler(w http.ResponseWriter, r *http.Request) {
182188
}
183189
}
184190

185-
// TaskLogsHandler get a logs for a task
186-
func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *http.Request) {
191+
// TaskLogzHandler get a logs for a task
192+
func (a *Application) TaskLogzHandler(latest bool) func(http.ResponseWriter, *http.Request) {
187193

188194
return func(w http.ResponseWriter, r *http.Request) {
189195
l := a.logger.With(
@@ -219,7 +225,7 @@ func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *ht
219225

220226
// just stdout for now
221227
// kudos @ndeloof, @rumpl, @glours
222-
_, err = stdcopy.StdCopy(w, nil, reader)
228+
_, err = stdcopy.StdCopy(w, w, reader)
223229
if err != nil {
224230
l.Error("Task log stdcopy write error", zap.Error(err))
225231
w.WriteHeader(http.StatusInternalServerError)
@@ -232,8 +238,8 @@ func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *ht
232238

233239
}
234240

235-
// TaskLogzHandler get a logs for a task
236-
func (a *Application) TaskLogzHandler(latest bool) func(http.ResponseWriter, *http.Request) {
241+
// TaskLogsHandler get a logs for a task, used to get row logs from curl, not used for now
242+
func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *http.Request) {
237243

238244
return func(w http.ResponseWriter, r *http.Request) {
239245
l := a.logger.With(
@@ -267,3 +273,34 @@ func (a *Application) TaskLogzHandler(latest bool) func(http.ResponseWriter, *ht
267273
}
268274
}
269275
}
276+
277+
func (a *Application) renderLogsPageForTask(ctx context.Context, t *task.Task, w http.ResponseWriter) error {
278+
279+
reader, err := t.Logs(ctx, false)
280+
if err != nil {
281+
return err
282+
}
283+
284+
var buffer bytes.Buffer
285+
_, err = stdcopy.StdCopy(&buffer, &buffer, reader)
286+
if err != nil {
287+
return err
288+
}
289+
290+
data, err := NewTaskPage(t, template.HTML(fmt.Sprintf("<pre>%s</pre>", ansihtml.ConvertToHTML(buffer.Bytes()))), a.GitlabURL, "Task Logs", "terminal")
291+
if err != nil {
292+
return err
293+
}
294+
295+
p := html.Page{
296+
Domain: a.Domain,
297+
Detail: fmt.Sprintf("%s / %s - logs", t.Service, t.Commit),
298+
Partial: html.Partial{
299+
Template: taskTemplate,
300+
Data: data,
301+
},
302+
}
303+
304+
w.WriteHeader(http.StatusOK)
305+
return p.Render(w)
306+
}

application/task_page.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type TaskPage struct {
1414
Project string
1515
Commit string
1616
Service string
17+
Branch string
1718
ID string
1819
CreatedAt string
1920
InnerDivClasses string
@@ -32,6 +33,7 @@ func NewTaskPage(t *task.Task, inner template.HTML, gitlabDomain, InnerTitle, In
3233
Project: prettyPath,
3334
GitlabDomain: gitlabDomain,
3435
Commit: t.Commit,
36+
Branch: t.Branch,
3537
Service: t.Service,
3638
ID: t.Id.String(),
3739
CreatedAt: t.Creation.Format("2006-01-02 15:04:05"),

application/templates/task.html

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
<h3>Task Description</h3>
1+
<div class="container task-description">
2+
<h3>Task Description</h3>
23

3-
<ul>
4-
<li>Project : <a href="{{ .GitlabDomain }}/{{ .Project }}" target="_blank">{{ .Project }}</a></li>
5-
<li>Commit : <a href="{{ .GitlabDomain }}/{{ .Project }}/-/commit/{{ .Commit }}" target="_blank">{{ .Commit }}</a></li>
6-
<li>Created At : {{ .CreatedAt }}</li>
7-
<li>Service : <a href="{{ .Domain }}/service/{{ .Service }}" target="_blank">{{ .Service }}</a></li>
8-
<li>ID : {{ .ID }}</li>
9-
</ul>
4+
<ul>
5+
<li>Project : <a href="{{ .GitlabDomain }}/{{ .Project }}" target="_blank">{{ .Project }}</a></li>
6+
<li>Commit : <a href="{{ .GitlabDomain }}/{{ .Project }}/-/commit/{{ .Commit }}" target="_blank">{{ .Commit }}</a></li>
7+
<li>Created At : {{ .CreatedAt }}</li>
8+
<li>Service : <a href="{{ .Domain }}/service/{{ .Service }}" target="_blank">{{ .Service }}</a></li>
9+
<li>ID : {{ .ID }}
10+
</ul>
1011

11-
<h3>{{ .InnerTitle }}</h3>
12+
<h3>Task Actions</h3>
13+
<a class="button" href="{{ .Domain }}/service/{{ .Service }}/{{ .Project }}/-/{{ .Branch }}/{{ .Commit }}/logs">Logs</a>
14+
<a class="button" href="{{ .Domain }}/service/{{ .Service }}/{{ .Project }}/-/{{ .Branch }}/{{ .Commit }}/volumes/data/result.html">Result</a>
15+
</div>
16+
17+
<div class="container">
18+
<h3>{{ .InnerTitle }}</h3>
1219

13-
<div class="{{ .InnerDivClasses }}">
14-
{{ .Inner }}
20+
<div class="{{ .InnerDivClasses }}">
21+
{{ .Inner }}
22+
</div>
1523
</div>

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ require (
101101
github.com/prometheus/client_model v0.2.0 // indirect
102102
github.com/prometheus/common v0.32.1 // indirect
103103
github.com/prometheus/procfs v0.7.3 // indirect
104+
github.com/robert-nix/ansihtml v1.0.0 // indirect
104105
github.com/sanathkr/go-yaml v0.0.0-20170819195128-ed9d249f429b // indirect
105106
github.com/sirupsen/logrus v1.8.1 // indirect
106107
github.com/spf13/cobra v1.3.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
964964
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
965965
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
966966
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
967+
github.com/robert-nix/ansihtml v1.0.0 h1:x/M0hHxcs+vCEGwfXtdWVROatZQRSXhn/akMwvPogB8=
968+
github.com/robert-nix/ansihtml v1.0.0/go.mod h1:CJwclxYaTPc2RfcxtanEACsYuTksh4yDXcNeHHKZINE=
967969
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
968970
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
969971
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=

html/templates/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ h6 {
4646
padding: 2rem;
4747
}
4848

49+
.task-description a {
50+
font-weight: bold;
51+
}
52+
4953
.label {
5054
font-weight: bold;
5155
}

0 commit comments

Comments
 (0)