This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
graphite_req.go
256 lines (233 loc) · 6.25 KB
/
graphite_req.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
246
247
248
249
250
251
252
253
254
255
256
package api
import (
"fmt"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/archives"
)
// ReqMap is a map of requests of data,
// it has single requests for which no pre-normalization effort will be performed, and
// requests that can be pre-normalized together to the same resolution, bundled by their PNGroup
type ReqMap struct {
single []models.Req
pngroups map[models.PNGroup][]models.Req
cnt uint32
}
func NewReqMap() *ReqMap {
return &ReqMap{
pngroups: make(map[models.PNGroup][]models.Req),
}
}
// Add adds a models.Req to the ReqMap
func (r *ReqMap) Add(req models.Req) {
r.cnt++
if req.PNGroup == 0 {
r.single = append(r.single, req)
return
}
r.pngroups[req.PNGroup] = append(r.pngroups[req.PNGroup], req)
}
// Dump provides a human readable string representation of the ReqsMap
func (r ReqMap) Dump() string {
out := fmt.Sprintf("ReqsMap (%d entries):\n", r.cnt)
out += " Groups:\n"
for i, reqs := range r.pngroups {
out += fmt.Sprintf(" * group %d:\n", i)
for _, r := range reqs {
out += " " + r.DebugString() + "\n"
}
}
out += " Single:\n"
for _, r := range r.single {
out += " " + r.DebugString() + "\n"
}
return out
}
// Requests indexed by their ArchivesID
type ReqsByArchives [][]models.Req
func (rba *ReqsByArchives) Add(idx int, r models.Req) {
for len(*rba) <= idx {
*rba = append(*rba, nil)
}
(*rba)[idx] = append((*rba)[idx], r)
}
// OutInterval returns the outinterval of the ReqsByArchives
// this assumes that all requests have been planned to a consistent out interval, of course.
func (rba ReqsByArchives) OutInterval() uint32 {
for _, reqs := range rba {
if len(reqs) != 0 {
return reqs[0].OutInterval
}
}
return 0
}
func (rba ReqsByArchives) HasData() bool {
for _, reqs := range rba {
if len(reqs) != 0 {
return true
}
}
return false
}
func (rba ReqsByArchives) Len() int {
var cnt int
for _, reqs := range rba {
cnt += len(reqs)
}
return cnt
}
// GroupData embodies a set of requests broken down by whether requests are MDP-optimizable, and by archivesID
type GroupData struct {
mdpyes ReqsByArchives // MDP-optimizable requests
mdpno ReqsByArchives // not MDP-optimizable reqs
}
func NewGroupData() GroupData {
return GroupData{}
}
func (gd GroupData) Len() int {
return gd.mdpno.Len() + gd.mdpyes.Len()
}
// ReqsPlan holds requests that have been planned, broken down by PNGroup and MDP-optimizability
type ReqsPlan struct {
pngroups map[models.PNGroup]GroupData
single GroupData
cnt uint32
archives archives.Index
}
// NewReqsPlan generates a ReqsPlan based on the provided ReqMap.
func NewReqsPlan(reqs ReqMap) ReqsPlan {
rp := ReqsPlan{
pngroups: make(map[models.PNGroup]GroupData),
single: NewGroupData(),
cnt: reqs.cnt,
archives: archives.NewIndex(),
}
for group, groupReqs := range reqs.pngroups {
data := NewGroupData()
for _, req := range groupReqs {
archivesID := rp.archives.ArchivesID(req.RawInterval, req.SchemaId)
if req.MaxPoints > 0 {
data.mdpyes.Add(archivesID, req)
} else {
data.mdpno.Add(archivesID, req)
}
}
rp.pngroups[group] = data
}
for _, req := range reqs.single {
archivesID := rp.archives.ArchivesID(req.RawInterval, req.SchemaId)
if req.MaxPoints > 0 {
rp.single.mdpyes.Add(archivesID, req)
} else {
rp.single.mdpno.Add(archivesID, req)
}
}
return rp
}
// PointsFetch returns how many points this plan will fetch when executed
func (rp ReqsPlan) PointsFetch() uint32 {
var cnt uint32
for _, rba := range rp.single.mdpyes {
for _, req := range rba {
cnt += req.PointsFetch()
}
}
for _, rba := range rp.single.mdpno {
for _, req := range rba {
cnt += req.PointsFetch()
}
}
for _, data := range rp.pngroups {
for _, rba := range data.mdpyes {
for _, req := range rba {
cnt += req.PointsFetch()
}
}
for _, rba := range data.mdpno {
for _, req := range rba {
cnt += req.PointsFetch()
}
}
}
return cnt
}
// Dump provides a human readable string representation of the ReqsPlan
func (rp ReqsPlan) Dump() string {
out := fmt.Sprintf("ReqsPlan (%d entries):\n", rp.cnt)
out += " # Groups:\n"
for i, data := range rp.pngroups {
out += fmt.Sprintf(" ## group %d\n", i)
out += " ### MDP-yes:\n"
for archivesID, reqs := range data.mdpyes {
for _, req := range reqs {
out += fmt.Sprintf(" [%v] %s\n", rp.archives.Get(archivesID), req.DebugString())
}
}
out += " ### MDP-no:\n"
for archivesID, reqs := range data.mdpno {
for _, req := range reqs {
out += fmt.Sprintf(" [%v] %s\n", rp.archives.Get(archivesID), req.DebugString())
}
}
}
out += " # Single\n"
out += " ## MDP-yes:\n"
for archivesID, reqs := range rp.single.mdpyes {
for _, req := range reqs {
out += fmt.Sprintf(" [%v] %s\n", rp.archives.Get(archivesID), req.DebugString())
}
}
out += " ## MDP-no:\n"
for archivesID, reqs := range rp.single.mdpno {
for _, req := range reqs {
out += fmt.Sprintf(" [%v] %s\n", rp.archives.Get(archivesID), req.DebugString())
}
}
return out
}
// PointsReturn estimates the amount of points that will be returned for this request
// best effort: not aware of summarize(), aggregation functions, runtime normalization. but does account for runtime consolidation
func (rp ReqsPlan) PointsReturn(planMDP uint32) uint32 {
var cnt uint32
for _, rba := range rp.single.mdpyes {
for _, req := range rba {
cnt += req.PointsReturn(planMDP)
}
}
for _, rba := range rp.single.mdpno {
for _, req := range rba {
cnt += req.PointsReturn(planMDP)
}
}
for _, data := range rp.pngroups {
for _, rba := range data.mdpyes {
for _, req := range rba {
cnt += req.PointsReturn(planMDP)
}
}
for _, rba := range data.mdpno {
for _, req := range rba {
cnt += req.PointsReturn(planMDP)
}
}
}
return cnt
}
// List returns the requests contained within the plan as a slice
func (rp ReqsPlan) List() []models.Req {
l := make([]models.Req, 0, rp.cnt)
for _, reqs := range rp.single.mdpno {
l = append(l, reqs...)
}
for _, reqs := range rp.single.mdpyes {
l = append(l, reqs...)
}
for _, data := range rp.pngroups {
for _, reqs := range data.mdpno {
l = append(l, reqs...)
}
for _, reqs := range data.mdpyes {
l = append(l, reqs...)
}
}
return l
}