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.

86 lines
2.0 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "gopkg.in/mgo.v2/bson"
  7. )
  8. func map24hours() map[int]int {
  9. h := make(map[int]int)
  10. for i := 0; i < 24; i++ {
  11. h[i] = 0
  12. }
  13. return h
  14. }
  15. func decomposeDate(blockTime int64) (int, int, int, int) {
  16. /*i, err := strconv.ParseInt(blockTime, 10, 64)
  17. if err != nil {
  18. panic(err)
  19. }*/
  20. i := blockTime
  21. year := time.Unix(i, 0).Year()
  22. month := time.Unix(i, 0).Month()
  23. day := time.Unix(i, 0).Day()
  24. hour := time.Unix(i, 0).Hour()
  25. return year, int(month), day, hour
  26. }
  27. func unixTimeToTime(blockTime int64) time.Time {
  28. return time.Unix(blockTime, 0)
  29. }
  30. func timeToDate(blockTime int64) string {
  31. stringTime := strconv.FormatInt(blockTime, 10)
  32. i, err := strconv.ParseInt(stringTime, 10, 64)
  33. if err != nil {
  34. panic(err)
  35. }
  36. tm := time.Unix(i, 0)
  37. //fmt.Println(tm)
  38. stringDate := tm.String()
  39. //fmt.Println(stringDate)
  40. return stringDate
  41. }
  42. func hourAnalysis(e EdgeModel, blockTime int64) {
  43. //fmt.Println(blockTime)
  44. /*date := timeToDate(blockTime)
  45. dateHour := strings.Split(date, " ")[1]
  46. hourString := strings.Split(dateHour, ":")[0]*/
  47. _, _, _, hour := decomposeDate(blockTime)
  48. hourCount := ChartCountModel{}
  49. err := hourCountCollection.Find(bson.M{"hour": hour}).One(&hourCount)
  50. if err != nil {
  51. //date not yet in DB
  52. var hourCount ChartCountModel
  53. hourCount.Elem = hour
  54. hourCount.Count = 1
  55. err = hourCountCollection.Insert(hourCount)
  56. check(err)
  57. } else {
  58. hourCount.Count++
  59. err = hourCountCollection.Update(bson.M{"hour": hour}, &hourCount)
  60. check(err)
  61. }
  62. }
  63. func dateAnalysis(e EdgeModel, blockTime int64) {
  64. fmt.Println(blockTime)
  65. date := timeToDate(blockTime)
  66. dateCount := DateCountModel{}
  67. err := dateCountCollection.Find(bson.M{"date": date}).One(&dateCount)
  68. if err != nil {
  69. //date not yet in DB
  70. var dateCount DateCountModel
  71. dateCount.Date = date
  72. dateCount.Time = blockTime
  73. dateCount.Count = 1
  74. err = dateCountCollection.Insert(dateCount)
  75. check(err)
  76. } else {
  77. dateCount.Count++
  78. err = dateCountCollection.Update(bson.M{"date": date}, &dateCount)
  79. check(err)
  80. }
  81. }