-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
248 lines (216 loc) · 5.2 KB
/
db.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"database/sql"
"fmt"
"os"
"sort"
"time"
_ "github.com/go-sql-driver/mysql"
)
var (
db *sql.DB
err error
)
func init() {
db, _ = sql.Open("mysql", fmt.Sprintf("%v:%v@(comm_db:3306)/%v",
os.Getenv("DB_USER"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_DB")))
// wait database container to start
for {
err := db.Ping()
if err == nil {
break
}
fmt.Println(err)
time.Sleep(2 * time.Second)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS TOPOLOGY_NODES (
TAG VARCHAR(64) NOT NULL,
NAME VARCHAR(16) NOT NULL,
POS_X INT NOT NULL,
POS_Y INT NOT NULL);`)
if err != nil {
panic(err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS TOPOLOGY_EDGES (
TAG VARCHAR(64) NOT NULL,
SOURCE VARCHAR(16) NOT NULL,
TARGET VARCHAR(16) NOT NULL);`)
if err != nil {
panic(err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS STATISTICS_IO (
TIMESTAMP BIGINT,
NAME VARCHAR(16) NOT NULL,
TX INT,
RX INT);`)
if err != nil {
panic(err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS STATISTICS_DELAY (
TIMESTAMP BIGINT,
NAME VARCHAR(16) NOT NULL,
SOURCE VARCHAR(16) NOT NULL,
SEQ INT,
DELAY DOUBLE);`)
if err != nil {
panic(err)
}
_, err = db.Exec(`DELETE FROM STATISTICS_IO WHERE 1`)
if err != nil {
panic(err)
}
_, err = db.Exec(`DELETE FROM STATISTICS_DELAY WHERE 1`)
if err != nil {
panic(err)
}
}
func insertTopo(topo TopologyData) {
db.Exec(fmt.Sprintf("DELETE FROM TOPOLOGY_NODES WHERE TAG=\"%s\"", topo.Tag))
db.Exec(fmt.Sprintf("DELETE FROM TOPOLOGY_EDGES WHERE TAG=\"%s\"", topo.Tag))
stmtNodes, err := db.Prepare(`INSERT INTO TOPOLOGY_NODES (TAG, NAME, POS_X, POS_Y) VALUES (?, ?, ?, ?);`)
if err != nil {
fmt.Println(err)
return
}
stmtEdges, err := db.Prepare(`INSERT INTO TOPOLOGY_EDGES (TAG, SOURCE, TARGET) VALUES (?, ?, ?);`)
if err != nil {
fmt.Println(err)
return
}
defer stmtNodes.Close()
defer stmtEdges.Close()
for _, n := range topo.Nodes {
_, err = stmtNodes.Exec(topo.Tag, n.Name, n.Position[0], n.Position[1])
if err != nil {
fmt.Println(err)
}
}
for _, e := range topo.Edges {
_, err = stmtEdges.Exec(topo.Tag, e[0], e[1])
if err != nil {
fmt.Println(err)
}
}
}
func queryTopo(tag string) (TopologyData, error) {
var topo TopologyData
var node TopologyNode
var edge = [2]string{}
var rowsNodes *sql.Rows
var rowsEdges *sql.Rows
topo.Tag = tag
rowsNodes, err := db.Query(`SELECT NAME,POS_X, POS_Y FROM TOPOLOGY_NODES where TAG=?`, tag)
if err != nil {
return topo, err
}
defer rowsNodes.Close()
for rowsNodes.Next() {
rowsNodes.Scan(&node.Name, &node.Position[0], &node.Position[1])
topo.Nodes = append(topo.Nodes, node)
}
rowsEdges, err = db.Query(`SELECT SOURCE, TARGET FROM TOPOLOGY_EDGES where TAG=?`, tag)
if err != nil {
return topo, err
}
defer rowsEdges.Close()
for rowsEdges.Next() {
rowsEdges.Scan(&edge[0], &edge[1])
topo.Edges = append(topo.Edges, edge)
}
return topo, nil
}
func queryTopoTags() ([]string, error) {
var tags []string
var rows *sql.Rows
rows, err = db.Query(`SELECT TAG FROM TOPOLOGY_NODES GROUP BY TAG`)
if err != nil {
return tags, err
}
defer rows.Close()
for rows.Next() {
var tag string
rows.Scan(&tag)
tags = append(tags, tag)
}
return tags, nil
}
func saveStatsIO(data map[string][2]int) {
t := time.Now()
timestamp := t.UnixMilli()
stmt, err := db.Prepare(`INSERT INTO STATISTICS_IO (TIMESTAMP, NAME, TX, RX) VALUES (?, ?, ?, ?);`)
if err != nil {
fmt.Println(err)
return
}
defer stmt.Close()
for name, value := range data {
_, err = stmt.Exec(timestamp, name, value[0], value[1])
if err != nil {
fmt.Println(err)
}
}
}
func saveStatsDelay(name, source string, seq int32, delay float64) {
t := time.Now()
timestamp := t.UnixMilli()
stmt, err := db.Prepare(`INSERT INTO STATISTICS_DELAY (TIMESTAMP, NAME, SOURCE, SEQ, DELAY) VALUES (?, ?, ?, ?, ?);`)
if err != nil {
fmt.Println(err)
return
}
defer stmt.Close()
_, err = stmt.Exec(timestamp, name, source, seq, delay)
if err != nil {
fmt.Println(err)
}
}
// timestamp, tx, rx
func queryStatsIOByName(name string) ([][3]int, error) {
var stats [][3]int
var rows *sql.Rows
rows, err = db.Query(`SELECT TIMESTAMP,TX,RX FROM STATISTICS_IO WHERE NAME=?`, name)
if err != nil {
return stats, err
}
defer rows.Close()
for rows.Next() {
var data [3]int
rows.Scan(&data[0], &data[1], &data[2])
stats = append(stats, data)
}
return stats, nil
}
type StatsDelay struct {
Source string `json:"source"`
Data [][2]float64 `json:"data"`
}
func queryStatsDelayByName(name string) ([]StatsDelay, error) {
var stats []StatsDelay
for subsys := range SUBSYS_MAP {
if subsys == name {
continue
}
var rows *sql.Rows
rows, err = db.Query(`SELECT TIMESTAMP, DELAY FROM STATISTICS_DELAY WHERE NAME=? and SOURCE=?`, name, subsys)
if err != nil {
return stats, err
}
var entry StatsDelay
entry.Source = subsys
defer rows.Close()
for rows.Next() {
var ts, delay float64
rows.Scan(&ts, &delay)
entry.Data = append(entry.Data, [2]float64{ts, delay})
}
if len(entry.Data) > 0 {
sort.SliceStable(entry.Data, func(i, j int) bool {
return entry.Data[i][0] < entry.Data[j][0]
})
stats = append(stats, entry)
}
}
return stats, nil
}