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.

89 lines
2.1 KiB

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