bi_api_service_get_income.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package access_log
  4. import (
  5. "context"
  6. "gd_statistics/apis"
  7. "gd_statistics/errors"
  8. "strconv"
  9. )
  10. func sortApiIncomeDetailInfo(array []apis.ApiServiceIncomeDetail) []apis.ApiServiceIncomeDetail {
  11. length := len(array)
  12. for i := 0; i < length; i++ {
  13. for j := i + 1; j < length; j++ {
  14. if array[i].Charge < array[j].Charge {
  15. tmp := array[i]
  16. array[i] = array[j]
  17. array[j] = tmp
  18. }
  19. }
  20. }
  21. return array
  22. }
  23. func sortApiIncomeInfo(array []apis.ApiServiceIncomeInfo) []apis.ApiServiceIncomeInfo {
  24. length := len(array)
  25. for i := 0; i < length; i++ {
  26. for j := i + 1; j < length; j++ {
  27. if array[i].Charge < array[j].Charge {
  28. tmp := array[i]
  29. array[i] = array[j]
  30. array[j] = tmp
  31. }
  32. }
  33. }
  34. for i, v := range array {
  35. array[i].Detail = sortApiIncomeDetailInfo(v.Detail)
  36. }
  37. return array
  38. }
  39. func apiServiceGetIncomeInfo(ctx context.Context, req *apis.BiGetApiServiceIncomeInfoReq, start, end int64, isToday bool) ([]apis.ApiServiceIncomeInfo, error) {
  40. mreq := apis.BiGetReportReq{}
  41. mreply := apis.BiGetReportReply{}
  42. mreq.IsAll = true
  43. mreq.TodayEndTimestamp = req.TodayEndTimestamp
  44. mreq.Date = getDatesFromTimestamp(start, end)
  45. var err error
  46. if isToday {
  47. mreply.Infos, err = getTodayReportFromHour(&mreq, start, end)
  48. if err != nil {
  49. return nil, err
  50. }
  51. } else {
  52. err = BiGetReport(ctx, &mreq, &mreply)
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. m := map[string]map[string]*apis.BiReportInfo{}
  58. for i, v := range mreply.Infos {
  59. if _, ok := m[v.ApiName]; ok == false {
  60. item := map[string]*apis.BiReportInfo{}
  61. item[v.MerchantName] = &mreply.Infos[i]
  62. m[v.ApiName] = item
  63. continue
  64. }
  65. if _, ok := m[v.ApiName][v.MerchantName]; ok == false {
  66. m[v.ApiName][v.MerchantName] = &mreply.Infos[i]
  67. continue
  68. }
  69. m[v.ApiName][v.MerchantName].Charge += v.Charge
  70. m[v.ApiName][v.MerchantName].Valid += v.Valid
  71. m[v.ApiName][v.MerchantName].Total += v.Total
  72. }
  73. ret := []apis.ApiServiceIncomeInfo{}
  74. for apiName, apiMap := range m {
  75. item := apis.ApiServiceIncomeInfo{}
  76. item.ApiName = apiName
  77. for _, api := range apiMap {
  78. apiItem := apis.ApiServiceIncomeDetail{}
  79. item.Charge += api.Charge
  80. apiItem.Charge = api.Charge
  81. apiItem.Valid = api.Valid
  82. apiItem.MerchantName = api.MerchantName
  83. item.Detail = append(item.Detail, apiItem)
  84. }
  85. ret = append(ret, item)
  86. }
  87. for i, v := range ret {
  88. for k, d := range ret[i].Detail {
  89. if v.Charge == 0 {
  90. ret[i].Detail[k].Rate = "0%"
  91. } else {
  92. ret[i].Detail[k].Rate = strconv.FormatFloat(float64(100*d.Charge/v.Charge), 'f', 2, 64) + "%"
  93. }
  94. }
  95. }
  96. return ret, nil
  97. }
  98. func BiGetApiServiceIncomeInfo(ctx context.Context, req *apis.BiGetApiServiceIncomeInfoReq, reply *apis.BiGetApiServiceIncomeInfoReply) (err error) {
  99. // 参数预处理
  100. if req.PageNumber <= 0 {
  101. req.PageNumber = 1
  102. }
  103. if req.PageSize <= 0 {
  104. req.PageSize = pageSize
  105. }
  106. reply.PageNumber = req.PageNumber
  107. reply.PageSize = req.PageSize
  108. offset := (req.PageNumber - 1) * req.PageSize
  109. limit := req.PageSize
  110. start := int64(0)
  111. end := int64(0)
  112. isToday := false
  113. // 今日,昨日,本周,本月
  114. switch req.TimeType {
  115. case 0:
  116. isToday = true
  117. start, end = getTodayTimestamp()
  118. case 1:
  119. start, end = getYesterdayTimestamp()
  120. case 2:
  121. start, end = getWeekTimestamp()
  122. case 3:
  123. start, end = getMonthTimestamp()
  124. default:
  125. return errors.ArgsError
  126. }
  127. // 获取所有信息
  128. infos, err := apiServiceGetIncomeInfo(ctx, req, start, end, isToday)
  129. if err != nil {
  130. return err
  131. }
  132. reply.Total = int64(len(infos))
  133. infos = sortApiIncomeInfo(infos)
  134. // 分页
  135. switch {
  136. case reply.Total-int64(offset) <= 0:
  137. reply.Infos = []apis.ApiServiceIncomeInfo{}
  138. case reply.Total-int64(offset) <= int64(limit):
  139. reply.Infos = infos[offset:]
  140. case reply.Total-int64(offset) > int64(limit):
  141. reply.Infos = infos[offset : offset+limit]
  142. }
  143. return nil
  144. }