forked from Onyx-Protocol/Onyx
/
summary.go
113 lines (98 loc) · 2.04 KB
/
summary.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
package main
import (
"bytes"
"context"
"encoding/json"
"html/template"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
)
type results struct {
Runs []Run
}
type Run struct {
StartedAt string
Elapsed time.Duration
Path string
Command []string
Ok bool
}
func getSummary(ctx context.Context) *results {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
res, err := s3svc.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: bucket,
Key: aws.String("results.json"),
})
if a, ok := err.(awserr.Error); ok && a.Code() == "NoSuchKey" {
return new(results)
} else if err != nil {
panic(err)
}
var s *results
err = json.NewDecoder(res.Body).Decode(&s)
if err != nil {
panic(err)
}
return s
}
func putSummary(ctx context.Context, s *results) {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
b, err := json.MarshalIndent(s, "", "\t")
if err != nil {
panic(err)
}
put(ctx, "results.json", b, "text/plain; charset=utf-8")
reverse(s.Runs)
var buf bytes.Buffer
err = summaryTemplate.Execute(&buf, s)
if err != nil {
panic(err)
}
put(ctx, "index.html", buf.Bytes(), "text/html; charset=utf-8")
_, err = s3svc.PutBucketWebsite(&s3.PutBucketWebsiteInput{
Bucket: bucket,
WebsiteConfiguration: &s3.WebsiteConfiguration{
IndexDocument: &s3.IndexDocument{Suffix: aws.String("index.html")},
},
})
if err != nil {
panic(err)
}
}
func reverse(a []Run) {
for i := 0; i < len(a)/2; i++ {
a[i], a[len(a)-i-1] = a[len(a)-i-1], a[i]
}
}
var summaryTemplate = template.Must(template.New("results").Parse(`
<!doctype html>
<html>
<head>
<style>
:root {
font-family: monospace;
}
td {
padding: .25em .5em;
}
</style>
</head>
<body>
<p>Raw results: <a href=results.json>results.json</a></p>
<table>
{{range .Runs}}
<tr>
<td>{{.StartedAt}}</td>
<td>{{if .Ok}}ok{{else}}<a href={{.Path}}><b>fail</b></a>{{end}}</td>
<td>{{.Elapsed}}</td>
<td>{{.Command}}</td>
</tr>
{{ end }}
</table>
</body>
</html>
`))