-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
244 lines (218 loc) · 6.48 KB
/
handlers.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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type nodeSage struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
ProvisionDate string `json:"provisionDate,omitempty"`
OSVersion string `json:"OSVersion,omitempty"`
ServiceTag string `json:"serviceTag,omitempty"`
SpecialDevices string `json:"SpecialDevices,omitempty"`
BiosVersion string `json:"BiosVersion,omitempty"`
Lat string `json:"lat,omitempty"`
Lon string `json:"lon,omitempty"`
}
type metadata struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Label string `json:"label,omitempty"`
Description string `json:"description,omitempty"`
}
type data struct {
Data []*nodeSage `json:"data,omitempty"`
Metadata []*metadata `json:"metadata,omitempty"`
}
type wildNodeSage struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
Vsn string `json:"vsn,omitempty"`
Lat float64 `json:"lat,omitempty"`
Lon float64 `json:"lon,omitempty"`
}
type wildNodedata struct {
Data []*wildNodeSage `json:"data,omitempty"`
Metadata []*metadata `json:"metadata"`
}
type pluginMetadata struct {
Node string `json:"node,omitempty"`
Host string `json:"host,omitempty"`
Plugin string `json:"plugin,omitempty"`
Camera string `json:"camera,omitempty"`
SensorID string `json:"sensorID,omitempty"`
}
type pluginSage struct {
Timestamp string `json:"timestamp,omitempty"`
Name string `json:"name,omitempty"`
Value float64 `json:"value,omitempty"`
Metadata pluginMetadata `json:"metadata,omitempty"`
}
type pluginData struct {
Data []*pluginSage `json:"data"`
Metadata []*metadata `json:"metadata"`
}
type sensorHardware struct {
ID string `json:"id,omitempty"`
Product_name string `json:"product_name,omitempty"`
Manufacture string `json:"manufacture,omitempty"`
Sensor_types string `json:"sensor_types,omitempty"`
Link string `json:"link,omitempty"`
}
type sensorHardwareData struct {
Data []*sensorHardware `json:"data"`
Metadata []*metadata `json:"metadata"`
}
func getSageNodes(w http.ResponseWriter, r *http.Request) {
nodeData := getNodeDataFromCSV(csvFile)
var allData data
allData.Data = nodeData
log.Println("GET: All nodes data")
respondJSON(w, http.StatusOK, allData)
return
}
func getNodeDataFromCSV(csvFile string) []*nodeSage {
csv_file, err := os.Open(csvFile)
if err != nil {
err = fmt.Errorf("Error with opening csv: %v", err)
return nil
}
defer csv_file.Close()
r := csv.NewReader(csv_file)
records, err := r.ReadAll()
if err != nil {
err = fmt.Errorf("Error with reading all data: %v", err)
return nil
}
header := make([]string, 0)
for _, headerContent := range records[0] {
header = append(header, headerContent)
}
indexMap := map[string]int{}
for i, name := range header {
indexMap[name] = i
}
records = records[1:]
nodes := []*nodeSage{}
for _, rec := range records {
node := new(nodeSage)
node.Name = rec[indexMap["name"]]
node.ID = rec[indexMap["id"]]
node.Status = rec[indexMap["status"]]
node.ProvisionDate = rec[indexMap["provisionDate"]]
node.OSVersion = rec[indexMap["OSVersion"]]
node.ServiceTag = rec[indexMap["ServiceTag"]]
node.SpecialDevices = rec[indexMap["SpecialDevices"]]
node.BiosVersion = rec[indexMap["BIOSVersion"]]
node.Lat = rec[indexMap["Lat"]]
node.Lon = rec[indexMap["Lon"]]
nodes = append(nodes, node)
}
return nodes
}
func getSageNodesMetadata(w http.ResponseWriter, r *http.Request) {
nodeMetadata := getNodeMetadataFromJson(jsonFile)
var allData data
allData.Metadata = nodeMetadata
log.Println("GET: All nodes metadata")
respondJSON(w, http.StatusOK, allData)
return
}
func getNodeMetadataFromJson(jsonFile string) []*metadata {
jsonData, err := os.Open(jsonFile)
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonData)
data := []*metadata{}
json.Unmarshal(byteValue, &data)
return data
}
func getSageNodesAndDataDict(w http.ResponseWriter, r *http.Request) {
nodeData := getNodeDataFromCSV(csvFile)
metadata := getNodeMetadataFromJson(jsonFile)
var allData data
allData.Data = nodeData
allData.Metadata = metadata
log.Println("GET: All nodes data and metadata")
respondJSON(w, http.StatusOK, allData)
return
}
//
func getWildNodeDataFromJson(jsonFile string) []*wildNodeSage {
jsonData, err := os.Open(jsonFile)
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonData)
data := []*wildNodeSage{}
json.Unmarshal(byteValue, &data)
return data
}
func getWildNodeData(w http.ResponseWriter, r *http.Request) {
data := getWildNodeDataFromJson(wildNodesFile)
var wildNodes wildNodedata
wildNodes.Data = data
wildNodes.Metadata = []*metadata{}
log.Println("GET: All wild node data")
respondJSON(w, http.StatusOK, wildNodes)
return
}
//Plugin Data section
func getPluginDataFromJson(jsonFile string) []*pluginSage {
jsonData, err := os.Open(jsonFile)
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonData)
data := []*pluginSage{}
json.Unmarshal(byteValue, &data)
return data
}
func getSagePluginData(w http.ResponseWriter, r *http.Request) {
data := getPluginDataFromJson(pluginFile)
var sagePluginData pluginData
sagePluginData.Data = data
sagePluginData.Metadata = []*metadata{}
log.Println("GET: All plugin data")
respondJSON(w, http.StatusOK, sagePluginData)
return
}
//Sensor Hardware Data Section
func getSensorHardwareFromJson(jsonFile string) []*sensorHardware {
jsonData, err := os.Open(jsonFile)
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonData)
data := []*sensorHardware{}
json.Unmarshal(byteValue, &data)
return data
}
func getSensorHardwareData(w http.ResponseWriter, r *http.Request) {
data := getSensorHardwareFromJson(sensorHardwareFile)
var sensorHardware sensorHardwareData
sensorHardware.Data = data
sensorHardware.Metadata = []*metadata{}
log.Println("GET: All sensor hardware data")
respondJSON(w, http.StatusOK, sensorHardware)
return
}
func authMW(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
// Add sage authentication later (if needed)
next(w, r)
}
func respondJSON(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
s, err := json.MarshalIndent(data, "", " ")
if err == nil {
w.Write(s)
}
}