client.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package events
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "strings"
  9. "time"
  10. "sort"
  11. "box-gm/utils"
  12. // "log"
  13. )
  14. type RecordClient struct {
  15. EventTime int64
  16. ServerId string
  17. Scene string
  18. EventId string
  19. Label string
  20. }
  21. type ClientRecordList []*RecordClient
  22. func (a ClientRecordList) Len() int { return len(a) }
  23. func (a ClientRecordList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  24. func (a ClientRecordList) Less(i, j int) bool {
  25. return a[i].EventTime > a[j].EventTime
  26. }
  27. func (a ClientRecordList) Sort() { sort.Sort(a) }
  28. func Client_get_records(scene, eid string, startTime, endTime int) []*RecordClient {
  29. // 日期列表
  30. var dateList []string
  31. times := 0
  32. for {
  33. ti := startTime + times*86400
  34. if ti >= endTime {
  35. break
  36. }
  37. date := time.Unix(int64(ti), 0).Format("2006-01-02")
  38. dateList = append(dateList, date)
  39. times++
  40. }
  41. // log.Printf("ChatQuery_get_records dateList[%v]", dateList)
  42. var records ClientRecordList
  43. loc, _ := time.LoadLocation("Local")
  44. for _, date := range dateList {
  45. eventFile := fmt.Sprintf("%s/client-%s.log", utils.GetKeyConf("events", "path"), date)
  46. f, err := os.Open(eventFile)
  47. if err != nil {
  48. log.Printf("eventFile[%s] err[%v]", eventFile, err)
  49. continue
  50. }
  51. defer f.Close()
  52. buff := bufio.NewReader(f)
  53. for {
  54. line, err := buff.ReadString('\n')
  55. if err != nil || io.EOF == err {
  56. break
  57. }
  58. line = strings.TrimSpace(line)
  59. /* string.format(
  60. "%s;%s;%s;%s;%s;%s;%s",
  61. get_time_string(),
  62. get_server_id(),
  63. _tostring(uid),
  64. _tostring(toUid),
  65. _tostring(msgty),
  66. _tostring(iCType),
  67. _tostring(cnt)*/
  68. arr := strings.Split(line, ";")
  69. theTime, errTime := time.ParseInLocation("2006-01-02 15:04:05", arr[0], loc)
  70. if errTime == nil {
  71. ti := int(theTime.Unix())
  72. if ti >= startTime && ti <= endTime {
  73. // 场景
  74. if scene == "" || scene == arr[2] {
  75. // 事件id
  76. if eid == "" || eid == arr[3] {
  77. item := &RecordClient{}
  78. item.EventTime = utils.GetTime64(arr[0])
  79. item.ServerId = arr[1]
  80. item.Scene = arr[2]
  81. item.EventId = arr[3]
  82. item.Label = arr[4]
  83. records = append(records, item)
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. records.Sort()
  91. // log.Printf("ChatQuery_get_records records[%v]", records)
  92. return records
  93. }