123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package access_log
- import (
- "context"
- "gd_statistics/apis"
- "gd_statistics/errors"
- "fmt"
- "github.com/astaxie/beego/orm"
- "strconv"
- "time"
- )
- func getThirdpartAccesslogCountWhere(req *apis.LogQueryThirdpartAccessLogCountReq) (w string, s []interface{}) {
- if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
- w += " and " + "timestamp >=" + " ? and timestamp <?"
- s = append(s, req.StartTimestamp, req.EndTimestamp)
- } else {
- nowTime := time.Now()
- zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
- startTimeStamp := zeroTime.Unix()
- endTimeStamp := nowTime.Unix()
- w += " and " + "timestamp >= " + " ? and timestamp < ?"
- s = append(s, startTimeStamp, endTimeStamp)
- }
- if req.ProviderApiId != 0 {
- w += " and " + "provider_api_id" + " = ?"
- s = append(s, req.ProviderApiId)
- }
- if req.ProviderId != 0 {
- w += " and " + "provider_id" + " = ?"
- s = append(s, req.ProviderId)
- }
- if len(s) > 0 {
- //有条件才截取最前面的and
- w = string([]byte(w)[4:])
- w = " where" + w
- return w, s
- } else {
- return "", nil
- }
- }
- func LogQueryThirdpartAccessLogCountNew(ctx context.Context, req *apis.LogQueryThirdpartAccessLogCountReq, reply *apis.LogQueryThirdpartAccessLogCountReply) error {
- if req.StartTimestamp == 0 || req.EndTimestamp == 0 {
- nowTime := time.Now()
- zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
- req.StartTimestamp = zeroTime.Unix()
- req.EndTimestamp = nowTime.Unix()
- }
- exportTimes := getExportTimes(req.StartTimestamp, req.EndTimestamp)
- for _, v := range exportTimes {
- req.StartTimestamp = v.start
- req.EndTimestamp = v.end
- tabName := getMonthTab("t_gd_thirdpart_log_month", req.StartTimestamp)
- subReply := &apis.LogQueryThirdpartAccessLogCountReply{}
- err := logQueryThirdpartAccessLogCount(req, subReply, tabName)
- if err != nil {
- return err
- }
- reply.QueryThirdpartAccessLogCounts = append(reply.QueryThirdpartAccessLogCounts, subReply.QueryThirdpartAccessLogCounts...)
- }
- if req.PageSize == 0 {
- req.PageSize = PAGESIZE
- }
- if req.PageNumber == 0 {
- req.PageNumber = 1
- }
- reply.Total = int64(len(reply.QueryThirdpartAccessLogCounts))
- for i := 0; i < int(reply.Total); i++ {
- for j := i + 1; j < int(reply.Total); j++ {
- if reply.QueryThirdpartAccessLogCounts[i].ProviderApiId > reply.QueryThirdpartAccessLogCounts[j].ProviderApiId {
- tmp := reply.QueryThirdpartAccessLogCounts[i]
- reply.QueryThirdpartAccessLogCounts[i] = reply.QueryThirdpartAccessLogCounts[j]
- reply.QueryThirdpartAccessLogCounts[j] = tmp
- }
- }
- }
- offset := req.PageSize * (req.PageNumber - 1)
- switch {
- case reply.Total-offset <= 0:
- reply.QueryThirdpartAccessLogCounts = []apis.QueryThirdpartAccessLogCount{}
- case reply.Total-offset <= req.PageSize:
- reply.QueryThirdpartAccessLogCounts = reply.QueryThirdpartAccessLogCounts[offset:]
- case reply.Total-offset > req.PageSize:
- reply.QueryThirdpartAccessLogCounts = reply.QueryThirdpartAccessLogCounts[offset : offset+req.PageSize]
- }
- return nil
- }
- func logQueryThirdpartAccessLogCount(req *apis.LogQueryThirdpartAccessLogCountReq, reply *apis.LogQueryThirdpartAccessLogCountReply, tabName string) error {
- where, val := getThirdpartAccesslogCountWhere(req)
- sqlStr := "select create_time as date,provider_api_id, provider_name,provider_api_name,count(state) as total,sum(state) as success,avg(elapsed) as elapsed from " + tabName + " %s group by create_time,provider_id,provider_api_id order by create_time desc"
- sqlStr = fmt.Sprintf(sqlStr, where)
- _, err := orm.NewOrm().Raw(sqlStr, val).QueryRows(&reply.QueryThirdpartAccessLogCounts)
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- for index, _ := range reply.QueryThirdpartAccessLogCounts {
- reply.QueryThirdpartAccessLogCounts[index].Failed = reply.QueryThirdpartAccessLogCounts[index].Total - reply.QueryThirdpartAccessLogCounts[index].Success
- if reply.QueryThirdpartAccessLogCounts[index].Total != 0 {
- reply.QueryThirdpartAccessLogCounts[index].SuccessRate = strconv.FormatFloat(float64(100*reply.QueryThirdpartAccessLogCounts[index].Success)/float64(reply.QueryThirdpartAccessLogCounts[index].Total), 'f', 2, 64) + "%"
- }
- }
- return nil
- }
- func LogQueryThirdpartAccessLogCount(ctx context.Context, req *apis.LogQueryThirdpartAccessLogCountReq, reply *apis.LogQueryThirdpartAccessLogCountReply) error {
- if true {
- return LogQueryThirdpartAccessLogCountNew(ctx, req, reply)
- }
- if req.PageSize == 0 {
- req.PageSize = PAGESIZE
- }
- if req.PageNumber == 0 {
- req.PageNumber = 1
- }
- limit := strconv.FormatInt((req.PageNumber-1)*req.PageSize, 10) + "," + strconv.FormatInt(req.PageSize, 10)
- where, val := getThirdpartAccesslogCountWhere(req)
- sqlStr := "select create_time as date,provider_name,provider_api_name,count(state) as total,sum(state) as success,avg(elapsed) as elapsed from t_gd_thirdpart_access_log %s group by create_time,provider_id,provider_api_id order by create_time desc limit %s"
- sqlStr = fmt.Sprintf(sqlStr, where, limit)
- var ids []int
- count, err := orm.NewOrm().Raw("select id from t_gd_thirdpart_access_log "+where+" group by create_time,provider_id,provider_api_id", val).QueryRows(&ids)
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- reply.Total = count
- _, err = orm.NewOrm().Raw(sqlStr, val).QueryRows(&reply.QueryThirdpartAccessLogCounts)
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- for index, _ := range reply.QueryThirdpartAccessLogCounts {
- reply.QueryThirdpartAccessLogCounts[index].Failed = reply.QueryThirdpartAccessLogCounts[index].Total - reply.QueryThirdpartAccessLogCounts[index].Success
- if reply.QueryThirdpartAccessLogCounts[index].Total != 0 {
- reply.QueryThirdpartAccessLogCounts[index].SuccessRate = strconv.FormatFloat(float64(100*reply.QueryThirdpartAccessLogCounts[index].Success)/float64(reply.QueryThirdpartAccessLogCounts[index].Total), 'f', 2, 64) + "%"
- }
- }
- reply.PageNumber = req.PageNumber
- reply.PageSize = req.PageSize
- return nil
- }
|