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.

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