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.

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