This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathgraphite_req.go
160 lines (148 loc) · 3.87 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
package api
import (
"fmt"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/expr"
)
// ReqMap is a map of requests of data,
// it has single requests for which no pre-normalization effort will be performed, and
// requests are that can be pre-normalized together to the same resolution, bundled by their PNGroup
type ReqMap struct {
single []models.Req
pngroups map[expr.PNGroup][]models.Req
cnt uint32
}
func NewReqMap() *ReqMap {
return &ReqMap{
pngroups: make(map[expr.PNGroup][]models.Req),
}
}
func (r *ReqMap) Add(req models.Req, group expr.PNGroup) {
r.cnt++
if group == 0 {
r.single = append(r.single, req)
}
r.pngroups[group] = append(r.pngroups[group], req)
}
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:", i)
for _, r := range reqs {
out += " " + r.DebugString() + "\n"
}
}
out += " Single:\n"
for _, r := range r.single {
out += " " + r.DebugString() + "\n"
}
return out
}
// PNGroupSplit embodies a PNGroup broken down by whether requests are MDP-optimizable
type PNGroupSplit struct {
mdpyes []models.Req // MDP-optimizable requests
mdpno []models.Req // not MDP-optimizable reqs
}
// ReqsPlan holds requests that have been planned
type ReqsPlan struct {
pngroups map[expr.PNGroup]PNGroupSplit
single PNGroupSplit
cnt uint32
}
func NewReqsPlan(reqs ReqMap) ReqsPlan {
rp := ReqsPlan{
pngroups: make(map[models.PNGroup]PNGroupSplit),
cnt: reqs.cnt,
}
for group, groupReqs := range reqs.pngroups {
var split PNGroupSplit
for _, req := range groupReqs {
if req.MaxPoints > 0 {
split.mdpyes = append(split.mdpyes, req)
} else {
split.mdpno = append(split.mdpno, req)
}
}
rp.pngroups[group] = split
}
for _, req := range reqs.single {
if req.MaxPoints > 0 {
rp.single.mdpyes = append(rp.single.mdpyes, req)
} else {
rp.single.mdpno = append(rp.single.mdpno, req)
}
}
return rp
}
func (rp ReqsPlan) PointsFetch() uint32 {
var cnt uint32
for _, r := range rp.single.mdpyes {
cnt += r.PointsFetch()
}
for _, r := range rp.single.mdpno {
cnt += r.PointsFetch()
}
for _, split := range rp.pngroups {
for _, r := range split.mdpyes {
cnt += r.PointsFetch()
}
for _, r := range split.mdpno {
cnt += r.PointsFetch()
}
}
return cnt
}
func (rp ReqsPlan) Dump() string {
out := fmt.Sprintf("ReqsPlan (%d entries):\n", rp.cnt)
out += " Groups:\n"
for i, split := range rp.pngroups {
out += fmt.Sprintf(" * group %d\nMDP-yes:\n", i)
for _, r := range split.mdpyes {
out += " " + r.DebugString() + "\n"
}
out += " MDP-no:\n"
for _, r := range split.mdpno {
out += " " + r.DebugString() + "\n"
}
}
out += " Single MDP-yes:\n"
for _, r := range rp.single.mdpyes {
out += " " + r.DebugString() + "\n"
}
out += " Single MDP-no:\n"
for _, r := range rp.single.mdpno {
out += " " + r.DebugString() + "\n"
}
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 _, r := range rp.single.mdpyes {
cnt += r.PointsReturn(planMDP)
}
for _, r := range rp.single.mdpno {
cnt += r.PointsReturn(planMDP)
}
for _, split := range rp.pngroups {
for _, r := range split.mdpyes {
cnt += r.PointsReturn(planMDP)
}
for _, r := range split.mdpno {
cnt += r.PointsReturn(planMDP)
}
}
return cnt
}
func (rp ReqsPlan) List() []models.Req {
l := make([]models.Req, 0, rp.cnt)
l = append(l, rp.single.mdpno...)
l = append(l, rp.single.mdpyes...)
for _, g := range rp.pngroups {
l = append(l, g.mdpno...)
l = append(l, g.mdpyes...)
}
return l
}