/
report.go
245 lines (216 loc) · 6.69 KB
/
report.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
/*
Copyright 2016 Vastech SA (PTY) LTD
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package report
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"sync"
"text/template"
"github.com/8710925/reporter/grafana"
"github.com/pborman/uuid"
)
// Report groups functions related to genrating the report.
// After reading and closing the pdf returned by Generate(), call Clean() to delete the pdf file as well the temporary build files
type Report interface {
Generate() (pdf io.ReadCloser, err error)
Title() string
Clean()
}
type report struct {
gClient grafana.Client
time grafana.TimeRange
texTemplate string
dashName string
tmpDir string
dashTitle string
}
const (
imgDir = "images"
reportTexFile = "report.tex"
reportPdf = "report.pdf"
)
// New creates a new Report.
// texTemplate is the content of a LaTex template file. If empty, a default tex template is used.
func New(g grafana.Client, dashName string, time grafana.TimeRange, texTemplate string, gridLayout bool) Report {
return new(g, dashName, time, texTemplate, gridLayout)
}
func new(g grafana.Client, dashName string, time grafana.TimeRange, texTemplate string, gridLayout bool) *report {
if texTemplate == "" {
if gridLayout {
texTemplate = defaultGridTemplate
} else {
texTemplate = defaultTemplate
}
}
tmpDir := filepath.Join("tmp", uuid.New())
return &report{g, time, texTemplate, dashName, tmpDir, ""}
}
// Generate returns the report.pdf file. After reading this file it should be Closed()
// After closing the file, call report.Clean() to delete the file as well the temporary build files
func (rep *report) Generate() (pdf io.ReadCloser, err error) {
dash, err := rep.gClient.GetDashboard(rep.dashName)
if err != nil {
err = fmt.Errorf("error fetching dashboard %v: %v", rep.dashName, err)
return
}
rep.dashTitle = dash.Title
err = rep.renderPNGsParallel(dash)
if err != nil {
err = fmt.Errorf("error rendering PNGs in parralel for dash %+v: %v", dash, err)
return
}
err = rep.generateTeXFile(dash)
if err != nil {
err = fmt.Errorf("error generating TeX file for dash %+v: %v", dash, err)
return
}
pdf, err = rep.runLaTeX()
return
}
// Title returns the dashboard title parsed from the dashboard definition
func (rep *report) Title() string {
//lazy fetch if Title() is called before Generate()
if rep.dashTitle == "" {
dash, err := rep.gClient.GetDashboard(rep.dashName)
if err != nil {
return ""
}
rep.dashTitle = dash.Title
}
return rep.dashTitle
}
// Clean deletes the temporary directory used during report generation
func (rep *report) Clean() {
err := os.RemoveAll(rep.tmpDir)
if err != nil {
log.Println("Error cleaning up tmp dir:", err)
}
}
func (rep *report) imgDirPath() string {
return filepath.Join(rep.tmpDir, imgDir)
}
func (rep *report) pdfPath() string {
return filepath.Join(rep.tmpDir, reportPdf)
}
func (rep *report) texPath() string {
return filepath.Join(rep.tmpDir, reportTexFile)
}
func (rep *report) renderPNGsParallel(dash grafana.Dashboard) error {
//buffer all panels on a channel
panels := make(chan grafana.Panel, len(dash.Panels))
for _, p := range dash.Panels {
panels <- p
}
close(panels)
//fetch images in parrallel form Grafana sever.
//limit concurrency using a worker pool to avoid overwhelming grafana
//for dashboards with many panels.
var wg sync.WaitGroup
workers := 5
wg.Add(workers)
errs := make(chan error, len(dash.Panels)) //routines can return errors on a channel
for i := 0; i < workers; i++ {
go func(panels <-chan grafana.Panel, errs chan<- error) {
defer wg.Done()
for p := range panels {
err := rep.renderPNG(p)
if err != nil {
log.Printf("Error creating image for panel: %v", err)
errs <- err
}
}
}(panels, errs)
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
return err
}
}
return nil
}
func (rep *report) renderPNG(p grafana.Panel) error {
body, err := rep.gClient.GetPanelPng(p, rep.dashName, rep.time)
if err != nil {
return fmt.Errorf("error getting panel %+v: %v", p, err)
}
defer body.Close()
err = os.MkdirAll(rep.imgDirPath(), 0777)
if err != nil {
return fmt.Errorf("error creating img directory:%v", err)
}
imgFileName := fmt.Sprintf("image%d.png", p.Id)
file, err := os.Create(filepath.Join(rep.imgDirPath(), imgFileName))
if err != nil {
return fmt.Errorf("error creating image file:%v", err)
}
defer file.Close()
_, err = io.Copy(file, body)
if err != nil {
return fmt.Errorf("error copying body to file:%v", err)
}
return nil
}
func (rep *report) generateTeXFile(dash grafana.Dashboard) error {
type templData struct {
grafana.Dashboard
grafana.TimeRange
grafana.Client
}
err := os.MkdirAll(rep.tmpDir, 0777)
if err != nil {
return fmt.Errorf("error creating temporary directory at %v: %v", rep.tmpDir, err)
}
file, err := os.Create(rep.texPath())
if err != nil {
return fmt.Errorf("error creating tex file at %v : %v", rep.texPath(), err)
}
defer file.Close()
tmpl, err := template.New("report").Delims("[[", "]]").Parse(rep.texTemplate)
if err != nil {
return fmt.Errorf("error parsing template '%s': %v", rep.texTemplate, err)
}
data := templData{dash, rep.time, rep.gClient}
err = tmpl.Execute(file, data)
if err != nil {
return fmt.Errorf("error executing tex template:%v", err)
}
return nil
}
func (rep *report) runLaTeX() (pdf *os.File, err error) {
// cmdPre := exec.Command("pdflatex", "-halt-on-error", "-draftmode", reportTexFile)
cmdPre := exec.Command("xelatex", "-halt-on-error", reportTexFile)
cmdPre.Dir = rep.tmpDir
outBytesPre, errPre := cmdPre.CombinedOutput()
log.Println("Calling LaTeX - preprocessing")
if errPre != nil {
err = fmt.Errorf("error calling LaTeX preprocessing: %q. Latex preprocessing failed with output: %s ", errPre, string(outBytesPre))
return
}
// cmd := exec.Command("pdflatex", "-halt-on-error", reportTexFile)
cmd := exec.Command("xelatex", "-halt-on-error", reportTexFile)
cmd.Dir = rep.tmpDir
outBytes, err := cmd.CombinedOutput()
log.Println("Calling LaTeX and building PDF")
if err != nil {
err = fmt.Errorf("error calling LaTeX: %q. Latex failed with output: %s ", err, string(outBytes))
return
}
pdf, err = os.Open(rep.pdfPath())
return
}