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.

261 lines
5.1 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "sort"
  7. "gopkg.in/mgo.v2/bson"
  8. "github.com/gorilla/mux"
  9. )
  10. type Routes []Route
  11. var routes = Routes{
  12. Route{
  13. "Index",
  14. "GET",
  15. "/",
  16. Index,
  17. },
  18. Route{
  19. "AllAddresses",
  20. "Get",
  21. "/alladdresses",
  22. AllAddresses,
  23. },
  24. Route{
  25. "GetLastAddr",
  26. "Get",
  27. "/lastaddr",
  28. GetLastAddr,
  29. },
  30. Route{
  31. "GetLastTx",
  32. "Get",
  33. "/lasttx",
  34. GetLastTx,
  35. },
  36. Route{
  37. "AddressNetwork",
  38. "GET",
  39. "/address/network/{address}",
  40. AddressNetwork,
  41. },
  42. Route{
  43. "AddressSankey",
  44. "GET",
  45. "/address/sankey/{address}",
  46. AddressSankey,
  47. },
  48. Route{
  49. "NetworkMap",
  50. "Get",
  51. "/map",
  52. NetworkMap,
  53. },
  54. Route{
  55. "GetHourAnalysis",
  56. "Get",
  57. "/houranalysis",
  58. GetHourAnalysis,
  59. },
  60. }
  61. //ROUTES
  62. func Index(w http.ResponseWriter, r *http.Request) {
  63. fmt.Fprintln(w, "ask for recommendations in /r")
  64. //http.FileServer(http.Dir("./web"))
  65. }
  66. /*
  67. func NewUser(w http.ResponseWriter, r *http.Request) {
  68. ipFilter(w, r)
  69. decoder := json.NewDecoder(r.Body)
  70. var newUser UserModel
  71. err := decoder.Decode(&newUser)
  72. check(err)
  73. defer r.Body.Close()
  74. saveUser(userCollection, newUser)
  75. fmt.Println(newUser)
  76. fmt.Fprintln(w, "new user added: ", newUser.ID)
  77. }
  78. */
  79. func AllAddresses(w http.ResponseWriter, r *http.Request) {
  80. ipFilter(w, r)
  81. nodes := []NodeModel{}
  82. iter := nodeCollection.Find(bson.M{}).Limit(10000).Iter()
  83. err := iter.All(&nodes)
  84. //convert []resp struct to json
  85. jsonNodes, err := json.Marshal(nodes)
  86. check(err)
  87. fmt.Fprintln(w, string(jsonNodes))
  88. }
  89. func GetLastAddr(w http.ResponseWriter, r *http.Request) {
  90. ipFilter(w, r)
  91. nodes := []NodeModel{}
  92. err := nodeCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&nodes)
  93. check(err)
  94. //convert []resp struct to json
  95. jNodes, err := json.Marshal(nodes)
  96. check(err)
  97. fmt.Fprintln(w, string(jNodes))
  98. }
  99. func GetLastTx(w http.ResponseWriter, r *http.Request) {
  100. ipFilter(w, r)
  101. edges := []EdgeModel{}
  102. err := edgeCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&edges)
  103. check(err)
  104. //convert []resp struct to json
  105. jsonData, err := json.Marshal(edges)
  106. check(err)
  107. fmt.Fprintln(w, string(jsonData))
  108. }
  109. func AddressNetwork(w http.ResponseWriter, r *http.Request) {
  110. ipFilter(w, r)
  111. vars := mux.Vars(r)
  112. address := vars["address"]
  113. if address == "undefined" {
  114. fmt.Fprintln(w, "not valid address")
  115. } else {
  116. network := addressTree(address)
  117. //convert []resp struct to json
  118. jNetwork, err := json.Marshal(network)
  119. check(err)
  120. fmt.Fprintln(w, string(jNetwork))
  121. }
  122. }
  123. func AddressSankey(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. var sankey SankeyModel
  132. fmt.Println("network generated")
  133. mapNodeK := make(map[string]int)
  134. for k, n := range network.Nodes {
  135. var sankeyNode SankeyNodeModel
  136. //sankeyNode.StringNode = n.Id
  137. sankeyNode.Node = k
  138. sankeyNode.Name = n.Id
  139. sankey.Nodes = append(sankey.Nodes, sankeyNode)
  140. mapNodeK[n.Id] = k
  141. }
  142. for _, e := range network.Edges {
  143. var sankeyLink SankeyLinkModel
  144. //sankeyLink.StringSource = e.From
  145. sankeyLink.Source = mapNodeK[e.From]
  146. //sankeyLink.StringTarget = e.To
  147. sankeyLink.Target = mapNodeK[e.To]
  148. sankeyLink.Value = e.Label
  149. sankey.Links = append(sankey.Links, sankeyLink)
  150. }
  151. fmt.Println("Sankey generated")
  152. //convert []resp struct to json
  153. jsonSankey, err := json.Marshal(sankey)
  154. check(err)
  155. fmt.Fprintln(w, string(jsonSankey))
  156. }
  157. }
  158. func NetworkMap(w http.ResponseWriter, r *http.Request) {
  159. ipFilter(w, r)
  160. nodes, err := getAllNodes()
  161. check(err)
  162. edges, err := getAllEdges()
  163. check(err)
  164. var network NetworkModel
  165. network.Nodes = nodes
  166. network.Edges = edges
  167. //convert []resp struct to json
  168. jNetwork, err := json.Marshal(network)
  169. check(err)
  170. fmt.Fprintln(w, string(jNetwork))
  171. }
  172. /*
  173. func SelectItem(w http.ResponseWriter, r *http.Request) {
  174. ipFilter(w, r)
  175. vars := mux.Vars(r)
  176. userid := vars["userid"]
  177. itemid := vars["itemid"]
  178. //find item
  179. item, err := getItemById(itemid)
  180. if err != nil {
  181. fmt.Fprintln(w, "item "+itemid+" not found")
  182. }
  183. //find user
  184. user, err := getUserById(userid)
  185. if err != nil {
  186. fmt.Fprintln(w, "user "+userid+" not found")
  187. }
  188. //increase TActed in item
  189. item.TActed = item.TActed + 1
  190. //save item
  191. item, err = updateItem(item)
  192. check(err)
  193. fmt.Println(item)
  194. //add item to []Actions of user
  195. user.Actions = append(user.Actions, itemid)
  196. //save user
  197. user, err = updateUser(user)
  198. check(err)
  199. fmt.Println(user)
  200. fmt.Fprintln(w, "user: "+userid+", selects item: "+itemid)
  201. }
  202. */
  203. func GetHourAnalysis(w http.ResponseWriter, r *http.Request) {
  204. ipFilter(w, r)
  205. hourAnalysis := []HourCountModel{}
  206. iter := hourCountCollection.Find(bson.M{}).Limit(10000).Iter()
  207. err := iter.All(&hourAnalysis)
  208. //sort by hour
  209. sort.Slice(hourAnalysis, func(i, j int) bool {
  210. return hourAnalysis[i].Hour < hourAnalysis[j].Hour
  211. })
  212. var resp HourAnalysisResp
  213. for _, d := range hourAnalysis {
  214. resp.Labels = append(resp.Labels, d.Hour)
  215. resp.Data = append(resp.Data, d.Count)
  216. }
  217. //convert []resp struct to json
  218. jsonResp, err := json.Marshal(resp)
  219. check(err)
  220. fmt.Fprintln(w, string(jsonResp))
  221. }