log_query_thirdpart_access_count.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package access_log
  2. import (
  3. "context"
  4. "gd_statistics/apis"
  5. "gd_statistics/errors"
  6. "gd_statistics/rpc_apis"
  7. "gd_statistics/rpc_apis/gd_management"
  8. "github.com/astaxie/beego/orm"
  9. "time"
  10. )
  11. func DateToTimestamp(date string) int64 {
  12. timeLayout := "2006-01-02"
  13. loc, _ := time.LoadLocation("Local")
  14. t, _ := time.ParseInLocation(timeLayout, date, loc)
  15. return t.Unix()
  16. }
  17. func LogQueryThirdpartAccessCountNew(ctx context.Context, req *apis.LogQueryThirdpartAccessCountReq, reply *apis.LogQueryThirdpartAccessCountReply) error {
  18. if req.Time == "" {
  19. req.Time = time.Now().Format("2006-01-02")
  20. }
  21. tabName := getMonthTab("t_gd_thirdpart_log_month", DateToTimestamp(req.Time))
  22. o := orm.NewOrm()
  23. querySet := o.QueryTable(tabName)
  24. mReq := gd_management.ManagementGetProviderApiLimitCountReq{req.ProviderApiId}
  25. mreply, err := rpc_apis.Management.ManagementGetProviderApiLimitCount(ctx, &mReq)
  26. if err != nil {
  27. return err
  28. }
  29. reply.Total = mreply.Count
  30. querySet = querySet.Filter("create_time", req.Time).Filter("provider_api_id", mreply.ProviderApiId)
  31. reply.UseCount, err = querySet.Count()
  32. if err != nil && err != orm.ErrNoRows {
  33. return errors.DataBaseError
  34. }
  35. if reply.Total == 0 {
  36. reply.LeftCount = 0
  37. } else {
  38. reply.LeftCount = reply.Total - reply.UseCount
  39. }
  40. return nil
  41. }
  42. func LogQueryThirdpartAccessCount(ctx context.Context, req *apis.LogQueryThirdpartAccessCountReq, reply *apis.LogQueryThirdpartAccessCountReply) error {
  43. if true {
  44. return LogQueryThirdpartAccessCountNew(ctx, req, reply)
  45. }
  46. o := orm.NewOrm()
  47. querySet := o.QueryTable("t_gd_thirdpart_access_log")
  48. /*if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
  49. querySet = querySet.Filter("timestamp__gte", req.StartTimestamp).Filter("timestamp__lte", req.EndTimestamp)
  50. } else {
  51. endTimeStamp := time.Now().Unix()
  52. startTimeStamp := endTimeStamp - 24*60*60
  53. cond := orm.NewCondition()
  54. cond = cond.And("timestamp__gte", startTimeStamp).And("timestamp__lte", endTimeStamp)
  55. querySet = querySet.SetCond(cond)
  56. }*/
  57. if req.Time == "" {
  58. req.Time = time.Now().Format("2006-01-02")
  59. }
  60. mReq := gd_management.ManagementGetProviderApiLimitCountReq{req.ProviderApiId}
  61. mreply, err := rpc_apis.Management.ManagementGetProviderApiLimitCount(ctx, &mReq)
  62. if err != nil {
  63. return err
  64. }
  65. reply.Total = mreply.Count
  66. querySet = querySet.Filter("create_time", req.Time).Filter("provider_api_id", mreply.ProviderApiId)
  67. reply.UseCount, err = querySet.Count()
  68. if err != nil && err != orm.ErrNoRows {
  69. return errors.DataBaseError
  70. }
  71. if reply.Total == 0 {
  72. reply.LeftCount = 0
  73. } else {
  74. reply.LeftCount = reply.Total - reply.UseCount
  75. }
  76. return nil
  77. }