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.

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