-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvm_functions.go
More file actions
182 lines (149 loc) · 5.48 KB
/
vm_functions.go
File metadata and controls
182 lines (149 loc) · 5.48 KB
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
package promsketch
import (
"context"
"math"
)
type VMFunctionCall func(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64
// VMFunctionCalls is a list of all funcVMtions supported by PromQL, including their types.
var VMFunctionCalls = map[string]VMFunctionCall{
"change_over_time": funcVMChangeOverTime,
"avg_over_time": funcVMAvgOverTime,
"count_over_time": funcVMCountOverTime,
"entropy_over_time": funcVMEntropyOverTime,
"max_over_time": funcVMMaxOverTime,
"min_over_time": funcVMMinOverTime,
"stddev_over_time": funcVMStddevOverTime,
"stdvar_over_time": funcVMStdvarOverTime,
"sum_over_time": funcVMSumOverTime,
"sum2_over_time": funcVMSum2OverTime,
"distinct_over_time": funcVMCardOverTime,
"l1_over_time": funcVML1OverTime,
"l2_over_time": funcVML2OverTime,
"quantile_over_time": funcVMQuantileOverTime,
// "quantiles_over_time": funcVMQuantilesOverTime,
}
// TODO: add last item value in the change data structure
func funcVMChangeOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
count := sketchIns.sampling.QueryCount(t1, t2)
return count
}
func funcVMAvgOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
// fmt.Println("in VM avg_over_time", sketchIns.sampling.Sampling_rate, len(sketchIns.sampling.Arr), sketchIns.sampling.Max_size)
// fmt.Println("in VM avg_over_time", sketchIns.sampling.Time_window_size, sketchIns.sampling.GetMinTime(), sketchIns.sampling.GetMaxTime(), t1, t2)
avg := sketchIns.sampling.QueryAvg(t1, t2)
return avg
}
func funcVMSumOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
sum := sketchIns.sampling.QuerySum(t1, t2)
return sum
}
func funcVMSum2OverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
sum2 := sketchIns.sampling.QuerySum2(t1, t2)
return sum2
}
func funcVMCountOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
count := sketchIns.sampling.QueryCount(t1, t2)
return count
}
func funcVMStddevOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
// count := sketchIns.sampling.QueryCount(t1, t2)
// sum := sketchIns.sampling.QuerySum(t1, t2)
// sum2 := sketchIns.sampling.QuerySum2(t1, t2)
// stddev := math.Sqrt(sum2/count - math.Pow(sum/count, 2))
stddev := sketchIns.sampling.QueryStddev(t1, t2)
return stddev
}
func funcVMStdvarOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
// count := sketchIns.sampling.QueryCount(t1, t2)
// sum := sketchIns.sampling.QuerySum(t1, t2)
// sum2 := sketchIns.sampling.QuerySum2(t1, t2)
// stdvar := sum2/count - math.Pow(sum/count, 2)
stdvar := sketchIns.sampling.QueryStdvar(t1, t2)
return stdvar
}
func funcVMEntropyOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
merged_univ, m, n, err := sketchIns.ehuniv.QueryIntervalMergeUniv(t1, t2, t)
if err != nil {
return 0
}
var entropy float64 = 0
if merged_univ != nil && m == nil {
entropy = merged_univ.calcEntropy()
} else {
entropy = calc_entropy_map(m, n)
}
return entropy
}
func funcVMCardOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
merged_univ, m, _, err := sketchIns.ehuniv.QueryIntervalMergeUniv(t1, t2, t)
if err != nil {
return 0
}
var card float64 = 0
if merged_univ != nil && m == nil {
card = merged_univ.calcCard()
} else {
card = calc_distinct_map(m)
}
return card
}
func funcVML1OverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
merged_univ, m, _, err := sketchIns.ehuniv.QueryIntervalMergeUniv(t1, t2, t)
if err != nil {
return 0
}
var l1 float64 = 0
if merged_univ != nil && m == nil {
l1 = merged_univ.calcL1()
} else {
l1 = calc_l1_map(m)
}
return l1
}
func funcVML2OverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
merged_univ, m, _, err := sketchIns.ehuniv.QueryIntervalMergeUniv(t1, t2, t)
if err != nil {
return 0
}
var l2 float64 = 0
if merged_univ != nil && m == nil {
l2 = merged_univ.calcL2()
} else {
l2 = calc_l2_map(m)
}
return l2
}
func funcVMQuantileOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
if len(c) < 1 {
return math.NaN()
}
phi := c[0]
merged_kll := sketchIns.ehkll.QueryIntervalMergeKLL(t1, t2)
cdf := merged_kll.CDF()
q_value := cdf.Query(phi)
return q_value
}
func funcVMQuantilesOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) []float64 {
if len(c) < 1 {
return []float64{math.NaN()}
}
merged_kll := sketchIns.ehkll.QueryIntervalMergeKLL(t1, t2)
cdf := merged_kll.CDF()
q_values := make([]float64, 0)
for _, phi := range c {
q_values = append(q_values, cdf.Query(phi))
}
return q_values
}
func funcVMMinOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
merged_kll := sketchIns.ehkll.QueryIntervalMergeKLL(t1, t2)
cdf := merged_kll.CDF()
q_value := cdf.Query(0)
return q_value
}
func funcVMMaxOverTime(ctx context.Context, sketchIns *SketchInstances, c []float64, t1, t2, t int64) float64 {
merged_kll := sketchIns.ehkll.QueryIntervalMergeKLL(t1, t2)
cdf := merged_kll.CDF()
q_value := cdf.Query(1)
return q_value
}