log_query_thirdpart_access_log_count.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package access_log
  2. import (
  3. "context"
  4. "gd_statistics/apis"
  5. "gd_statistics/errors"
  6. "fmt"
  7. "github.com/astaxie/beego/orm"
  8. "strconv"
  9. "time"
  10. )
  11. func getThirdpartAccesslogCountWhere(req *apis.LogQueryThirdpartAccessLogCountReq) (w string, s []interface{}) {
  12. if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
  13. w += " and " + "timestamp >=" + " ? and timestamp <?"
  14. s = append(s, req.StartTimestamp, req.EndTimestamp)
  15. } else {
  16. nowTime := time.Now()
  17. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  18. startTimeStamp := zeroTime.Unix()
  19. endTimeStamp := nowTime.Unix()
  20. w += " and " + "timestamp >= " + " ? and timestamp < ?"
  21. s = append(s, startTimeStamp, endTimeStamp)
  22. }
  23. if req.ProviderApiId != 0 {
  24. w += " and " + "provider_api_id" + " = ?"
  25. s = append(s, req.ProviderApiId)
  26. }
  27. if req.ProviderId != 0 {
  28. w += " and " + "provider_id" + " = ?"
  29. s = append(s, req.ProviderId)
  30. }
  31. if len(s) > 0 {
  32. //有条件才截取最前面的and
  33. w = string([]byte(w)[4:])
  34. w = " where" + w
  35. return w, s
  36. } else {
  37. return "", nil
  38. }
  39. }
  40. func LogQueryThirdpartAccessLogCountNew(ctx context.Context, req *apis.LogQueryThirdpartAccessLogCountReq, reply *apis.LogQueryThirdpartAccessLogCountReply) error {
  41. if req.StartTimestamp == 0 || req.EndTimestamp == 0 {
  42. nowTime := time.Now()
  43. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  44. req.StartTimestamp = zeroTime.Unix()
  45. req.EndTimestamp = nowTime.Unix()
  46. }
  47. exportTimes := getExportTimes(req.StartTimestamp, req.EndTimestamp)
  48. for _, v := range exportTimes {
  49. req.StartTimestamp = v.start
  50. req.EndTimestamp = v.end
  51. tabName := getMonthTab("t_gd_thirdpart_log_month", req.StartTimestamp)
  52. subReply := &apis.LogQueryThirdpartAccessLogCountReply{}
  53. err := logQueryThirdpartAccessLogCount(req, subReply, tabName)
  54. if err != nil {
  55. return err
  56. }
  57. reply.QueryThirdpartAccessLogCounts = append(reply.QueryThirdpartAccessLogCounts, subReply.QueryThirdpartAccessLogCounts...)
  58. }
  59. if req.PageSize == 0 {
  60. req.PageSize = PAGESIZE
  61. }
  62. if req.PageNumber == 0 {
  63. req.PageNumber = 1
  64. }
  65. reply.Total = int64(len(reply.QueryThirdpartAccessLogCounts))
  66. for i := 0; i < int(reply.Total); i++ {
  67. for j := i + 1; j < int(reply.Total); j++ {
  68. if reply.QueryThirdpartAccessLogCounts[i].ProviderApiId > reply.QueryThirdpartAccessLogCounts[j].ProviderApiId {
  69. tmp := reply.QueryThirdpartAccessLogCounts[i]
  70. reply.QueryThirdpartAccessLogCounts[i] = reply.QueryThirdpartAccessLogCounts[j]
  71. reply.QueryThirdpartAccessLogCounts[j] = tmp
  72. }
  73. }
  74. }
  75. offset := req.PageSize * (req.PageNumber - 1)
  76. switch {
  77. case reply.Total-offset <= 0:
  78. reply.QueryThirdpartAccessLogCounts = []apis.QueryThirdpartAccessLogCount{}
  79. case reply.Total-offset <= req.PageSize:
  80. reply.QueryThirdpartAccessLogCounts = reply.QueryThirdpartAccessLogCounts[offset:]
  81. case reply.Total-offset > req.PageSize:
  82. reply.QueryThirdpartAccessLogCounts = reply.QueryThirdpartAccessLogCounts[offset : offset+req.PageSize]
  83. }
  84. return nil
  85. }
  86. func logQueryThirdpartAccessLogCount(req *apis.LogQueryThirdpartAccessLogCountReq, reply *apis.LogQueryThirdpartAccessLogCountReply, tabName string) error {
  87. where, val := getThirdpartAccesslogCountWhere(req)
  88. sqlStr := "select create_time as date,provider_api_id, provider_name,provider_api_name,count(state) as total,sum(state) as success,avg(elapsed) as elapsed from " + tabName + " %s group by create_time,provider_id,provider_api_id order by create_time desc"
  89. sqlStr = fmt.Sprintf(sqlStr, where)
  90. _, err := orm.NewOrm().Raw(sqlStr, val).QueryRows(&reply.QueryThirdpartAccessLogCounts)
  91. if err != nil && err != orm.ErrNoRows {
  92. return errors.DataBaseError
  93. }
  94. for index, _ := range reply.QueryThirdpartAccessLogCounts {
  95. reply.QueryThirdpartAccessLogCounts[index].Failed = reply.QueryThirdpartAccessLogCounts[index].Total - reply.QueryThirdpartAccessLogCounts[index].Success
  96. if reply.QueryThirdpartAccessLogCounts[index].Total != 0 {
  97. reply.QueryThirdpartAccessLogCounts[index].SuccessRate = strconv.FormatFloat(float64(100*reply.QueryThirdpartAccessLogCounts[index].Success)/float64(reply.QueryThirdpartAccessLogCounts[index].Total), 'f', 2, 64) + "%"
  98. }
  99. }
  100. return nil
  101. }
  102. func LogQueryThirdpartAccessLogCount(ctx context.Context, req *apis.LogQueryThirdpartAccessLogCountReq, reply *apis.LogQueryThirdpartAccessLogCountReply) error {
  103. if true {
  104. return LogQueryThirdpartAccessLogCountNew(ctx, req, reply)
  105. }
  106. if req.PageSize == 0 {
  107. req.PageSize = PAGESIZE
  108. }
  109. if req.PageNumber == 0 {
  110. req.PageNumber = 1
  111. }
  112. limit := strconv.FormatInt((req.PageNumber-1)*req.PageSize, 10) + "," + strconv.FormatInt(req.PageSize, 10)
  113. where, val := getThirdpartAccesslogCountWhere(req)
  114. sqlStr := "select create_time as date,provider_name,provider_api_name,count(state) as total,sum(state) as success,avg(elapsed) as elapsed from t_gd_thirdpart_access_log %s group by create_time,provider_id,provider_api_id order by create_time desc limit %s"
  115. sqlStr = fmt.Sprintf(sqlStr, where, limit)
  116. var ids []int
  117. count, err := orm.NewOrm().Raw("select id from t_gd_thirdpart_access_log "+where+" group by create_time,provider_id,provider_api_id", val).QueryRows(&ids)
  118. if err != nil && err != orm.ErrNoRows {
  119. return errors.DataBaseError
  120. }
  121. reply.Total = count
  122. _, err = orm.NewOrm().Raw(sqlStr, val).QueryRows(&reply.QueryThirdpartAccessLogCounts)
  123. if err != nil && err != orm.ErrNoRows {
  124. return errors.DataBaseError
  125. }
  126. for index, _ := range reply.QueryThirdpartAccessLogCounts {
  127. reply.QueryThirdpartAccessLogCounts[index].Failed = reply.QueryThirdpartAccessLogCounts[index].Total - reply.QueryThirdpartAccessLogCounts[index].Success
  128. if reply.QueryThirdpartAccessLogCounts[index].Total != 0 {
  129. reply.QueryThirdpartAccessLogCounts[index].SuccessRate = strconv.FormatFloat(float64(100*reply.QueryThirdpartAccessLogCounts[index].Success)/float64(reply.QueryThirdpartAccessLogCounts[index].Total), 'f', 2, 64) + "%"
  130. }
  131. }
  132. reply.PageNumber = req.PageNumber
  133. reply.PageSize = req.PageSize
  134. return nil
  135. }