uilog.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package http
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "go-common/app/interface/bbq/common/model"
  7. "go-common/library/log"
  8. "go-common/library/log/infoc"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  11. jsoniter "github.com/json-iterator/go"
  12. )
  13. // UILog .
  14. type UILog struct {
  15. infoc *infoc.Infoc
  16. }
  17. // New .
  18. func New(c *infoc.Config) *UILog {
  19. return &UILog{
  20. infoc: infoc.New(c),
  21. }
  22. }
  23. // Infoc .
  24. func (u *UILog) Infoc(ctx *bm.Context, action int, ext interface{}) {
  25. arg := new(model.Base)
  26. ctx.Bind(arg)
  27. log.V(5).Info("uilog [%+v]", arg)
  28. if action == model.ActionPlay {
  29. // data type 1 心跳上报 2 暂停 3 home
  30. switch arg.DataType {
  31. case 2:
  32. action = model.ActionPlayPause
  33. case 3:
  34. action = model.ActionPlayOut
  35. }
  36. }
  37. // interface base field
  38. app := arg.App
  39. client := arg.Client
  40. version := arg.Version
  41. channel := arg.Channel
  42. loc := arg.Location
  43. cip := ctx.Request.RemoteAddr
  44. if ips := ctx.Request.Header.Get("X-Forwarded-For"); ips != "" {
  45. ipArr := strings.Split(ips, ",")
  46. if len(ipArr) > 0 {
  47. cip = ipArr[0]
  48. }
  49. }
  50. // session id
  51. sid, _ := ctx.Get("SessionID")
  52. // trace id
  53. tracer, _ := trace.FromContext(ctx.Context)
  54. tid := tracer
  55. // recsys query id
  56. qid := arg.QueryID
  57. if action == model.ActionRecommend {
  58. qid = fmt.Sprintf("%s", tracer)
  59. }
  60. // video
  61. svid := arg.SVID
  62. // user
  63. var mid int64
  64. if tmp, ok := ctx.Get("mid"); ok {
  65. mid = tmp.(int64)
  66. }
  67. buvid := ctx.Request.Header.Get("Buvid")
  68. totalDuration := arg.TotalDuration
  69. playDuration := arg.PlayDuration
  70. ctime := time.Now().Unix()
  71. b, _ := jsoniter.Marshal(ext)
  72. u.infoc.Info(client, app, version, channel, loc, cip, qid, sid, tid, svid, mid, buvid, action, totalDuration, playDuration, ctime, string(b), arg.From, arg.FromID, arg.PFrom, arg.PFromID)
  73. }