warning_provider_count.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package warning
  4. import (
  5. "context"
  6. "gd_crontab/apis"
  7. "strings"
  8. "time"
  9. "github.com/astaxie/beego/orm"
  10. "gopkg.in/mgo.v2/bson"
  11. )
  12. type MgoProviderApiCount struct {
  13. Id bson.ObjectId `json:"_id" bson:"_id"`
  14. MerchantChildApiId int64 `json:"merchant_child_api_id" bson:"merchant_child_api_id"`
  15. ProviderApiId int64 `json:"provider_api_id" bson:"provider_api_id"`
  16. DayRemainRateThreshold float64 `json:"day_remain_rate_threshold" bson:"day_remain_rate_threshold"`
  17. }
  18. // recomputeGlobalUsed 将用量加上数据源跑批用量
  19. func recomputeGlobalUsed(req *apis.WarningProviderCountReq) {
  20. o := orm.NewOrm()
  21. sql := `select sum(1) from t_gd_try_api_access_log where api_id=? and create_time like "%??%"`
  22. sql = strings.Replace(sql, "??", time.Now().Format("2006-01-02"), -1)
  23. used := 0
  24. o.Raw(sql, req.ProviderApiId).QueryRow(&used)
  25. req.GlobalDayUsed += used
  26. }
  27. // warningProviderGlobalCount 数据源用量预警
  28. func warningProviderGlobalCount(req *apis.WarningProviderCountReq) error {
  29. /*collection := mongo.Session.DB(accessDB).C(providerGlobalTab)
  30. mpac := MgoProviderApiCount{}
  31. // 阀值信息
  32. var remainConf = ProviderDayCountThresholds
  33. // 设置收件箱
  34. w := apis.Warning{}
  35. w.To = strings.Split(config.Conf.Warning.DefaultMails, ";")
  36. // 未限制调用量直接返回
  37. if req.GlobalDayCount == 0 {
  38. collection.Remove(bson.M{"provider_api_id": req.ProviderApiId})
  39. return nil
  40. }
  41. // 用量重新计算,加上数据源跑批中的用量
  42. recomputeGlobalUsed(req)
  43. remain := req.GlobalDayCount - req.GlobalDayUsed
  44. if remain < 0 {
  45. remain = 0
  46. }
  47. remainRate := float64(remain) / float64(req.GlobalDayCount)
  48. lock(req.ProviderApiName, 1)
  49. defer unLock(req.ProviderApiName, 1)
  50. text := ""
  51. // 获取已经发送过邮件的预警信息
  52. collection.Find(bson.M{"provider_api_id": req.ProviderApiId}).One(&mpac)
  53. defer func() {
  54. // 预警信息入库,并发送邮件
  55. if text == "" {
  56. return
  57. }
  58. if mpac.Id.Valid() == false {
  59. mpac.ProviderApiId = req.ProviderApiId
  60. mongo.MgoInsertMulti(collection, true, mpac)
  61. } else {
  62. collection.UpdateId(mpac.Id, mpac)
  63. }
  64. w.WarningText = append(w.WarningText,text)
  65. //w.WarningText = text
  66. Warning(&w)
  67. }()
  68. // 用量耗尽但已发送过预警邮件
  69. if remain == 0 && mpac.Id.Valid() && mpac.DayRemainRateThreshold == 0 {
  70. return nil
  71. }
  72. // 用量耗尽且没有发送过预警邮件
  73. if remain == 0 && (mpac.Id.Valid() == false || mpac.DayRemainRateThreshold > 0) {
  74. text = fmt.Sprintf("数据源 %s 接口当日用量已耗尽", req.ProviderApiName)
  75. mpac.DayRemainRateThreshold = 0
  76. w.Subject = fmt.Sprintf("%s %s 数据源用量告警", req.ProviderName, req.ProviderApiName)
  77. return nil
  78. }
  79. // 用量达到某个阀值且未发送过邮件
  80. for _, v := range remainConf {
  81. if remainRate < v && (mpac.DayRemainRateThreshold > v || mpac.DayRemainRateThreshold == 0) {
  82. text = fmt.Sprintf("数据源 %s 接口当日用量已不足%d%%,剩余用量为%d,请关注",
  83. req.ProviderApiName, int(100.0*v), remain)
  84. mpac.DayRemainRateThreshold = v
  85. w.Subject = fmt.Sprintf("%s %s 数据源用量告警", req.ProviderName, req.ProviderApiName)
  86. return nil
  87. }
  88. }*/
  89. return nil
  90. }
  91. // WarningProviderCount 数据源用量预警
  92. func WarningProviderCount(ctx context.Context, req *apis.WarningProviderCountReq, reply *apis.WarningProviderCountReply) error {
  93. warningProviderGlobalCount(req)
  94. return nil
  95. }