-
Notifications
You must be signed in to change notification settings - Fork 0
/
percentiles_stat.go
84 lines (76 loc) · 1.78 KB
/
percentiles_stat.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
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Wrangle Ltd
package diffprof
import (
"github.com/wrgl/wrgl/pkg/objects"
)
type PercentileDiff struct {
Old float64 `json:"old"`
New float64 `json:"new"`
}
func comparePercentiles(newP, oldP []float64) []*PercentileDiff {
result := []*PercentileDiff{}
for _, f := range newP {
result = append(result, &PercentileDiff{
New: f,
})
}
if len(newP) == 0 {
for _, f := range oldP {
result = append(result, &PercentileDiff{
Old: f,
})
}
} else if len(oldP) == len(newP) {
for i, f := range oldP {
result[i].Old = f
}
}
return result
}
func (s *PercentileDiff) Unchanged() bool {
return s.Old == s.New
}
type PercentilesStat struct {
Name string `json:"name"`
ShortName string `json:"shortName"`
NewAddition bool `json:"newAddition,omitempty"`
Removed bool `json:"removed,omitempty"`
Values []*PercentileDiff `json:"values"`
}
func percentilesStatFactory(name, sname string, getField func(col *objects.ColumnProfile) []float64) statDiffFactory {
return func(newTblProf, oldTblProf *objects.TableProfile, newColProf, oldColProf *objects.ColumnProfile) interface{} {
sd := &PercentilesStat{
Name: name,
ShortName: sname,
}
var ov, nv []float64
if oldColProf != nil {
ov = getField(oldColProf)
}
if newColProf != nil {
nv = getField(newColProf)
}
if nv == nil {
if ov == nil {
return nil
}
sd.Removed = true
} else if ov == nil {
sd.NewAddition = true
}
sd.Values = comparePercentiles(nv, ov)
return sd
}
}
func (s *PercentilesStat) Unchanged() bool {
if s.NewAddition || s.Removed {
return false
}
for _, v := range s.Values {
if !v.Unchanged() {
return false
}
}
return true
}