-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
156 lines (133 loc) · 3.13 KB
/
main.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/kataras/iris"
"github.com/urfave/cli"
)
func main() {
var delay int
var iterations int
var redisHost string
var serve bool
var url string
app := cli.NewApp()
app.Name = "jsonchart"
app.Usage = "generate charts from JSON endpoints"
app.Version = "0.1.0"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "delay, d",
Value: 1,
Usage: "delay in `SECONDS` between polling URL",
Destination: &delay,
},
cli.IntFlag{
Name: "iterations, i",
Value: 1,
Usage: "number of times to poll URL, -1 runs indefinitely",
Destination: &iterations,
},
cli.StringFlag{
Name: "redis, r",
Value: "redis",
Usage: "redis `HOST` to connect to",
Destination: &redisHost,
},
cli.BoolFlag{
Name: "serve, s",
Usage: "startup webserver to serve up charts",
Destination: &serve,
},
cli.StringFlag{
Name: "url, u",
Usage: "`URL` to poll from",
Destination: &url,
},
}
app.Action = func(c *cli.Context) error {
if url != "" {
url, delay, er := validateArgs(url, delay)
if er != nil {
fmt.Println(er.Error())
return er
}
if serve {
go alwaysBeGettin(url, delay, iterations, redisHost)
} else {
alwaysBeGettin(url, delay, iterations, redisHost)
}
} else {
// TODO write out html/js
fmt.Println("no url provided, nothing new to get")
}
if serve {
serveWeb()
}
return nil
}
app.Run(os.Args)
}
func serveWeb() {
app := iris.Default()
// handle js and html files
app.StaticWeb("/", "./www")
app.Run(iris.Addr(":8080"))
}
func alwaysBeGettin(url string, delay int, iterations int, redisHost string) {
flattenedMatrix := make(map[string]map[string][]float64)
fields := getFields(fetchJSON(url))
fields = queryUser(fields)
fmt.Println(fields)
i := iterations
counter := 1
for i != 0 {
jsonMap := fetchJSON(url)
metrics := getMetrics(jsonMap)
conn := connectRedis(redisHost)
storeMetrics(url, conn, metrics)
matrix := getStoredMetricMatrix(url, conn)
flattenedMatrix = flattenMetricMatrix(url, matrix)
defer conn.Close()
if iterations == -1 {
fmt.Println("completed iteration: ", counter, "/ inf")
} else {
fmt.Println("completed iteration: ", counter, "/", iterations)
}
time.Sleep(time.Second * time.Duration(delay))
writeGChartHTML()
writeGChartJs(url, delay, counter, flattenedMatrix[url])
i--
counter++
}
}
func validateArgs(url string, delay int) (string, int, error) {
if delay < 1 {
return "", -1, errors.New("error: delay must be a number greater than 0")
}
// Validate URL
url, er := parseURL(url)
if er != nil {
return "", -1, errors.New(er.Error())
}
// Make sure data can be retrieved from URL
s, er := fetchURLData(url)
if er != nil {
return "", -1, errors.New(er.Error())
}
// Validate JSON
var js map[string]interface{}
if json.Unmarshal([]byte(s), &js) != nil {
return "", -1, errors.New("error: url does not return valid json")
}
// TODO validate redisHost
return url, delay, nil
}
func check(e error) {
if e != nil {
panic(e)
}
}