-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwrangler.go
106 lines (82 loc) · 2.33 KB
/
wrangler.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
package main
import (
"strconv"
"strings"
"time"
"github.com/SmallAffairCollective/json_path_scanner"
"github.com/mediocregopher/radix.v2/redis"
)
func getFields(jsonMap map[string]interface{}) []string {
var keys []string
metrics := getMetrics(jsonMap)
for k := range metrics {
keys = append(keys, k)
}
return keys
}
func getMetrics(jsonMap map[string]interface{}) map[string]float64 {
metrics := make(map[string]float64)
ch := make(chan *json_path_scanner.PathValue)
go func() {
// TODO try/catch if fails check if it's flat, otherwise fail
json_path_scanner.Scan(jsonMap, ch)
}()
for p := range ch {
if p.Value != nil {
key := p.Path[2:len(p.Path)]
// format path to make it cleaner
key = strings.Replace(key, "[", ".", -1)
key = strings.Replace(key, "]", "", -1)
key = strings.Replace(key, "'", "", -1)
if value, ok := p.Value.(float64); ok {
metrics[key] = value
} else if value, ok := p.Value.(string); ok {
if v, err := strconv.Atoi(value); err == nil {
metrics[key] = float64(v)
}
}
}
}
return metrics
}
func storeMetrics(url string, conn *redis.Client, metrics map[string]float64) bool {
unixTime := time.Now().Unix()
resp := conn.Cmd("SADD", url, strconv.FormatInt(unixTime, 10))
if resp.Err != nil {
return false
}
resp = conn.Cmd("HMSET", url+strconv.FormatInt(unixTime, 10), metrics)
if resp.Err != nil {
return false
}
return true
}
func getStoredMetricMatrix(url string, conn *redis.Client) map[string]map[string]float64 {
resp := conn.Cmd("SMEMBERS", url)
check(resp.Err)
l, _ := resp.List()
matrix := make(map[string]map[string]float64)
for _, item := range l {
resp = conn.Cmd("HGETALL", url+item)
check(resp.Err)
matrix[url+item] = make(map[string]float64)
m, _ := resp.Map()
for key := range m {
value, _ := strconv.ParseFloat(m[key], 64)
matrix[url+item][key] = value
}
}
return matrix
}
func flattenMetricMatrix(url string, matrix map[string]map[string]float64) map[string]map[string][]float64 {
flattenedMatrix := make(map[string]map[string][]float64)
flattenedMatrix[url] = make(map[string][]float64)
for item := range matrix {
if strings.HasPrefix(item, url) {
for field := range matrix[item] {
flattenedMatrix[url][field] = append(flattenedMatrix[url][field], matrix[item][field])
}
}
}
return flattenedMatrix
}