-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathutils.go
More file actions
65 lines (52 loc) · 1.62 KB
/
Copy pathutils.go
File metadata and controls
65 lines (52 loc) · 1.62 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
package main
import (
"encoding/json"
"regexp"
"strings"
"time"
"github.com/spf13/viper"
)
func GetTimeRange() (now time.Time, now1mAgo time.Time) {
now = time.Now().Add(-time.Duration(viper.GetInt("scrape_delay")) * time.Second).UTC()
s := 60 * time.Second
now = now.Truncate(s)
now1mAgo = now.Add(-60 * time.Second)
return now, now1mAgo
}
func GetMonthRange() (now time.Time, monthStart time.Time, nextMonthStart time.Time) {
now = time.Now().Add(-time.Duration(viper.GetInt("scrape_delay")) * time.Second).UTC()
now = now.Truncate(time.Minute)
monthStart = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
nextMonthStart = monthStart.AddDate(0, 1, 0)
return now, monthStart, nextMonthStart
}
func jsonStringToMap(fields string) (map[string]interface{}, error) {
var extraFields map[string]interface{}
err := json.Unmarshal([]byte(fields), &extraFields)
return extraFields, err
}
var (
numericIDPattern = regexp.MustCompile(`^[0-9]+$`)
uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
hexIDPattern = regexp.MustCompile(`^[0-9a-fA-F]{8,}$`)
)
func normalizePath(path string) string {
if path == "" || path == "/" {
return path
}
path = strings.Split(path, "?")[0]
segments := strings.Split(path, "/")
for i, segment := range segments {
if segment == "" {
continue
}
if numericIDPattern.MatchString(segment) {
segments[i] = ":id"
} else if uuidPattern.MatchString(segment) {
segments[i] = ":uuid"
} else if hexIDPattern.MatchString(segment) {
segments[i] = ":id"
}
}
return strings.Join(segments, "/")
}