Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathstring.go
More file actions
Latest commit
196 lines (186 loc) · 4.94 KB
/
Copy pathstring.go
File metadata and controls
196 lines (186 loc) · 4.94 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package devstatscode
import (
"bytes"
"crypto/sha1"
"encoding/csv"
"encoding/hex"
"fmt"
"io"
"math/rand"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
)
// 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')
funcPrepareQuickRangeQuery(sql, period, from, tostring) (string, string) {
start:=0
startPatt:="{{period:"
startPattLen:=len(startPatt)
endPatt:="}}"
endPattLen:=len(endPatt)
lenSQL:=len(sql)
res:=""
sHours:="0"
periodMode:=false
ifperiod!="" {
periodMode=true
sHours=IntervalHours(period)
} else {
iffrom!=""&&to!="" {
tFrom:=TimeParseAny(from)
tTo:=TimeParseAny(to)
from=ToYMDHMSDate(tFrom)
to=ToYMDHMSDate(tTo)
sHours=RangeHours(tFrom, tTo)
}
}
for {
idx1:=strings.Index(sql[start:], startPatt)
ifidx1==-1 {
break
}
idx2:=strings.Index(sql[start+idx1:], endPatt)
col:=sql[start+idx1+startPattLen : start+idx1+idx2]
res+=sql[start : start+idx1]
ifperiodMode {
res+=" ("+col+" >= now() - '"+period+"'::interval) "
} else {
iffrom==""||to=="" {
return"You need to provide either non-empty `period` or non empty `from` and `to`", sHours
}
res+=" ("+col+" >= '"+from+"' and "+col+" < '"+to+"') "
}
start+=idx1+idx2+endPattLen
}
res+=sql[start:lenSQL]
ifperiodMode {
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)
}
returnres, sHours
}
// SafeUTF8String - make sure string is UTF-8 valid
funcSafeUTF8String(inputstring) string {
returnstring(bytes.ToValidUTF8([]byte(strings.Replace(input, "\x00", "", -1)), []byte("")))
}
// Slugify replace all whitespace with "-", remove all non-word letters downcase
funcSlugify(argstring) string {
re:=regexp.MustCompile(`[^\w-]+`)
arg=re.ReplaceAllLiteralString(arg, "-")
returnstrings.ToLower(arg)
}
// GetHidden - return list of shas to replace
funcGetHidden(ctx*Ctx, configFilestring) (shaMapmap[string]string) {
shaMap=make(map[string]string)
f, err:=os.Open(configFile)
iferr!=nil {
f, err=os.Open(ctx.DataDir+"/"+configFile)
}
iferr==nil {
deferfunc() { _=f.Close() }()
reader:=csv.NewReader(f)
for {
row, err:=reader.Read()
iferr==io.EOF {
break
} elseiferr!=nil {
FatalOnError(err)
}
sha:=row[0]
ifsha=="sha1" {
continue
}
shaMap[sha] ="anon-"+sha
}
}
return
}
// MaybeHideFunc - use closure as a data storage
funcMaybeHideFunc(shasmap[string]string) (ffunc(string) string) {
cache:=make(map[string]string)
f=func(argstring) string {
varshastring
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]
ifok {
returnanon
}
returnarg
}
returnf
}
// MaybeHideFuncTS - use closure as a data storage - thread safe
funcMaybeHideFuncTS(shasmap[string]string) (ffunc(string) string) {
cache:=make(map[string]string)
mtx:=&sync.RWMutex{}
smtx:=&sync.Mutex{}
f=func(argstring) string {
varshastring
mtx.RLock()
sha, ok:=cache[arg]
mtx.RUnlock()
if!ok {
hash:=sha1.New()
_, err:=hash.Write([]byte(arg))
FatalOnError(err)
sha=hex.EncodeToString(hash.Sum(nil))
mtx.Lock()
cache[arg] =sha
mtx.Unlock()
}
smtx.Lock()
anon, ok:=shas[sha]
smtx.Unlock()
ifok {
returnanon
}
returnarg
}
returnf
}
// RandString - return random string
funcRandString() string {
returnfmt.Sprintf("%x", rand.Uint64())
}
// FormatRawBytes - format []uint8 string
funcFormatRawBytes(rawB []uint8) string {
raw:=fmt.Sprintf("%v", reflect.ValueOf(rawB))
op:=strings.Index(raw, "[") +1
cl:=strings.Index(raw, "]")
ary:=strings.Split(raw[op:cl], " ")
formatted:=""
for_, s:=rangeary {
b, _:=strconv.ParseInt(s, 10, 32)
formatted+=fmt.Sprintf("%02x", b)
}
returnfmt.Sprintf("%T(%d)", rawB, len(rawB)) +":"+formatted+":"+fmt.Sprintf("%+v", rawB)
}
// FormatRawInterface - format raw string that is probably value or pointer to either []uint8 or sql.RawBytes
funcFormatRawInterface(rawIinterface{}) string {
raw:=fmt.Sprintf("%v", reflect.ValueOf(rawI))
op:=strings.Index(raw, "[") +1
cl:=strings.Index(raw, "]")
ary:=strings.Split(raw[op:cl], " ")
formatted:=""
for_, s:=rangeary {
b, _:=strconv.ParseInt(s, 10, 32)
formatted+=fmt.Sprintf("%02x", b)
}
returnfmt.Sprintf("%T", rawI) +":"+formatted+":"+fmt.Sprintf("%+v", rawI)
}