forked from cncf/devstatscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
108 lines (103 loc) · 2.68 KB
/
string.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
package devstatscode
import (
"crypto/sha1"
"encoding/csv"
"encoding/hex"
"io"
"os"
"regexp"
"strings"
)
// PrepareQuickRangeQuery Perpares query using either ready `period` string or using `from` and `to` strings
// Values to replace are specially encoded {{period:alias.column}}
// Can either replace with: (alias.column >= now() - 'period'::interval)
// Or (alias.column >= 'from' and alias.column < 'to')
func PrepareQuickRangeQuery(sql, period, from, to string) string {
start := 0
startPatt := "{{period:"
startPattLen := len(startPatt)
endPatt := "}}"
endPattLen := len(endPatt)
lenSQL := len(sql)
res := ""
periodMode := false
if period != "" {
periodMode = true
}
for {
idx1 := strings.Index(sql[start:], startPatt)
if idx1 == -1 {
break
}
idx2 := strings.Index(sql[start+idx1:], endPatt)
col := sql[start+idx1+startPattLen : start+idx1+idx2]
res += sql[start : start+idx1]
if periodMode {
res += " (" + col + " >= now() - '" + period + "'::interval) "
} else {
if from == "" || to == "" {
return "You need to provide either non-empty `period` or non empty `from` and `to`"
}
res += " (" + col + " >= '" + from + "' and " + col + " < '" + to + "') "
}
start += idx1 + idx2 + endPattLen
}
res += sql[start:lenSQL]
if periodMode {
res = strings.Replace(res, "{{from}}", "(now() -'"+period+"'::interval)", -1)
res = strings.Replace(res, "{{to}}", "(now())", -1)
} else {
res = strings.Replace(res, "{{from}}", "'"+from+"'", -1)
res = strings.Replace(res, "{{to}}", "'"+to+"'", -1)
}
return res
}
// Slugify replace all whitespace with "-", remove all non-word letters downcase
func Slugify(arg string) string {
re := regexp.MustCompile(`[^\w-]+`)
arg = re.ReplaceAllLiteralString(arg, "-")
return strings.ToLower(arg)
}
// GetHidden - return list of shas to replace
func GetHidden(configFile string) map[string]string {
shaMap := make(map[string]string)
f, err := os.Open(configFile)
if err == nil {
defer func() { _ = f.Close() }()
reader := csv.NewReader(f)
for {
row, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
FatalOnError(err)
}
sha := row[0]
if sha == "sha1" {
continue
}
shaMap[sha] = "anon-" + sha
}
}
return shaMap
}
// MaybeHideFunc - use closure as a data storage
func MaybeHideFunc(shas map[string]string) func(string) string {
cache := make(map[string]string)
return func(arg string) string {
var sha string
sha, ok := cache[arg]
if !ok {
hash := sha1.New()
_, err := hash.Write([]byte(arg))
FatalOnError(err)
sha = hex.EncodeToString(hash.Sum(nil))
cache[arg] = sha
}
anon, ok := shas[sha]
if ok {
return anon
}
return arg
}
}