-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
142 lines (136 loc) · 4.29 KB
/
handler.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
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/webview/webview"
)
func storeConnection(w webview.WebView, host string) {
err := addInfluxDBConfig(w, databaseHandler, host)
if err != nil {
createAlertDialog(w, "could not save", err.Error())
}
//createAlertDialog(w, "juu", "tesmos")
connections, _ := getInfluxDBConfigs(w, databaseHandler)
/*fmt.Println("connections: %v", connections)
connectionsJSON, _ := json.Marshal(connections)
data = fmt.Sprintf("window.connections=%s", connectionsJSON)
w.Eval(data)*/
popluateConnections(w, connections)
//createAlertDialog(w, "Connections", "jee")
}
func deleteConnection(w webview.WebView, host string) {
err := deleteInfluxDBConfig(w, databaseHandler, host)
if err != nil {
createAlertDialog(w, "Could not delete", err.Error())
}
connections, _ := getInfluxDBConfigs(w, databaseHandler)
popluateConnections(w, connections)
}
func handleRPC(w webview.WebView, data string) {
cmd := struct {
Name string `json:"cmd"`
}{}
if err := json.Unmarshal([]byte(data), &cmd); err != nil {
log.Println(err)
return
}
switch cmd.Name {
case "connections":
storeConnection(w, "http://localhost:8086")
case "addCon":
connection := InfluxDBConnection{}
if err := json.Unmarshal([]byte(data), &connection); err != nil {
createAlertDialog(w, "Could not decrypt connection info", err.Error())
} else if len(connection.Host) > 0 {
storeConnection(w, connection.Host)
} else {
createAlertDialog(w, "Host not given", "Host needs to be http://<domain>:<port>")
}
case "delCon":
connection := InfluxDBConnection{}
if err := json.Unmarshal([]byte(data), &connection); err != nil {
createAlertDialog(w, "Could not decrypt delete connection info", err.Error())
} else {
deleteConnection(w, connection.Host)
}
case "setHost":
if err := json.Unmarshal([]byte(data), &connectionConfig); err != nil {
createAlertDialog(w, "Could not set host", err.Error())
}
case "connectInflux":
if err := json.Unmarshal([]byte(data), &connectionConfig); err != nil {
createAlertDialog(w, "Could not connect influxdb", err.Error())
}
fmt.Printf("*****, %v, %v, %v", connectionConfig.Host, connectionConfig.Username, connectionConfig.Password)
res, msg := pingInfluxDB()
if !res {
createAlertDialog(w, connectionConfig.Host, msg)
}
res, databases, data := showDatabases()
if !res {
createAlertDialog(w, data, "")
return
}
updateConnectionStatus(w, res)
populateDatabases(w, databases)
case "sendQuery":
query := InfluxDBQuery{}
if err := json.Unmarshal([]byte(data), &query); err != nil {
createAlertDialog(w, "Could not send query", err.Error())
}
fmt.Printf("*****, %v, %v", query.Query, query.Database)
res, data := runInfluxDBQueryJSON(query.Query, query.Database)
if !res {
createAlertDialog(w, data, "")
return
}
w.Eval(data)
default:
createAlertDialog(w, "Default", "nothing happened")
/*case strings.HasPrefix(data, "connectInfluxDB:"):
// message := strings.TrimPrefix(data, "connectInfluxDB:")
// s := strings.Split(message, ";")
// port, err := strconv.Atoi(s[1])
// if err != nil {
/ w.Dialog(webview.DialogTypeAlert, webview.DialogFlagError, "Connect", "Could not convert port to integer!")
/ return
}
host := fmt.Sprintf("%s:%d", s[0], port)
connectionConfig = InfluxDBConnection{Host: host, Username: s[2], Password: s[3]}
res, message := pingInfluxDB()
if !res {
createAlertDialog(w, message, "")
return
}
res, data := showDatabases()
if !res {
createAlertDialog(w, data, "")
return
}
w.Eval(data)
w.Eval("window.toggleConnectionStatus();")
case strings.HasPrefix(data, "createInfluxDBQuery"):
query := strings.TrimPrefix(data, "createInfluxDBQuery:")
queryInfo := struct {
Query string `json:"query"`
Database string `json:"database"`
}{}
if err := json.Unmarshal([]byte(query), &queryInfo); err != nil {
w.Dialog(webview.DialogTypeAlert, webview.DialogFlagError, "Could not parse results!", err.Error())
return
}
appendToLog(queryInfo.Query)
updateHistoryLogs(w)
res, data := runInfluxDBQuery(queryInfo.Query, queryInfo.Database)
if !res {
createAlertDialog(w, data, "")
return
}
w.Eval(data)
*/
}
}
func updateHistoryLogs(w webview.WebView) {
writeHistoryLogs(w, getLogOptions())
}