-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
metricsdecorator.go
215 lines (180 loc) · 5.24 KB
/
metricsdecorator.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
package maroto
import (
"github.com/johnfercher/go-tree/node"
"github.com/johnfercher/maroto/v2/internal/time"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/maroto/v2/pkg/metrics"
)
type MetricsDecorator struct {
addRowsTime []*metrics.Time
addRowTime []*metrics.Time
addAutoRowTime []*metrics.Time
addPageTime []*metrics.Time
headerTime *metrics.Time
footerTime *metrics.Time
generateTime *metrics.Time
structureTime *metrics.Time
inner core.Maroto
}
// NewMetricsDecorator is responsible to create the metrics decorator
// for the maroto instance.
func NewMetricsDecorator(inner core.Maroto) core.Maroto {
return &MetricsDecorator{
inner: inner,
}
}
// FitlnCurrentPage decoratess the FitlnCurrentPage method of maroto instance.
func (m *MetricsDecorator) FitlnCurrentPage(heightNewLine float64) bool {
return m.inner.FitlnCurrentPage(heightNewLine)
}
// GetCurrentConfig decorates the GetCurrentConfig method of maroto instance.
func (m *MetricsDecorator) GetCurrentConfig() *entity.Config {
return m.inner.GetCurrentConfig()
}
// Generate decorates the Generate method of maroto instance.
func (m *MetricsDecorator) Generate() (core.Document, error) {
var document core.Document
var err error
timeSpent := time.GetTimeSpent(func() {
document, err = m.inner.Generate()
})
m.generateTime = timeSpent
if err != nil {
return nil, err
}
bytes := document.GetBytes()
report := m.buildMetrics(len(bytes)).Normalize()
return core.NewPDF(bytes, report), nil
}
// AddPages decorates the AddPages method of maroto instance.
func (m *MetricsDecorator) AddPages(pages ...core.Page) {
timeSpent := time.GetTimeSpent(func() {
m.inner.AddPages(pages...)
})
m.addPageTime = append(m.addPageTime, timeSpent)
}
// AddRows decorates the AddRows method of maroto instance.
func (m *MetricsDecorator) AddRows(rows ...core.Row) {
timeSpent := time.GetTimeSpent(func() {
m.inner.AddRows(rows...)
})
m.addRowsTime = append(m.addRowsTime, timeSpent)
}
// AddRow decorates the AddRow method of maroto instance.
func (m *MetricsDecorator) AddRow(rowHeight float64, cols ...core.Col) core.Row {
var r core.Row
timeSpent := time.GetTimeSpent(func() {
r = m.inner.AddRow(rowHeight, cols...)
})
m.addRowTime = append(m.addRowTime, timeSpent)
return r
}
// AddRow decorates the AddRow method of maroto instance.
func (m *MetricsDecorator) AddAutoRow(cols ...core.Col) core.Row {
var r core.Row
timeSpent := time.GetTimeSpent(func() {
r = m.inner.AddAutoRow(cols...)
})
m.addAutoRowTime = append(m.addAutoRowTime, timeSpent)
return r
}
// RegisterHeader decorates the RegisterHeader method of maroto instance.
func (m *MetricsDecorator) RegisterHeader(rows ...core.Row) error {
var err error
timeSpent := time.GetTimeSpent(func() {
err = m.inner.RegisterHeader(rows...)
})
m.headerTime = timeSpent
return err
}
// RegisterFooter decorates the RegisterFooter method of maroto instance.
func (m *MetricsDecorator) RegisterFooter(rows ...core.Row) error {
var err error
timeSpent := time.GetTimeSpent(func() {
err = m.inner.RegisterFooter(rows...)
})
m.footerTime = timeSpent
return err
}
// GetStructure decorates the GetStructure method of maroto instance.
func (m *MetricsDecorator) GetStructure() *node.Node[core.Structure] {
var tree *node.Node[core.Structure]
timeSpent := time.GetTimeSpent(func() {
tree = m.inner.GetStructure()
})
m.structureTime = timeSpent
return tree
}
func (m *MetricsDecorator) buildMetrics(bytesSize int) *metrics.Report {
var timeMetrics []metrics.TimeMetric
if m.structureTime != nil {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "get_tree_structure",
Times: []*metrics.Time{m.structureTime},
Avg: m.structureTime,
})
}
if m.generateTime != nil {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "generate",
Times: []*metrics.Time{m.generateTime},
Avg: m.generateTime,
})
}
if m.headerTime != nil {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "header",
Times: []*metrics.Time{m.headerTime},
Avg: m.headerTime,
})
}
if m.footerTime != nil {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "footer",
Times: []*metrics.Time{m.footerTime},
Avg: m.footerTime,
})
}
if len(m.addPageTime) > 0 {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "add_page",
Times: m.addPageTime,
Avg: m.getAVG(m.addPageTime),
})
}
if len(m.addRowTime) > 0 {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "add_row",
Times: m.addRowTime,
Avg: m.getAVG(m.addRowTime),
})
}
if len(m.addRowsTime) > 0 {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "add_rows",
Times: m.addRowsTime,
Avg: m.getAVG(m.addRowsTime),
})
}
return &metrics.Report{
TimeMetrics: timeMetrics,
SizeMetric: metrics.SizeMetric{
Key: "file_size",
Size: metrics.Size{
Value: float64(bytesSize),
Scale: metrics.Byte,
},
},
}
}
func (m *MetricsDecorator) getAVG(times []*metrics.Time) *metrics.Time {
var sum float64
for _, time := range times {
sum += time.Value
}
return &metrics.Time{
Value: sum / float64(len(times)),
Scale: times[0].Scale,
}
}