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}
 80
 81//ROUTES
 82
 83func Index(w http.ResponseWriter, r *http.Request) {
 84	fmt.Fprintln(w, "ask for recommendations in /r")
 85	//http.FileServer(http.Dir("./web"))
 86}
 87
 88/*
 89func NewUser(w http.ResponseWriter, r *http.Request) {
 90	ipFilter(w, r)
 91	decoder := json.NewDecoder(r.Body)
 92	var newUser UserModel
 93	err := decoder.Decode(&newUser)
 94	check(err)
 95	defer r.Body.Close()
 96
 97	saveUser(userCollection, newUser)
 98
 99	fmt.Println(newUser)
100	fmt.Fprintln(w, "new user added: ", newUser.ID)
101}
102*/
103func AllAddresses(w http.ResponseWriter, r *http.Request) {
104	ipFilter(w, r)
105
106	nodes := []NodeModel{}
107	iter := nodeCollection.Find(bson.M{"type": "address"}).Limit(10000).Iter()
108	err := iter.All(&nodes)
109
110	//convert []resp struct to json
111	jsonNodes, err := json.Marshal(nodes)
112	check(err)
113
114	fmt.Fprintln(w, string(jsonNodes))
115}
116func GetLastAddr(w http.ResponseWriter, r *http.Request) {
117	ipFilter(w, r)
118
119	addresses := []AddressModel{}
120	err := addressCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&addresses)
121	check(err)
122
123	//convert []resp struct to json
124	jsonResp, err := json.Marshal(addresses)
125	check(err)
126
127	fmt.Fprintln(w, string(jsonResp))
128}
129func GetLastTx(w http.ResponseWriter, r *http.Request) {
130	ipFilter(w, r)
131
132	txs := []TxModel{}
133	err := txCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&txs)
134	check(err)
135
136	//convert []resp struct to json
137	jsonData, err := json.Marshal(txs)
138	check(err)
139
140	fmt.Fprintln(w, string(jsonData))
141}
142func AddressNetwork(w http.ResponseWriter, r *http.Request) {
143	ipFilter(w, r)
144
145	vars := mux.Vars(r)
146	address := vars["address"]
147	if address == "undefined" {
148		fmt.Fprintln(w, "not valid address")
149	} else {
150		network := addressTree(address)
151
152		//convert []resp struct to json
153		jNetwork, err := json.Marshal(network)
154		check(err)
155
156		fmt.Fprintln(w, string(jNetwork))
157	}
158}
159func AddressSankey(w http.ResponseWriter, r *http.Request) {
160	ipFilter(w, r)
161	vars := mux.Vars(r)
162	address := vars["address"]
163	if address == "undefined" {
164		fmt.Fprintln(w, "not valid address")
165	} else {
166		network := addressTree(address)
167		var sankey SankeyModel
168
169		fmt.Println("network generated")
170		mapNodeK := make(map[string]int)
171		for k, n := range network.Nodes {
172			var sankeyNode SankeyNodeModel
173			//sankeyNode.StringNode = n.Id
174			sankeyNode.Node = k
175			sankeyNode.Name = n.Id
176			sankey.Nodes = append(sankey.Nodes, sankeyNode)
177			mapNodeK[n.Id] = k
178		}
179		for _, e := range network.Edges {
180			var sankeyLink SankeyLinkModel
181			//sankeyLink.StringSource = e.From
182			sankeyLink.Source = mapNodeK[e.From]
183			//sankeyLink.StringTarget = e.To
184			sankeyLink.Target = mapNodeK[e.To]
185			sankeyLink.Value = e.Label
186			sankey.Links = append(sankey.Links, sankeyLink)
187		}
188		fmt.Println("Sankey generated")
189
190		//convert []resp struct to json
191		jsonSankey, err := json.Marshal(sankey)
192		check(err)
193
194		fmt.Fprintln(w, string(jsonSankey))
195	}
196}
197func NetworkMap(w http.ResponseWriter, r *http.Request) {
198	ipFilter(w, r)
199
200	nodes, err := getAllNodes()
201	check(err)
202	edges, err := getAllEdges()
203	check(err)
204
205	var network NetworkModel
206	network.Nodes = nodes
207	network.Edges = edges
208
209	//convert []resp struct to json
210	jNetwork, err := json.Marshal(network)
211	check(err)
212
213	fmt.Fprintln(w, string(jNetwork))
214}
215
216func GetTotalHourAnalysis(w http.ResponseWriter, r *http.Request) {
217	ipFilter(w, r)
218
219	hourAnalysis := []ChartCountModel{}
220	iter := hourCountCollection.Find(bson.M{}).Limit(10000).Iter()
221	err := iter.All(&hourAnalysis)
222
223	//sort by hour
224	sort.Slice(hourAnalysis, func(i, j int) bool {
225		return hourAnalysis[i].Elem < hourAnalysis[j].Elem
226	})
227
228	var resp ChartAnalysisResp
229	for _, d := range hourAnalysis {
230		resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
231		resp.Data = append(resp.Data, d.Count)
232	}
233
234	//convert []resp struct to json
235	jsonResp, err := json.Marshal(resp)
236	check(err)
237	fmt.Fprintln(w, string(jsonResp))
238}
239
240func GetLast24HourAnalysis(w http.ResponseWriter, r *http.Request) {
241	ipFilter(w, r)
242
243	fromDate := time.Now().AddDate(0, 0, -1)
244	toDate := time.Now()
245
246	txs := []TxModel{}
247	err := txCollection.Find(bson.M{
248		"datet": bson.M{
249			"$gt": fromDate,
250			"$lt": toDate,
251		},
252	}).Sort("-$natural").All(&txs)
253	check(err)
254
255	//generate map with 24 hours
256	hourFrequencies := map24hours()
257	for _, tx := range txs {
258		hourFrequencies[tx.Date.Hour]++
259	}
260	var hourCount []ChartCountModel
261	for hour, frequency := range hourFrequencies {
262		hourCount = append(hourCount, ChartCountModel{hour, frequency})
263	}
264	//sort by hour
265	sort.Slice(hourCount, func(i, j int) bool {
266		return hourCount[i].Elem < hourCount[j].Elem
267	})
268
269	var resp ChartAnalysisResp
270	for _, d := range hourCount {
271		resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
272		resp.Data = append(resp.Data, d.Count)
273	}
274
275	//convert []resp struct to json
276	jsonResp, err := json.Marshal(resp)
277	check(err)
278	fmt.Fprintln(w, string(jsonResp))
279}
280func GetLast7DayAnalysis(w http.ResponseWriter, r *http.Request) {
281	ipFilter(w, r)
282
283	fromDate := time.Now().AddDate(0, 0, -7)
284	toDate := time.Now()
285
286	txs := []TxModel{}
287	err := txCollection.Find(bson.M{
288		"datet": bson.M{
289			"$gt": fromDate,
290			"$lt": toDate,
291		},
292	}).Sort("-$natural").All(&txs)
293	check(err)
294
295	//generate map with 24 hours
296	//hourFrequencies := map24hours()
297	dayFrequencies := make(map[int]int)
298	for _, tx := range txs {
299		dayFrequencies[tx.Date.Day]++
300	}
301	var dayCount []ChartCountModel
302	for day, frequency := range dayFrequencies {
303		dayCount = append(dayCount, ChartCountModel{day, frequency})
304	}
305	//sort by hour
306	sort.Slice(dayCount, func(i, j int) bool {
307		return dayCount[i].Elem < dayCount[j].Elem
308	})
309
310	var resp ChartAnalysisResp
311	for _, d := range dayCount {
312		resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
313		resp.Data = append(resp.Data, d.Count)
314	}
315
316	//convert []resp struct to json
317	jsonResp, err := json.Marshal(resp)
318	check(err)
319	fmt.Fprintln(w, string(jsonResp))
320}