123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package warning
- import (
- "context"
- "gd_crontab/apis"
- "strings"
- "time"
- "github.com/astaxie/beego/orm"
- "gopkg.in/mgo.v2/bson"
- )
- type MgoProviderApiCount struct {
- Id bson.ObjectId `json:"_id" bson:"_id"`
- MerchantChildApiId int64 `json:"merchant_child_api_id" bson:"merchant_child_api_id"`
- ProviderApiId int64 `json:"provider_api_id" bson:"provider_api_id"`
- DayRemainRateThreshold float64 `json:"day_remain_rate_threshold" bson:"day_remain_rate_threshold"`
- }
- // recomputeGlobalUsed 将用量加上数据源跑批用量
- func recomputeGlobalUsed(req *apis.WarningProviderCountReq) {
- o := orm.NewOrm()
- sql := `select sum(1) from t_gd_try_api_access_log where api_id=? and create_time like "%??%"`
- sql = strings.Replace(sql, "??", time.Now().Format("2006-01-02"), -1)
- used := 0
- o.Raw(sql, req.ProviderApiId).QueryRow(&used)
- req.GlobalDayUsed += used
- }
- // warningProviderGlobalCount 数据源用量预警
- func warningProviderGlobalCount(req *apis.WarningProviderCountReq) error {
- /*collection := mongo.Session.DB(accessDB).C(providerGlobalTab)
- mpac := MgoProviderApiCount{}
- // 阀值信息
- var remainConf = ProviderDayCountThresholds
- // 设置收件箱
- w := apis.Warning{}
- w.To = strings.Split(config.Conf.Warning.DefaultMails, ";")
- // 未限制调用量直接返回
- if req.GlobalDayCount == 0 {
- collection.Remove(bson.M{"provider_api_id": req.ProviderApiId})
- return nil
- }
- // 用量重新计算,加上数据源跑批中的用量
- recomputeGlobalUsed(req)
- remain := req.GlobalDayCount - req.GlobalDayUsed
- if remain < 0 {
- remain = 0
- }
- remainRate := float64(remain) / float64(req.GlobalDayCount)
- lock(req.ProviderApiName, 1)
- defer unLock(req.ProviderApiName, 1)
- text := ""
- // 获取已经发送过邮件的预警信息
- collection.Find(bson.M{"provider_api_id": req.ProviderApiId}).One(&mpac)
- defer func() {
- // 预警信息入库,并发送邮件
- if text == "" {
- return
- }
- if mpac.Id.Valid() == false {
- mpac.ProviderApiId = req.ProviderApiId
- mongo.MgoInsertMulti(collection, true, mpac)
- } else {
- collection.UpdateId(mpac.Id, mpac)
- }
- w.WarningText = append(w.WarningText,text)
- //w.WarningText = text
- Warning(&w)
- }()
- // 用量耗尽但已发送过预警邮件
- if remain == 0 && mpac.Id.Valid() && mpac.DayRemainRateThreshold == 0 {
- return nil
- }
- // 用量耗尽且没有发送过预警邮件
- if remain == 0 && (mpac.Id.Valid() == false || mpac.DayRemainRateThreshold > 0) {
- text = fmt.Sprintf("数据源 %s 接口当日用量已耗尽", req.ProviderApiName)
- mpac.DayRemainRateThreshold = 0
- w.Subject = fmt.Sprintf("%s %s 数据源用量告警", req.ProviderName, req.ProviderApiName)
- return nil
- }
- // 用量达到某个阀值且未发送过邮件
- for _, v := range remainConf {
- if remainRate < v && (mpac.DayRemainRateThreshold > v || mpac.DayRemainRateThreshold == 0) {
- text = fmt.Sprintf("数据源 %s 接口当日用量已不足%d%%,剩余用量为%d,请关注",
- req.ProviderApiName, int(100.0*v), remain)
- mpac.DayRemainRateThreshold = v
- w.Subject = fmt.Sprintf("%s %s 数据源用量告警", req.ProviderName, req.ProviderApiName)
- return nil
- }
- }*/
- return nil
- }
- // WarningProviderCount 数据源用量预警
- func WarningProviderCount(ctx context.Context, req *apis.WarningProviderCountReq, reply *apis.WarningProviderCountReply) error {
- warningProviderGlobalCount(req)
- return nil
- }
|