forked from IzakMarais/reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
177 lines (152 loc) · 4.2 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
/*
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/izakmarais/reporter/grafana"
"github.com/pborman/uuid"
)
type Report struct {
gClient grafana.Client
time grafana.TimeRange
texTemplate string
dashName string
tmpDir string
}
const (
imgDir = "images"
reportTexFile = "report.tex"
reportPdf = "report.pdf"
)
// New creates a new Report. If texTemplate is empty, a default tex template is used.
func New(g grafana.Client, dashName string, time grafana.TimeRange, texTemplate string) Report {
if texTemplate == "" {
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 *os.File, err error) {
dash, err := rep.gClient.GetDashboard(rep.dashName)
if err != nil {
return
}
err = rep.renderPNGsParallel(dash)
if err != nil {
return
}
err = rep.generateTeXFile(dash)
if err != nil {
return
}
pdf, err = rep.runLaTeX()
return
}
// 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) (err error) {
var wg sync.WaitGroup
wg.Add(len(dash.Panels))
for _, p := range dash.Panels {
go func(p grafana.Panel) {
defer wg.Done()
err = rep.renderPNG(p)
if err != nil {
log.Printf("Error creating image for panel: %v", err)
return
}
}(p)
}
wg.Wait()
return
}
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", 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) (err error) {
type templData struct {
grafana.Dashboard
grafana.TimeRange
}
err = os.MkdirAll(rep.tmpDir, 0777)
if err != nil {
return
}
file, err := os.Create(rep.texPath())
if err != nil {
return
}
defer file.Close()
tmpl, err := template.New("report").Delims("[[", "]]").Parse(rep.texTemplate)
if err != nil {
err = fmt.Errorf("Error parsing template '%s': %q", rep.texTemplate, err)
return
}
data := templData{dash, rep.time}
err = tmpl.Execute(file, data)
return
}
func (rep *Report) runLaTeX() (pdf *os.File, err error) {
cmd := exec.Command("pdflatex", "-halt-on-error", reportTexFile)
cmd.Dir = rep.tmpDir
outBytes, err := cmd.CombinedOutput()
log.Println("Calling LaTeX")
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
}