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.

79 lines
1.9 KiB

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