goBlockchainDataAnalysis/serverRoutes.go
serverRoutes.goRaw
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "sort"
8 "strconv"
9 "time"
10
11 "gopkg.in/mgo.v2/bson"
12
13 "github.com/gorilla/mux"
14)
15
16type Routes []Route
17
18var routes = Routes{
19 Route{
20 "Index",
21 "GET",
22 "/",
23 Index,
24 },
25 Route{
26 "Stats",
27 "Get",
28 "/stats",
29 Stats,
30 },
31 Route{
32 "AllAddresses",
33 "Get",
34 "/alladdresses",
35 AllAddresses,
36 },
37 Route{
38 "GetLastAddr",
39 "Get",
40 "/lastaddr",
41 GetLastAddr,
42 },
43 Route{
44 "GetLastTx",
45 "Get",
46 "/lasttx",
47 GetLastTx,
48 },
49 Route{
50 "AddressNetwork",
51 "GET",
52 "/address/network/{address}",
53 AddressNetwork,
54 },
55 Route{
56 "AddressSankey",
57 "GET",
58 "/address/sankey/{address}",
59 AddressSankey,
60 },
61 Route{
62 "NetworkMap",
63 "Get",
64 "/map",
65 NetworkMap,
66 },
67 Route{
68 "GetTotalHourAnalysis",
69 "Get",
70 "/totalhouranalysis",
71 GetTotalHourAnalysis,
72 },
73 Route{
74 "GetLast24HourAnalysis",
75 "Get",
76 "/last24hour",
77 GetLast24HourAnalysis,
78 },
79 Route{
80 "GetLast7DayAnalysis",
81 "Get",
82 "/last7day",
83 GetLast7DayAnalysis,
84 },
85 Route{
86 "GetLast7DayHourAnalysis",
87 "Get",
88 "/last7dayhour",
89 GetLast7DayHourAnalysis,
90 },
91}
92
93//ROUTES
94
95func Index(w http.ResponseWriter, r *http.Request) {
96 fmt.Fprintln(w, "ask for recommendations in /r")
97 //http.FileServer(http.Dir("./web"))
98}
99
100/*
101func NewUser(w http.ResponseWriter, r *http.Request) {
102 ipFilter(w, r)
103 decoder := json.NewDecoder(r.Body)
104 var newUser UserModel
105 err := decoder.Decode(&newUser)
106 check(err)
107 defer r.Body.Close()
108
109 saveUser(userCollection, newUser)
110
111 fmt.Println(newUser)
112 fmt.Fprintln(w, "new user added: ", newUser.ID)
113}
114*/
115func Stats(w http.ResponseWriter, r *http.Request) {
116 ipFilter(w, r)
117
118 stats := getStats()
119
120 jsonResp, err := json.Marshal(stats)
121 check(err)
122
123 fmt.Fprintln(w, string(jsonResp))
124}
125func AllAddresses(w http.ResponseWriter, r *http.Request) {
126 ipFilter(w, r)
127
128 nodes := []NodeModel{}
129 iter := nodeCollection.Find(bson.M{"type": "address"}).Limit(10000).Iter()
130 err := iter.All(&nodes)
131
132 //convert []resp struct to json
133 jsonNodes, err := json.Marshal(nodes)
134 check(err)
135
136 fmt.Fprintln(w, string(jsonNodes))
137}
138func GetLastAddr(w http.ResponseWriter, r *http.Request) {
139 ipFilter(w, r)
140
141 addresses := []AddressModel{}
142 err := addressCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&addresses)
143 check(err)
144
145 //convert []resp struct to json
146 jsonResp, err := json.Marshal(addresses)
147 check(err)
148
149 fmt.Fprintln(w, string(jsonResp))
150}
151func GetLastTx(w http.ResponseWriter, r *http.Request) {
152 ipFilter(w, r)
153
154 txs := []TxModel{}
155 err := txCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&txs)
156 check(err)
157
158 //convert []resp struct to json
159 jsonData, err := json.Marshal(txs)
160 check(err)
161
162 fmt.Fprintln(w, string(jsonData))
163}
164func AddressNetwork(w http.ResponseWriter, r *http.Request) {
165 ipFilter(w, r)
166
167 vars := mux.Vars(r)
168 address := vars["address"]
169 if address == "undefined" {
170 fmt.Fprintln(w, "not valid address")
171 } else {
172 network := addressTree(address)
173 network.Nodes[0].Shape = "triangle"
174
175 //convert []resp struct to json
176 jNetwork, err := json.Marshal(network)
177 check(err)
178
179 fmt.Fprintln(w, string(jNetwork))
180 }
181}
182func AddressSankey(w http.ResponseWriter, r *http.Request) {
183 ipFilter(w, r)
184 vars := mux.Vars(r)
185 address := vars["address"]
186 if address == "undefined" {
187 fmt.Fprintln(w, "not valid address")
188 } else {
189 network := addressTree(address)
190 var sankey SankeyModel
191
192 fmt.Println("network generated")
193 mapNodeK := make(map[string]int)
194 for k, n := range network.Nodes {
195 var sankeyNode SankeyNodeModel
196 //sankeyNode.StringNode = n.Id
197 sankeyNode.Node = k
198 sankeyNode.Name = n.Id
199 sankey.Nodes = append(sankey.Nodes, sankeyNode)
200 mapNodeK[n.Id] = k
201 }
202 for _, e := range network.Edges {
203 var sankeyLink SankeyLinkModel
204 //sankeyLink.StringSource = e.From
205 sankeyLink.Source = mapNodeK[e.From]
206 //sankeyLink.StringTarget = e.To
207 sankeyLink.Target = mapNodeK[e.To]
208 sankeyLink.Value = e.Label
209 sankey.Links = append(sankey.Links, sankeyLink)
210 }
211 fmt.Println("Sankey generated")
212
213 //convert []resp struct to json
214 jsonSankey, err := json.Marshal(sankey)
215 check(err)
216
217 fmt.Fprintln(w, string(jsonSankey))
218 }
219}
220func NetworkMap(w http.ResponseWriter, r *http.Request) {
221 ipFilter(w, r)
222
223 nodes, err := getAllNodes()
224 check(err)
225 edges, err := getAllEdges()
226 check(err)
227
228 var network NetworkModel
229 network.Nodes = nodes
230 network.Edges = edges
231
232 //convert []resp struct to json
233 jNetwork, err := json.Marshal(network)
234 check(err)
235
236 fmt.Fprintln(w, string(jNetwork))
237}
238
239func GetTotalHourAnalysis(w http.ResponseWriter, r *http.Request) {
240 ipFilter(w, r)
241
242 hourAnalysis := []ChartCountModel{}
243 iter := hourCountCollection.Find(bson.M{}).Limit(10000).Iter()
244 err := iter.All(&hourAnalysis)
245
246 //sort by hour
247 sort.Slice(hourAnalysis, func(i, j int) bool {
248 return hourAnalysis[i].Elem < hourAnalysis[j].Elem
249 })
250
251 var resp ChartAnalysisResp
252 for _, d := range hourAnalysis {
253 resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
254 resp.Data = append(resp.Data, d.Count)
255 }
256
257 //convert []resp struct to json
258 jsonResp, err := json.Marshal(resp)
259 check(err)
260 fmt.Fprintln(w, string(jsonResp))
261}
262
263func GetLast24HourAnalysis(w http.ResponseWriter, r *http.Request) {
264 ipFilter(w, r)
265
266 fromDate := time.Now().AddDate(0, 0, -1)
267 toDate := time.Now()
268
269 txs := []TxModel{}
270 err := txCollection.Find(bson.M{
271 "datet": bson.M{
272 "$gt": fromDate,
273 "$lt": toDate,
274 },
275 }).Sort("-$natural").All(&txs)
276 check(err)
277
278 //generate map with 24 hours
279 hourFrequencies := map24hours()
280 for _, tx := range txs {
281 hourFrequencies[tx.Date.Hour]++
282 }
283 var hourCount []ChartCountModel
284 for hour, frequency := range hourFrequencies {
285 hourCount = append(hourCount, ChartCountModel{hour, frequency})
286 }
287 //sort by hour
288 sort.Slice(hourCount, func(i, j int) bool {
289 return hourCount[i].Elem < hourCount[j].Elem
290 })
291
292 var resp ChartAnalysisResp
293 for _, d := range hourCount {
294 resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
295 resp.Data = append(resp.Data, d.Count)
296 }
297
298 //convert []resp struct to json
299 jsonResp, err := json.Marshal(resp)
300 check(err)
301 fmt.Fprintln(w, string(jsonResp))
302}
303func GetLast7DayAnalysis(w http.ResponseWriter, r *http.Request) {
304 ipFilter(w, r)
305
306 fromDate := time.Now().AddDate(0, 0, -7)
307 toDate := time.Now()
308
309 txs := []TxModel{}
310 err := txCollection.Find(bson.M{
311 "datet": bson.M{
312 "$gt": fromDate,
313 "$lt": toDate,
314 },
315 }).Sort("-$natural").All(&txs)
316 check(err)
317
318 //generate map with 24 hours
319 //hourFrequencies := map24hours()
320 dayFrequencies := make(map[int]int)
321 for _, tx := range txs {
322 dayFrequencies[tx.Date.Day]++
323 }
324 var dayCount []ChartCountModel
325 for day, frequency := range dayFrequencies {
326 dayCount = append(dayCount, ChartCountModel{day, frequency})
327 }
328 //sort by hour
329 sort.Slice(dayCount, func(i, j int) bool {
330 return dayCount[i].Elem < dayCount[j].Elem
331 })
332
333 var resp ChartAnalysisResp
334 for _, d := range dayCount {
335 resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
336 resp.Data = append(resp.Data, d.Count)
337 }
338
339 //convert []resp struct to json
340 jsonResp, err := json.Marshal(resp)
341 check(err)
342 fmt.Fprintln(w, string(jsonResp))
343}
344
345func GetLast7DayHourAnalysis(w http.ResponseWriter, r *http.Request) {
346 ipFilter(w, r)
347
348 var resp ChartSeriesAnalysisResp
349
350 for i := 0; i < 7; i++ {
351 fromDate := time.Now().AddDate(0, 0, -i-1)
352 toDate := time.Now().AddDate(0, 0, -i)
353 txs := []TxModel{}
354 err := txCollection.Find(bson.M{
355 "datet": bson.M{
356 "$gt": fromDate,
357 "$lt": toDate,
358 },
359 }).Sort("-$natural").All(&txs)
360 check(err)
361
362 //generate map with 24 hours
363 hourFrequencies := map24hours()
364 for _, tx := range txs {
365 hourFrequencies[tx.Date.Hour]++
366 }
367 var hourCount []ChartCountModel
368 for hour, frequency := range hourFrequencies {
369 hourCount = append(hourCount, ChartCountModel{hour, frequency})
370 }
371 //sort by hour
372 sort.Slice(hourCount, func(i, j int) bool {
373 return hourCount[i].Elem < hourCount[j].Elem
374 })
375 var dayData []int
376 for _, d := range hourCount {
377 dayData = append(dayData, d.Count)
378 }
379 resp.Series = append(resp.Series, txs[0].Date.Day)
380 resp.Data = append(resp.Data, dayData)
381 }
382 hourLabels := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}
383 resp.Labels = hourLabels
384
385 //convert []resp struct to json
386 jsonResp, err := json.Marshal(resp)
387 check(err)
388 fmt.Fprintln(w, string(jsonResp))
389}