login.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package events
  2. import (
  3. "fmt"
  4. "os"
  5. "bufio"
  6. "io"
  7. "strings"
  8. "math"
  9. "box-gm/utils"
  10. "time"
  11. // "log"
  12. "github.com/tealeg/xlsx"
  13. "github.com/astaxie/beego"
  14. )
  15. type RecordLogin struct {
  16. EventTime string
  17. ServerId string
  18. Uid int
  19. RegisteTime int64
  20. IP string
  21. }
  22. type RecordLoginArray []*RecordLogin
  23. // 登录分布
  24. func Login_get_day_time_count(date string, serverId string) (map[int]int, int) {
  25. var timeCount = make(map[int]int)
  26. var playerLevel = make(map[int]int)
  27. f, err := os.Open(fmt.Sprintf("%s/login-%s.log", utils.GetKeyConf("events", "path"), date))
  28. if err != nil {
  29. return timeCount, 0
  30. }
  31. defer f.Close()
  32. totalCount := 0
  33. startTime := utils.GetTime(date+" 00:00:00")
  34. buff := bufio.NewReader(f)
  35. for {
  36. line, err := buff.ReadString('\n')
  37. if err != nil || io.EOF == err {
  38. break
  39. }
  40. line = strings.TrimSpace(line)
  41. arr := strings.Split(line, ";")
  42. if serverId == "0" || arr[1] == serverId {
  43. eventTime := utils.GetTime(arr[0])
  44. uid := utils.StringToInt(arr[2])
  45. if playerLevel[uid] == 0 {
  46. totalCount ++
  47. }
  48. playerLevel[uid] = eventTime
  49. sec := eventTime - startTime
  50. sec = int(math.Floor(float64(sec/300)))
  51. timeCount[sec] ++
  52. }
  53. }
  54. return timeCount, totalCount
  55. }
  56. func Login_get_range_day_time_count(days int, serverId string) ([]*tgSeries, []string) {
  57. var series []*tgSeries
  58. currTime := int(time.Now().Unix())
  59. dayCount := 0
  60. for {
  61. tiEvent := currTime - 86400*dayCount
  62. date := time.Unix(int64(tiEvent), 0).Format("2006-01-02")
  63. item := &tgSeries{}
  64. glCount, totalCount := Login_get_day_time_count(date, serverId)
  65. item.Name = date + fmt.Sprintf(" 活跃人数:%d", totalCount)
  66. for i:=0; i < 289; i++ {
  67. item.Data = append(item.Data, glCount[i])
  68. }
  69. item.MapCount = glCount
  70. series = append(series, item)
  71. // log.Printf("login date[%s] item[%v]", date, item)
  72. dayCount ++
  73. if dayCount > days {
  74. break
  75. }
  76. }
  77. var arrCate []string
  78. loc, _ := time.LoadLocation("Local")
  79. theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02") + " 00:00:00", loc)
  80. for i:=0; i < 289; i++ {
  81. ti := theTime.Add(time.Minute*5*time.Duration(i))
  82. arrCate = append(arrCate, fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
  83. }
  84. return series, arrCate
  85. }
  86. // 保存文件
  87. func Save_login_records(series []*tgSeries, user string) string {
  88. var file *xlsx.File
  89. var sheet *xlsx.Sheet
  90. var row *xlsx.Row
  91. var err error
  92. file = xlsx.NewFile()
  93. sheet, err = file.AddSheet("登录分布")
  94. if err != nil {
  95. beego.Warn(err)
  96. return ""
  97. }
  98. row = sheet.AddRow()
  99. row.AddCell().Value = "日期"
  100. row.AddCell().Value = "时间"
  101. row.AddCell().Value = "人数"
  102. loc, _ := time.LoadLocation("Local")
  103. theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02") + " 00:00:00", loc)
  104. for i := 0; i < len(series); i++ {
  105. item := series[i]
  106. for k, v := range item.MapCount {
  107. row = sheet.AddRow()
  108. row.AddCell().SetString(item.Name)
  109. ti := theTime.Add(time.Minute*5*time.Duration(k))
  110. row.AddCell().SetString(fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
  111. row.AddCell().SetInt(v)
  112. }
  113. }
  114. name := fmt.Sprintf("./static/统计-登录-%s.xlsx", user)
  115. os.Remove(name)
  116. err = file.Save(name)
  117. if err != nil {
  118. beego.Warn(err)
  119. return ""
  120. }
  121. return name
  122. }
  123. // 玩家登录记录
  124. func LoginGetPlayerDayRecords(uid int, date string) RecordLoginArray {
  125. var dayRecords RecordLoginArray
  126. f, err := os.Open(fmt.Sprintf("%s/login-%s.log", utils.GetKeyConf("events", "path"), date))
  127. if err != nil {
  128. return dayRecords
  129. }
  130. defer f.Close()
  131. buff := bufio.NewReader(f)
  132. for {
  133. line, err := buff.ReadString('\n')
  134. if err != nil || io.EOF == err {
  135. break
  136. }
  137. line = strings.TrimSpace(line)
  138. arr := strings.Split(line, ";")
  139. _uid := utils.StringToInt(arr[2])
  140. if uid == _uid {
  141. info := &RecordLogin{
  142. EventTime: arr[0],
  143. ServerId: arr[1],
  144. Uid: _uid,
  145. RegisteTime: utils.StringToInt64(arr[3]),
  146. IP: arr[4],
  147. }
  148. dayRecords = append(dayRecords, info)
  149. }
  150. }
  151. return dayRecords
  152. }
  153. func LoginGetPlayerRangeDayRecords(uid int, days int) RecordLoginArray {
  154. var records RecordLoginArray
  155. currTime := int(time.Now().Unix())
  156. dayCount := 0
  157. for {
  158. tiEvent := currTime - 86400*dayCount
  159. date := time.Unix(int64(tiEvent), 0).Format("2006-01-02")
  160. dayRecords := LoginGetPlayerDayRecords(uid, date)
  161. records = append(records, dayRecords...)
  162. dayCount ++
  163. if dayCount > days {
  164. break
  165. }
  166. }
  167. return records
  168. }
  169. // IP登录记录
  170. func LoginGetIPDayRecords(ip string, date string) RecordLoginArray {
  171. var dayRecords RecordLoginArray
  172. f, err := os.Open(fmt.Sprintf("%s/login-%s.log", utils.GetKeyConf("events", "path"), date))
  173. if err != nil {
  174. return dayRecords
  175. }
  176. defer f.Close()
  177. buff := bufio.NewReader(f)
  178. for {
  179. line, err := buff.ReadString('\n')
  180. if err != nil || io.EOF == err {
  181. break
  182. }
  183. line = strings.TrimSpace(line)
  184. arr := strings.Split(line, ";")
  185. if ip == arr[4] {
  186. info := &RecordLogin{
  187. EventTime: arr[0],
  188. ServerId: arr[1],
  189. Uid: utils.StringToInt(arr[2]),
  190. RegisteTime: utils.StringToInt64(arr[3]),
  191. IP: arr[4],
  192. }
  193. dayRecords = append(dayRecords, info)
  194. }
  195. }
  196. return dayRecords
  197. }
  198. func LoginGetIPRangeDayRecords(ip string, days int) RecordLoginArray {
  199. var records RecordLoginArray
  200. currTime := int(time.Now().Unix())
  201. dayCount := 0
  202. for {
  203. tiEvent := currTime - 86400*dayCount
  204. date := time.Unix(int64(tiEvent), 0).Format("2006-01-02")
  205. dayRecords := LoginGetIPDayRecords(ip, date)
  206. records = append(records, dayRecords...)
  207. dayCount ++
  208. if dayCount > days {
  209. break
  210. }
  211. }
  212. return records
  213. }