abtest.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "go-common/app/service/openplatform/abtest/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/database/sql"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _selAllByStatus = "SELECT `id`,`name`,`desc`,`stra`,`seed`,`result`,`status`,`version`,`group`,`author_name`,`modifier_name`,`ctime`,`mtime` FROM `abtest` WHERE `deleted`=0 AND `status` IN (%s) ORDER BY mtime DESC LIMIT %d, %d"
  14. _selAllByStatusAndGroup = "SELECT `id`,`name`,`desc`,`stra`,`seed`,`result`,`status`,`version`,`group`,`author_name`,`modifier_name`,`ctime`,`mtime` FROM `abtest` WHERE `group`=? AND `deleted`=0 AND `status` IN (%s) ORDER BY mtime DESC LIMIT %d, %d"
  15. _selActByGroup = "SELECT `id`,`name`,`desc`,`stra`,`seed`,`result`,`status`,`version`,`group`,`author_name`,`modifier_name`,`ctime`,`mtime` FROM `abtest` WHERE `group`=? AND `status`=1 AND `deleted`=0"
  16. _selCnt = "SELECT count(*) as `count` FROM `abtest` WHERE `deleted`=0 AND `status` IN (%s)"
  17. _selCntByGroup = "SELECT count(*) as `count` FROM `abtest` WHERE `group`=? AND `deleted`=0 AND `status` IN (%s)"
  18. _selByID = "SELECT `id`,`name`,`desc`,`stra`,`seed`,`result`,`status`,`version`,`group`,`author_name`,`modifier_name`,`ctime`,`mtime` FROM `abtest` WHERE `id`=? AND `deleted`=0"
  19. _selByIDAndGroup = "SELECT `id`,`name`,`desc`,`stra`,`seed`,`result`,`status`,`version`,`group`,`author_name`,`modifier_name`,`ctime`,`mtime` FROM `abtest` WHERE `id`=? AND `group`=? AND `deleted`=0"
  20. _insAB = "INSERT INTO `abtest` (`name`,`desc`,`stra`,`seed`,`result`,`status`,`group`,`author_name`,`modifier_name`) VALUES(?,?,?,?,?,0,?,?,?)"
  21. _upAB = "UPDATE `abtest` SET `name`=?,`desc`=?,`stra`=?,`result`=?,`modifier_name`=?,`version`=? WHERE `id`=?"
  22. _upStatus = "UPDATE `abtest` SET `status`=?,`modifier_name`=? WHERE `id`=?"
  23. _delAB = "UPDATE `abtest` SET `deleted`=1 WHERE `id`=? AND `status`!=1"
  24. )
  25. //ActByGroup 根据group获取当前激活项目
  26. func (d *Dao) ActByGroup(c context.Context, group int) (res []*model.AB, err error) {
  27. var (
  28. rows *sql.Rows
  29. straStr string
  30. )
  31. if rows, err = d.db.Query(c, _selActByGroup, group); err != nil {
  32. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ActByGroup] d.db.Query err: %v", err))
  33. return
  34. }
  35. defer rows.Close()
  36. res = make([]*model.AB, 0)
  37. for rows.Next() {
  38. ele := &model.AB{}
  39. if err = rows.Scan(&ele.ID, &ele.Name, &ele.Desc, &straStr, &ele.Seed, &ele.Result, &ele.Status, &ele.Version, &ele.Group, &ele.Author, &ele.Modifier, &ele.CreateTime, &ele.ModifyTime); err != nil {
  40. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ActByGroup] rows.Scan err: %v", err))
  41. return
  42. }
  43. if err = json.Unmarshal([]byte(straStr), &ele.Stra); err != nil {
  44. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ActByGroup] json.Unmarshal err: %v", err))
  45. return
  46. }
  47. res = append(res, ele)
  48. }
  49. return
  50. }
  51. //Ab 获取单个测试
  52. func (d *Dao) Ab(c context.Context, id int) (res *model.AB, err error) {
  53. var straStr string
  54. row := d.db.QueryRow(c, _selByID, id)
  55. res = &model.AB{}
  56. if err = row.Scan(&res.ID, &res.Name, &res.Desc, &straStr, &res.Seed, &res.Result, &res.Status, &res.Version, &res.Group, &res.Author, &res.Modifier, &res.CreateTime, &res.ModifyTime); err != nil {
  57. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|Ab] row.Scan err: %v", err))
  58. return
  59. }
  60. err = json.Unmarshal([]byte(straStr), &res.Stra)
  61. return
  62. }
  63. //AbByIDAndGroup 获取单个测试
  64. func (d *Dao) AbByIDAndGroup(c context.Context, id int, group int) (res *model.AB, err error) {
  65. var (
  66. straStr string
  67. row *sql.Row
  68. )
  69. row = d.db.QueryRow(c, _selByIDAndGroup, id, group)
  70. res = &model.AB{}
  71. if err = row.Scan(&res.ID, &res.Name, &res.Desc, &straStr, &res.Seed, &res.Result, &res.Status, &res.Version, &res.Group, &res.Author, &res.Modifier, &res.CreateTime, &res.ModifyTime); err != nil {
  72. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|AbByIDAndGroup] row.Scan err: %v", err))
  73. return
  74. }
  75. err = json.Unmarshal([]byte(straStr), &res.Stra)
  76. return
  77. }
  78. //CountAb 获取测试数量
  79. func (d *Dao) CountAb(c context.Context, mstatus string) (count int, err error) {
  80. row := d.db.QueryRow(c, fmt.Sprintf(_selCnt, mstatus))
  81. if err = row.Scan(&count); err != nil {
  82. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|CountAb] row.Scan err: %v", err))
  83. return
  84. }
  85. return
  86. }
  87. //CountAbByGroup 获取测试数量
  88. func (d *Dao) CountAbByGroup(c context.Context, mstatus string, group int) (count int, err error) {
  89. row := d.db.QueryRow(c, fmt.Sprintf(_selCntByGroup, mstatus), group)
  90. if err = row.Scan(&count); err != nil {
  91. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|CountAbByGroup] row.Scan err: %v", err))
  92. return
  93. }
  94. return
  95. }
  96. //ListAb 分页获取所有测试列表
  97. func (d *Dao) ListAb(c context.Context, offset, size int, mstatus string) (res []*model.AB, count int, err error) {
  98. var (
  99. rows *sql.Rows
  100. straStr string
  101. )
  102. if rows, err = d.db.Query(c, fmt.Sprintf(_selAllByStatus, mstatus, offset, size)); err != nil {
  103. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ListAb] d.db.Query err: %v", err))
  104. return
  105. }
  106. defer rows.Close()
  107. res = make([]*model.AB, 0)
  108. for rows.Next() {
  109. ele := &model.AB{}
  110. if err = rows.Scan(&ele.ID, &ele.Name, &ele.Desc, &straStr, &ele.Seed, &ele.Result, &ele.Status, &ele.Version, &ele.Group, &ele.Author, &ele.Modifier, &ele.CreateTime, &ele.ModifyTime); err != nil {
  111. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ListAb] rows.Scan err: %v", err))
  112. return
  113. }
  114. err = json.Unmarshal([]byte(straStr), &ele.Stra)
  115. if err != nil {
  116. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ListAb] json.Unmarshal err: %v", err))
  117. continue
  118. }
  119. res = append(res, ele)
  120. }
  121. count, err = d.CountAb(c, mstatus)
  122. return
  123. }
  124. //ListAbByGroup 分页获取分组测试列表
  125. func (d *Dao) ListAbByGroup(c context.Context, offset, size int, mstatus string, group int) (res []*model.AB, count int, err error) {
  126. var (
  127. rows *sql.Rows
  128. straStr string
  129. )
  130. if rows, err = d.db.Query(c, fmt.Sprintf(_selAllByStatusAndGroup, mstatus, offset, size), group); err != nil {
  131. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ListAbByGroup] d.db.Query err: %v", err))
  132. return
  133. }
  134. defer rows.Close()
  135. res = make([]*model.AB, 0)
  136. for rows.Next() {
  137. ele := &model.AB{}
  138. if err = rows.Scan(&ele.ID, &ele.Name, &ele.Desc, &straStr, &ele.Seed, &ele.Result, &ele.Status, &ele.Version, &ele.Group, &ele.Author, &ele.Modifier, &ele.CreateTime, &ele.ModifyTime); err != nil {
  139. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ListAbByGroup] rows.Scan err: %v", err))
  140. return
  141. }
  142. err = json.Unmarshal([]byte(straStr), &ele.Stra)
  143. if err != nil {
  144. err = errors.Wrap(err, fmt.Sprintf("[dao.abtest|ListAbByGroup] json.Unmarshal err: %v", err))
  145. continue
  146. }
  147. res = append(res, ele)
  148. }
  149. count, err = d.CountAbByGroup(c, mstatus, group)
  150. return
  151. }
  152. //AddAb 添加AB实验
  153. func (d *Dao) AddAb(c context.Context, name, desc string, stra string, seed, result, group int, username string) (newID int64, err error) {
  154. var res xsql.Result
  155. if res, err = d.db.Exec(c, _insAB, name, desc, stra, seed, result, group, username, username); err != nil {
  156. return
  157. }
  158. newID, err = res.LastInsertId()
  159. return
  160. }
  161. //DelAb 删除AB实验
  162. func (d *Dao) DelAb(c context.Context, id int) (rowsAffected int64, err error) {
  163. var res xsql.Result
  164. if res, err = d.db.Exec(c, _delAB, id); err != nil {
  165. return
  166. }
  167. return res.RowsAffected()
  168. }
  169. //UpAb 更新AB实验
  170. func (d *Dao) UpAb(c context.Context, id int, name, desc string, stra string, result int, username string, newVersion, status, group int) (rowsAffected int64, err error) {
  171. var res xsql.Result
  172. if res, err = d.db.Exec(c, _upAB, name, desc, stra, result, username, newVersion, id); err != nil {
  173. return
  174. }
  175. return res.RowsAffected()
  176. }
  177. //UpStatus 更新AB实验状态
  178. func (d *Dao) UpStatus(c context.Context, id, status int, username string, group int) (rowsAffected int64, err error) {
  179. var res xsql.Result
  180. if res, err = d.db.Exec(c, _upStatus, status, username, id); err != nil {
  181. return
  182. }
  183. return res.RowsAffected()
  184. }
  185. //Incr stat result, save to redis
  186. func (d *Dao) Incr(c context.Context, key string) {
  187. conn := d.redis.Get(c)
  188. defer conn.Close()
  189. conn.Do("incr", key)
  190. conn.Do("expire", key, 86400*7)
  191. }
  192. //Move backup key
  193. func (d *Dao) Move(c context.Context, key string) {
  194. conn := d.redis.Get(c)
  195. defer conn.Close()
  196. i, _ := redis.Int(conn.Do("get", key))
  197. if _, err := conn.Do("setex", "O:"+key, 86400*7, i); err == nil {
  198. conn.Do("del", key)
  199. }
  200. }
  201. // GetFromRedis .
  202. func (d *Dao) GetFromRedis(c context.Context, key string) (value int, err error) {
  203. conn := d.redis.Get(c)
  204. defer conn.Close()
  205. value, err = redis.Int(conn.Do("get", key))
  206. return
  207. }