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