You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
8.0 KiB

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