task_oper_history.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/videoup-task/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _avgUtimeSQL = "SELECT IFNULL(avg(utime),0.0) FROM task_oper_history WHERE action=2 AND uid=? AND ctime>=? AND ctime<?"
  11. _sumDurationSQL = "SELECT IFNULL(sum(v.duration),0) FROM task_oper_history as t LEFT JOIN video as v ON t.cid=v.id WHERE action IN (2,5) AND t.uid=? AND t.ctime>=? AND t.ctime<?"
  12. _actionCountSQL = "SELECT action,count(*) FROM task_oper_history WHERE uid=? AND ctime>=? AND ctime<? AND action IN (2,5,6,7) GROUP BY action"
  13. _passCountSQL = "SELECT count(*) FROM task_oper_history WHERE action IN (2,5) AND uid=? AND ctime>=? AND ctime<? AND result=0"
  14. _subjectCountSQL = `SELECT count(*) FROM (
  15. SELECT id FROM task_dispatch WHERE uid=? AND state=? AND mtime>=? AND ctime<? AND subject=1
  16. UNION ALL SELECT task_id as id FROM task_dispatch_done WHERE uid=? AND state=? AND mtime>=? AND ctime<? AND subject=1) as t
  17. `
  18. _activeUidsSQL = "SELECT DISTINCT uid from task_oper_history WHERE ctime>=? AND ctime<? AND uid!=0 AND action IN (2,5)"
  19. )
  20. // AvgUtimeByUID 平均处理耗时, 只统计action=2的
  21. func (d *Dao) AvgUtimeByUID(c context.Context, uid int64, stime, etime time.Time) (utime float64, err error) {
  22. if err = d.arcReadDB.QueryRow(c, _avgUtimeSQL, uid, stime, etime).Scan(&utime); err != nil {
  23. if err == sql.ErrNoRows {
  24. err = nil
  25. }
  26. log.Error("d.arcReadDB.QueryRow error(%v)", err)
  27. }
  28. return
  29. }
  30. // SumDurationByUID 视频总时长,统计action=2,5的
  31. func (d *Dao) SumDurationByUID(c context.Context, uid int64, stime, etime time.Time) (duration int64, err error) {
  32. if err = d.arcReadDB.QueryRow(c, _sumDurationSQL, uid, stime, etime).Scan(&duration); err != nil {
  33. if err == sql.ErrNoRows {
  34. err = nil
  35. }
  36. log.Error("d.arcReadDB.QueryRow error(%v)", err)
  37. }
  38. return
  39. }
  40. // ActionCountByUID 操作个数统计
  41. func (d *Dao) ActionCountByUID(c context.Context, uid int64, stime, etime time.Time) (mapAction map[int8]int64, err error) {
  42. mapAction = make(map[int8]int64)
  43. rows, err := d.arcReadDB.Query(c, _actionCountSQL, uid, stime, etime)
  44. if err != nil {
  45. log.Error("d.arcReadDB.Query error(%v)", err)
  46. return
  47. }
  48. defer rows.Close()
  49. for rows.Next() {
  50. var (
  51. action int8
  52. count int64
  53. )
  54. if err = rows.Scan(&action, &count); err != nil {
  55. log.Error("rows.Scan error(%v)", err)
  56. return
  57. }
  58. mapAction[action] = count
  59. }
  60. return
  61. }
  62. // PassCountByUID 总过审个数
  63. func (d *Dao) PassCountByUID(c context.Context, uid int64, stime, etime time.Time) (count int64, err error) {
  64. if err = d.arcReadDB.QueryRow(c, _passCountSQL, uid, stime, etime).Scan(&count); err != nil {
  65. if err == sql.ErrNoRows {
  66. err = nil
  67. }
  68. log.Error("d.arcReadDB.QueryRow error(%v)", err)
  69. }
  70. return
  71. }
  72. // SubjectCountByUID 总指派个数
  73. func (d *Dao) SubjectCountByUID(c context.Context, uid int64, stime, etime time.Time) (count int64, err error) {
  74. if err = d.arcReadDB.QueryRow(c, _subjectCountSQL, uid, model.TaskStateCompleted, stime, etime, uid, model.TaskStateCompleted, stime, etime).Scan(&count); err != nil {
  75. if err == sql.ErrNoRows {
  76. err = nil
  77. }
  78. log.Error("d.arcReadDB.QueryRow error(%v)", err)
  79. }
  80. return
  81. }
  82. // ActiveUids 统计24小时内有提交的
  83. func (d *Dao) ActiveUids(c context.Context, stime, etime time.Time) (uids []int64, err error) {
  84. st := time.Now()
  85. defer func() {
  86. log.Info("ActiveUids du(%.2fm) wait(%.2fs)", etime.Sub(stime).Minutes(), time.Since(st).Seconds())
  87. }()
  88. rows, err := d.arcReadDB.Query(c, _activeUidsSQL, stime, etime)
  89. if err != nil {
  90. log.Error("d.arcReadDB.Query error(%v)", err)
  91. return
  92. }
  93. defer rows.Close()
  94. for rows.Next() {
  95. var uid int64
  96. if err = rows.Scan(&uid); err != nil {
  97. log.Error("rows.Scan error(%v)", err)
  98. return
  99. }
  100. uids = append(uids, uid)
  101. }
  102. return
  103. }