abtest.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package abtest
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "go-common/app/interface/main/app-resource/conf"
  9. "go-common/app/interface/main/app-resource/model/experiment"
  10. "go-common/library/database/sql"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/xstr"
  15. "github.com/pkg/errors"
  16. )
  17. const (
  18. _selExpLimit = `SELECT experiment_id,build,conditions FROM experiment_limit`
  19. _selExpByIDs = `SELECT id,name,plat,strategy,description,traffic_group FROM experiment WHERE state=1 AND id IN (%s) ORDER BY uptime DESC`
  20. )
  21. // Dao is notice dao.
  22. type Dao struct {
  23. db *sql.DB
  24. limit *sql.Stmt
  25. dataPath string
  26. client *bm.Client
  27. }
  28. // New new a notice dao.
  29. func New(c *conf.Config) (d *Dao) {
  30. d = &Dao{
  31. db: sql.NewMySQL(c.MySQL.Show),
  32. client: bm.NewClient(conf.Conf.HTTPClient),
  33. dataPath: c.Host.Data + "/abserver/v1/app/match-exp",
  34. }
  35. d.limit = d.db.Prepared(_selExpLimit)
  36. return
  37. }
  38. func (d *Dao) ExperimentLimit(c context.Context) (lm map[int64][]*experiment.Limit, err error) {
  39. rows, err := d.limit.Query(c)
  40. if err != nil {
  41. log.Error("d.limit.Query error (%v)", err)
  42. return
  43. }
  44. defer rows.Close()
  45. lm = map[int64][]*experiment.Limit{}
  46. for rows.Next() {
  47. limit := &experiment.Limit{}
  48. if err = rows.Scan(&limit.ExperimentID, &limit.Build, &limit.Condition); err != nil {
  49. log.Error("rows.Scan err (%v)", err)
  50. continue
  51. }
  52. lm[limit.ExperimentID] = append(lm[limit.ExperimentID], limit)
  53. }
  54. return
  55. }
  56. func (d *Dao) ExperimentByIDs(c context.Context, ids []int64) (eps []*experiment.Experiment, err error) {
  57. rows, err := d.db.Query(c, fmt.Sprintf(_selExpByIDs, xstr.JoinInts(ids)))
  58. if err != nil {
  59. log.Error("d.expByIDs.Query error(%v)", err)
  60. return
  61. }
  62. defer rows.Close()
  63. for rows.Next() {
  64. ep := &experiment.Experiment{}
  65. if err = rows.Scan(&ep.ID, &ep.Name, &ep.Plat, &ep.Strategy, &ep.Desc, &ep.TrafficGroup); err != nil {
  66. log.Error("rows.Scan err (%v)", err)
  67. continue
  68. }
  69. eps = append(eps, ep)
  70. }
  71. return
  72. }
  73. // AbServer http://info.bilibili.co/pages/viewpage.action?pageId=8741843 大数据abtest
  74. func (d *Dao) AbServer(c context.Context, buvid, device, mobiAPP, filteredStr string, build int, mid int64) (res json.RawMessage, err error) {
  75. params := url.Values{}
  76. params.Set("buvid", buvid)
  77. params.Set("device", device)
  78. params.Set("mobi_app", mobiAPP)
  79. params.Set("build", strconv.Itoa(build))
  80. if mid > 0 {
  81. params.Set("mid", strconv.FormatInt(mid, 10))
  82. }
  83. if filteredStr != "" {
  84. params.Set("filtered", filteredStr)
  85. }
  86. var data struct {
  87. Code int `json:"errorCode"`
  88. }
  89. if err = d.client.Get(c, d.dataPath, "", params, &res); err != nil {
  90. return
  91. }
  92. if err = json.Unmarshal(res, &data); err != nil {
  93. err = errors.Wrap(err, "json.Unmarshal")
  94. return
  95. }
  96. if data.Code != ecode.OK.Code() {
  97. log.Warn("code(%d) path:(%s)", data.Code, d.dataPath+params.Encode())
  98. return
  99. }
  100. return
  101. }