12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package access_log
- import (
- "context"
- "gd_statistics/apis"
- "gd_statistics/errors"
- "gd_statistics/rpc_apis"
- "gd_statistics/rpc_apis/gd_management"
- "github.com/astaxie/beego/orm"
- "time"
- )
- func DateToTimestamp(date string) int64 {
- timeLayout := "2006-01-02"
- loc, _ := time.LoadLocation("Local")
- t, _ := time.ParseInLocation(timeLayout, date, loc)
- return t.Unix()
- }
- func LogQueryThirdpartAccessCountNew(ctx context.Context, req *apis.LogQueryThirdpartAccessCountReq, reply *apis.LogQueryThirdpartAccessCountReply) error {
- if req.Time == "" {
- req.Time = time.Now().Format("2006-01-02")
- }
- tabName := getMonthTab("t_gd_thirdpart_log_month", DateToTimestamp(req.Time))
- o := orm.NewOrm()
- querySet := o.QueryTable(tabName)
- mReq := gd_management.ManagementGetProviderApiLimitCountReq{req.ProviderApiId}
- mreply, err := rpc_apis.Management.ManagementGetProviderApiLimitCount(ctx, &mReq)
- if err != nil {
- return err
- }
- reply.Total = mreply.Count
- querySet = querySet.Filter("create_time", req.Time).Filter("provider_api_id", mreply.ProviderApiId)
- reply.UseCount, err = querySet.Count()
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- if reply.Total == 0 {
- reply.LeftCount = 0
- } else {
- reply.LeftCount = reply.Total - reply.UseCount
- }
- return nil
- }
- func LogQueryThirdpartAccessCount(ctx context.Context, req *apis.LogQueryThirdpartAccessCountReq, reply *apis.LogQueryThirdpartAccessCountReply) error {
- if true {
- return LogQueryThirdpartAccessCountNew(ctx, req, reply)
- }
- o := orm.NewOrm()
- querySet := o.QueryTable("t_gd_thirdpart_access_log")
- /*if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
- querySet = querySet.Filter("timestamp__gte", req.StartTimestamp).Filter("timestamp__lte", req.EndTimestamp)
- } else {
- endTimeStamp := time.Now().Unix()
- startTimeStamp := endTimeStamp - 24*60*60
- cond := orm.NewCondition()
- cond = cond.And("timestamp__gte", startTimeStamp).And("timestamp__lte", endTimeStamp)
- querySet = querySet.SetCond(cond)
- }*/
- if req.Time == "" {
- req.Time = time.Now().Format("2006-01-02")
- }
- mReq := gd_management.ManagementGetProviderApiLimitCountReq{req.ProviderApiId}
- mreply, err := rpc_apis.Management.ManagementGetProviderApiLimitCount(ctx, &mReq)
- if err != nil {
- return err
- }
- reply.Total = mreply.Count
- querySet = querySet.Filter("create_time", req.Time).Filter("provider_api_id", mreply.ProviderApiId)
- reply.UseCount, err = querySet.Count()
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- if reply.Total == 0 {
- reply.LeftCount = 0
- } else {
- reply.LeftCount = reply.Total - reply.UseCount
- }
- return nil
- }
|