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.

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