rtonline.go 3.5 KB

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