register.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package events
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "math"
  7. "os"
  8. "strings"
  9. "box-gm/utils"
  10. "time"
  11. // "log"
  12. "github.com/astaxie/beego"
  13. "github.com/tealeg/xlsx"
  14. )
  15. // 注册分布
  16. func Register_get_day_time_count(date string, shareCode string) (map[int]int, int) {
  17. var timeCount = make(map[int]int)
  18. filename := fmt.Sprintf("%s/register-%s.log", utils.GetKeyConf("events", "path"), date)
  19. f, err := os.Open(filename)
  20. if err != nil {
  21. return timeCount, 0
  22. }
  23. defer f.Close()
  24. startTime := utils.GetTime(date + " 00:00:00")
  25. totalCount := 0
  26. buff := bufio.NewReader(f)
  27. for {
  28. line, err := buff.ReadString('\n')
  29. if (err != nil || io.EOF == err) && line == "" {
  30. break
  31. }
  32. line = strings.TrimSpace(line)
  33. arr := strings.Split(line, ";")
  34. if shareCode == "" || arr[3] == shareCode {
  35. eventTime := utils.GetTime(arr[1])
  36. sec := eventTime - startTime
  37. sec = int(math.Floor(float64(sec / 300)))
  38. timeCount[sec]++
  39. totalCount++
  40. }
  41. if err != nil || io.EOF == err {
  42. break
  43. }
  44. }
  45. return timeCount, totalCount
  46. }
  47. func Register_get_range_day_time_count(days int, shareCode string) ([]*tgSeries, []string) {
  48. var series []*tgSeries
  49. currTime := int(time.Now().Unix())
  50. dayCount := 0
  51. for {
  52. tiEvent := currTime - 86400*dayCount
  53. date := time.Unix(int64(tiEvent), 0).Format("2006-01-02")
  54. item := &tgSeries{}
  55. glCount, totalCount := Register_get_day_time_count(date, shareCode)
  56. item.Name = date + fmt.Sprintf(" 总人数:%d", totalCount)
  57. for i := 0; i < 289; i++ {
  58. item.Data = append(item.Data, glCount[i])
  59. }
  60. item.MapCount = glCount
  61. // log.Printf("item[%v]", item)
  62. series = append(series, item)
  63. dayCount++
  64. if dayCount > days {
  65. break
  66. }
  67. }
  68. var arrCate []string
  69. loc, _ := time.LoadLocation("Local")
  70. theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" 00:00:00", loc)
  71. for i := 0; i < 289; i++ {
  72. ti := theTime.Add(time.Minute * 5 * time.Duration(i))
  73. arrCate = append(arrCate, fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
  74. }
  75. return series, arrCate
  76. }
  77. // 保存文件
  78. func Save_register_records(series []*tgSeries, user string) string {
  79. var file *xlsx.File
  80. var sheet *xlsx.Sheet
  81. var row *xlsx.Row
  82. var err error
  83. file = xlsx.NewFile()
  84. sheet, err = file.AddSheet("注册分布")
  85. if err != nil {
  86. beego.Warn(err)
  87. return ""
  88. }
  89. row = sheet.AddRow()
  90. row.AddCell().Value = "日期"
  91. row.AddCell().Value = "时间"
  92. row.AddCell().Value = "人数"
  93. loc, _ := time.LoadLocation("Local")
  94. theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" 00:00:00", loc)
  95. for i := 0; i < len(series); i++ {
  96. item := series[i]
  97. for k, v := range item.MapCount {
  98. row = sheet.AddRow()
  99. row.AddCell().SetString(item.Name)
  100. ti := theTime.Add(time.Minute * 5 * time.Duration(k))
  101. row.AddCell().SetString(fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
  102. row.AddCell().SetInt(v)
  103. }
  104. }
  105. name := fmt.Sprintf("./static/统计-注册-%s.xlsx", user)
  106. os.Remove(name)
  107. err = file.Save(name)
  108. if err != nil {
  109. beego.Warn(err)
  110. return ""
  111. }
  112. return name
  113. }