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.

389 lines
8.2 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. "Stats",
  22. "Get",
  23. "/stats",
  24. Stats,
  25. },
  26. Route{
  27. "AllAddresses",
  28. "Get",
  29. "/alladdresses",
  30. AllAddresses,
  31. },
  32. Route{
  33. "GetLastAddr",
  34. "Get",
  35. "/lastaddr",
  36. GetLastAddr,
  37. },
  38. Route{
  39. "GetLastTx",
  40. "Get",
  41. "/lasttx",
  42. GetLastTx,
  43. },
  44. Route{
  45. "AddressNetwork",
  46. "GET",
  47. "/address/network/{address}",
  48. AddressNetwork,
  49. },
  50. Route{
  51. "AddressSankey",
  52. "GET",
  53. "/address/sankey/{address}",
  54. AddressSankey,
  55. },
  56. Route{
  57. "NetworkMap",
  58. "Get",
  59. "/map",
  60. NetworkMap,
  61. },
  62. Route{
  63. "GetTotalHourAnalysis",
  64. "Get",
  65. "/totalhouranalysis",
  66. GetTotalHourAnalysis,
  67. },
  68. Route{
  69. "GetLast24HourAnalysis",
  70. "Get",
  71. "/last24hour",
  72. GetLast24HourAnalysis,
  73. },
  74. Route{
  75. "GetLast7DayAnalysis",
  76. "Get",
  77. "/last7day",
  78. GetLast7DayAnalysis,
  79. },
  80. Route{
  81. "GetLast7DayHourAnalysis",
  82. "Get",
  83. "/last7dayhour",
  84. GetLast7DayHourAnalysis,
  85. },
  86. }
  87. //ROUTES
  88. func Index(w http.ResponseWriter, r *http.Request) {
  89. fmt.Fprintln(w, "ask for recommendations in /r")
  90. //http.FileServer(http.Dir("./web"))
  91. }
  92. /*
  93. func NewUser(w http.ResponseWriter, r *http.Request) {
  94. ipFilter(w, r)
  95. decoder := json.NewDecoder(r.Body)
  96. var newUser UserModel
  97. err := decoder.Decode(&newUser)
  98. check(err)
  99. defer r.Body.Close()
  100. saveUser(userCollection, newUser)
  101. fmt.Println(newUser)
  102. fmt.Fprintln(w, "new user added: ", newUser.ID)
  103. }
  104. */
  105. func Stats(w http.ResponseWriter, r *http.Request) {
  106. ipFilter(w, r)
  107. stats := getStats()
  108. jsonResp, err := json.Marshal(stats)
  109. check(err)
  110. fmt.Fprintln(w, string(jsonResp))
  111. }
  112. func AllAddresses(w http.ResponseWriter, r *http.Request) {
  113. ipFilter(w, r)
  114. nodes := []NodeModel{}
  115. iter := nodeCollection.Find(bson.M{"type": "address"}).Limit(10000).Iter()
  116. err := iter.All(&nodes)
  117. //convert []resp struct to json
  118. jsonNodes, err := json.Marshal(nodes)
  119. check(err)
  120. fmt.Fprintln(w, string(jsonNodes))
  121. }
  122. func GetLastAddr(w http.ResponseWriter, r *http.Request) {
  123. ipFilter(w, r)
  124. addresses := []AddressModel{}
  125. err := addressCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&addresses)
  126. check(err)
  127. //convert []resp struct to json
  128. jsonResp, err := json.Marshal(addresses)
  129. check(err)
  130. fmt.Fprintln(w, string(jsonResp))
  131. }
  132. func GetLastTx(w http.ResponseWriter, r *http.Request) {
  133. ipFilter(w, r)
  134. txs := []TxModel{}
  135. err := txCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&txs)
  136. check(err)
  137. //convert []resp struct to json
  138. jsonData, err := json.Marshal(txs)
  139. check(err)
  140. fmt.Fprintln(w, string(jsonData))
  141. }
  142. func AddressNetwork(w http.ResponseWriter, r *http.Request) {
  143. ipFilter(w, r)
  144. vars := mux.Vars(r)
  145. address := vars["address"]
  146. if address == "undefined" {
  147. fmt.Fprintln(w, "not valid address")
  148. } else {
  149. network := addressTree(address)
  150. network.Nodes[0].Shape = "triangle"
  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 AddressSankey(w http.ResponseWriter, r *http.Request) {
  158. ipFilter(w, r)
  159. vars := mux.Vars(r)
  160. address := vars["address"]
  161. if address == "undefined" {
  162. fmt.Fprintln(w, "not valid address")
  163. } else {
  164. network := addressTree(address)
  165. var sankey SankeyModel
  166. fmt.Println("network generated")
  167. mapNodeK := make(map[string]int)
  168. for k, n := range network.Nodes {
  169. var sankeyNode SankeyNodeModel
  170. //sankeyNode.StringNode = n.Id
  171. sankeyNode.Node = k
  172. sankeyNode.Name = n.Id
  173. sankey.Nodes = append(sankey.Nodes, sankeyNode)
  174. mapNodeK[n.Id] = k
  175. }
  176. for _, e := range network.Edges {
  177. var sankeyLink SankeyLinkModel
  178. //sankeyLink.StringSource = e.From
  179. sankeyLink.Source = mapNodeK[e.From]
  180. //sankeyLink.StringTarget = e.To
  181. sankeyLink.Target = mapNodeK[e.To]
  182. sankeyLink.Value = e.Label
  183. sankey.Links = append(sankey.Links, sankeyLink)
  184. }
  185. fmt.Println("Sankey generated")
  186. //convert []resp struct to json
  187. jsonSankey, err := json.Marshal(sankey)
  188. check(err)
  189. fmt.Fprintln(w, string(jsonSankey))
  190. }
  191. }
  192. func NetworkMap(w http.ResponseWriter, r *http.Request) {
  193. ipFilter(w, r)
  194. nodes, err := getAllNodes()
  195. check(err)
  196. edges, err := getAllEdges()
  197. check(err)
  198. var network NetworkModel
  199. network.Nodes = nodes
  200. network.Edges = edges
  201. //convert []resp struct to json
  202. jNetwork, err := json.Marshal(network)
  203. check(err)
  204. fmt.Fprintln(w, string(jNetwork))
  205. }
  206. func GetTotalHourAnalysis(w http.ResponseWriter, r *http.Request) {
  207. ipFilter(w, r)
  208. hourAnalysis := []ChartCountModel{}
  209. iter := hourCountCollection.Find(bson.M{}).Limit(10000).Iter()
  210. err := iter.All(&hourAnalysis)
  211. //sort by hour
  212. sort.Slice(hourAnalysis, func(i, j int) bool {
  213. return hourAnalysis[i].Elem < hourAnalysis[j].Elem
  214. })
  215. var resp ChartAnalysisResp
  216. for _, d := range hourAnalysis {
  217. resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
  218. resp.Data = append(resp.Data, d.Count)
  219. }
  220. //convert []resp struct to json
  221. jsonResp, err := json.Marshal(resp)
  222. check(err)
  223. fmt.Fprintln(w, string(jsonResp))
  224. }
  225. func GetLast24HourAnalysis(w http.ResponseWriter, r *http.Request) {
  226. ipFilter(w, r)
  227. fromDate := time.Now().AddDate(0, 0, -1)
  228. toDate := time.Now()
  229. txs := []TxModel{}
  230. err := txCollection.Find(bson.M{
  231. "datet": bson.M{
  232. "$gt": fromDate,
  233. "$lt": toDate,
  234. },
  235. }).Sort("-$natural").All(&txs)
  236. check(err)
  237. //generate map with 24 hours
  238. hourFrequencies := map24hours()
  239. for _, tx := range txs {
  240. hourFrequencies[tx.Date.Hour]++
  241. }
  242. var hourCount []ChartCountModel
  243. for hour, frequency := range hourFrequencies {
  244. hourCount = append(hourCount, ChartCountModel{hour, frequency})
  245. }
  246. //sort by hour
  247. sort.Slice(hourCount, func(i, j int) bool {
  248. return hourCount[i].Elem < hourCount[j].Elem
  249. })
  250. var resp ChartAnalysisResp
  251. for _, d := range hourCount {
  252. resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
  253. resp.Data = append(resp.Data, d.Count)
  254. }
  255. //convert []resp struct to json
  256. jsonResp, err := json.Marshal(resp)
  257. check(err)
  258. fmt.Fprintln(w, string(jsonResp))
  259. }
  260. func GetLast7DayAnalysis(w http.ResponseWriter, r *http.Request) {
  261. ipFilter(w, r)
  262. fromDate := time.Now().AddDate(0, 0, -7)
  263. toDate := time.Now()
  264. txs := []TxModel{}
  265. err := txCollection.Find(bson.M{
  266. "datet": bson.M{
  267. "$gt": fromDate,
  268. "$lt": toDate,
  269. },
  270. }).Sort("-$natural").All(&txs)
  271. check(err)
  272. //generate map with 24 hours
  273. //hourFrequencies := map24hours()
  274. dayFrequencies := make(map[int]int)
  275. for _, tx := range txs {
  276. dayFrequencies[tx.Date.Day]++
  277. }
  278. var dayCount []ChartCountModel
  279. for day, frequency := range dayFrequencies {
  280. dayCount = append(dayCount, ChartCountModel{day, frequency})
  281. }
  282. //sort by hour
  283. sort.Slice(dayCount, func(i, j int) bool {
  284. return dayCount[i].Elem < dayCount[j].Elem
  285. })
  286. var resp ChartAnalysisResp
  287. for _, d := range dayCount {
  288. resp.Labels = append(resp.Labels, strconv.Itoa(d.Elem))
  289. resp.Data = append(resp.Data, d.Count)
  290. }
  291. //convert []resp struct to json
  292. jsonResp, err := json.Marshal(resp)
  293. check(err)
  294. fmt.Fprintln(w, string(jsonResp))
  295. }
  296. func GetLast7DayHourAnalysis(w http.ResponseWriter, r *http.Request) {
  297. ipFilter(w, r)
  298. var resp ChartSeriesAnalysisResp
  299. for i := 0; i < 7; i++ {
  300. fromDate := time.Now().AddDate(0, 0, -i-1)
  301. toDate := time.Now().AddDate(0, 0, -i)
  302. txs := []TxModel{}
  303. err := txCollection.Find(bson.M{
  304. "datet": bson.M{
  305. "$gt": fromDate,
  306. "$lt": toDate,
  307. },
  308. }).Sort("-$natural").All(&txs)
  309. check(err)
  310. //generate map with 24 hours
  311. hourFrequencies := map24hours()
  312. for _, tx := range txs {
  313. hourFrequencies[tx.Date.Hour]++
  314. }
  315. var hourCount []ChartCountModel
  316. for hour, frequency := range hourFrequencies {
  317. hourCount = append(hourCount, ChartCountModel{hour, frequency})
  318. }
  319. //sort by hour
  320. sort.Slice(hourCount, func(i, j int) bool {
  321. return hourCount[i].Elem < hourCount[j].Elem
  322. })
  323. var dayData []int
  324. for _, d := range hourCount {
  325. dayData = append(dayData, d.Count)
  326. }
  327. resp.Series = append(resp.Series, txs[0].Date.Day)
  328. resp.Data = append(resp.Data, dayData)
  329. }
  330. hourLabels := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}
  331. resp.Labels = hourLabels
  332. //convert []resp struct to json
  333. jsonResp, err := json.Marshal(resp)
  334. check(err)
  335. fmt.Fprintln(w, string(jsonResp))
  336. }