123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- package crontab
- import (
- "context"
- "gd_crontab/apis"
- "gd_crontab/errors"
- "fmt"
- "strconv"
- "strings"
- "time"
- "gd_crontab/common.in/utils"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- )
- func getInterfaceCountWhere(req *apis.LogQueryInterfaceCountReq, querySet orm.QuerySeter) orm.QuerySeter {
- if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
- cond := orm.NewCondition()
- cond = cond.And("timestamp__gte", req.StartTimestamp).And("timestamp__lt", req.EndTimestamp)
- querySet = querySet.SetCond(cond)
- } 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()
- cond := orm.NewCondition()
- cond = cond.And("timestamp__gte", startTimeStamp).And("timestamp__lt", endTimeStamp)
- querySet = querySet.SetCond(cond)
- }
- if req.ApiId != 0 {
- querySet = querySet.Filter("api_id", req.ApiId)
- }
- if req.MerchantId != 0 {
- querySet = querySet.Filter("merchant_id", req.MerchantId)
- }
- return querySet
- }
- func LogQueryInterfaceCount(ctx context.Context, req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceCountReply) error {
- querySet := orm.NewOrm().QueryTable("t_gd_access_log")
- querySet = getInterfaceCountWhere(req, querySet)
- /*count, err := querySet.Count()
- if err != nil {
- return errors.DataBaseError
- }
- reply.Total = count*/
- reply.LogQuerySuccessCount.Query, _ = querySet.Filter("code", 0).Count() // 查得
- reply.LogQuerySuccessCount.UnQuery, _ = querySet.Filter("code", 1100).Count() // 查无
- reply.LogQuerySuccessCount.Reuse, _ = querySet.Filter("is_reuse", 1).Filter("code", 0).Count() // 复用
- reply.LogQueryFailCount.ProviderFail, _ = querySet.Filter("code", 1101).Count() // 三方失败
- reply.LogQueryFailCount.ServiceFail, _ = querySet.Filter("code__in", 1000, 1001).Count() // 平台错误
- reply.LogQueryInvalidCount.ParamError, _ = querySet.Filter("code", 1103).Count() // 参数错误
- reply.LogQueryInvalidCount.ParamFormatError, _ = querySet.Filter("code", 1102).Count() // 参数格式错误
- validCount := (reply.LogQuerySuccessCount.UnQuery + reply.LogQuerySuccessCount.Query + reply.LogQueryFailCount.ProviderFail + reply.LogQueryFailCount.ServiceFail)
- reply.Total = validCount
- if validCount == 0 {
- return nil
- }
- reply.SuccessRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.UnQuery+reply.LogQuerySuccessCount.Query)*100/float64(validCount))/100), 64)
- reply.ReuseRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.Reuse)*100/float64(validCount))/100), 64)
- reply.QueryRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.Query)*100/float64(validCount))/100), 64)
- reply.FailRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQueryFailCount.ProviderFail+reply.LogQueryFailCount.ServiceFail)*100/float64(validCount))/100), 64)
- reply.UnQueryRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.UnQuery)*100/float64(validCount))/100), 64)
- reply.ProviderFailRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQueryFailCount.ProviderFail)*100/float64(validCount))/100), 64)
- reply.ServiceFailRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQueryFailCount.ServiceFail)*100/float64(validCount))/100), 64)
- return nil
- }
- func getInterfaceAnalyzeErrorWhere(req *apis.LogQueryInterfaceCountReq) string {
- sql := "SELECT COUNT(*) as `count`, raw_code as code, msg FROM " + req.TabName + " WHERE"
- if req.GroupByState {
- sql = "SELECT COUNT(*) as `count`, raw_code as code, state, msg FROM " + req.TabName + " WHERE"
- }
- if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
- sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, 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()
- sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, startTimeStamp, endTimeStamp)
- }
- if req.ApiId != 0 {
- sql = fmt.Sprintf("%s AND `api_id`=%d", sql, req.ApiId)
- }
- if req.MerchantId != 0 {
- sql = fmt.Sprintf("%s AND `merchant_id`=%d", sql, req.MerchantId)
- }
- if req.Option == 1 {
- sql = fmt.Sprintf("%s AND (`code`=0 OR `code`=1100)", sql)
- }
- if req.Option == 2 {
- sql = fmt.Sprintf("%s AND (`code`<>0 AND `code`<>1100)", sql)
- }
- if req.GroupByState {
- return sql + " GROUP BY `raw_code`, `state`"
- }
- return sql + " GROUP BY `raw_code`"
- }
- func logQueryInterfaceAnalyzeError(req *apis.LogQueryInterfaceCountReq, m map[string]int) error {
- o := orm.NewOrm()
- array := make([]apis.LogQueryInterfaceAnalyzeError, 0)
- //sql组装
- sql := getInterfaceAnalyzeErrorWhere(req)
- _, err := o.Raw(sql).QueryRows(&array)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- for _, v := range array {
- key := fmt.Sprintf("%v----%v----%v", v.Code, v.Msg, v.State)
- if _, ok := m[key]; ok == false {
- m[key] = v.Count
- } else {
- m[key] += v.Count
- }
- }
- return nil
- }
- func LogQueryInterfaceAnalyzeErrorNew(req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceAnalyzeErrorReply) error {
- if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
- now := time.Now()
- req.EndTimestamp = now.Unix()
- req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
- }
- m := map[string]int{}
- if req.TabName == "t_gd_access_log_day" {
- if err := logQueryInterfaceAnalyzeError(req, m); err != nil {
- return err
- }
- } else {
- exportTimes := getExportTimes(req.StartTimestamp, req.EndTimestamp)
- for _, v := range exportTimes {
- req.StartTimestamp = v.start
- req.EndTimestamp = v.end
- req.TabName = getMonthTab("t_gd_access_log_month", req.StartTimestamp)
- err := logQueryInterfaceAnalyzeError(req, m)
- if err != nil {
- return err
- }
- }
- }
- i := 0
- reply.List = make([]apis.LogQueryInterfaceAnalyzeError, len(m))
- for k, v := range m {
- array := strings.Split(k, "----")
- if len(array) > 2 {
- reply.List[i].Code, _ = strconv.Atoi(array[0])
- reply.List[i].Msg = array[1]
- if array[2] == "false" {
- reply.List[i].State = false
- } else {
- reply.List[i].State = true
- }
- }
- reply.List[i].Count = v
- i++
- }
- return nil
- }
- // 接口查询错误分析
- func LogQueryInterfaceAnalyzeError(ctx context.Context, req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceAnalyzeErrorReply) error {
- if true {
- return LogQueryInterfaceAnalyzeErrorNew(req, reply)
- }
- if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
- now := time.Now()
- req.EndTimestamp = now.Unix()
- req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
- }
- result := make([]apis.LogQueryInterfaceAnalyzeError, 0)
- if req.TabName == "" {
- req.TabName = "t_gd_access_log"
- }
- //sql组装
- sql := getInterfaceAnalyzeErrorWhere(req)
- _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- reply.List = result
- return nil
- }
- func getLogQueryInterfaceThirdPartyCountWhere(req *apis.LogQueryInterfaceCountReq) string {
- sql := "SELECT COUNT(*) as `count`, provider_name, provider_api_name FROM t_gd_thirdpart_access_log WHERE"
- if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
- sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, 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()
- sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, startTimeStamp, endTimeStamp)
- }
- if req.ApiId != 0 {
- sql = fmt.Sprintf("%s AND `api_id`=%d", sql, req.ApiId)
- }
- if req.MerchantId != 0 {
- sql = fmt.Sprintf("%s AND `merchant_id`=%d", sql, req.MerchantId)
- }
- return sql + " GROUP BY `provider_api_id`"
- }
- // 数据源请求分析
- func LogQueryInterfaceThirdPartyCount(ctx context.Context, req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceThirdPartyCountReply) error {
- result := make([]apis.LogQueryInterfaceThirdPartyCount, 0)
- //获取sql
- sql := getLogQueryInterfaceThirdPartyCountWhere(req)
- _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- reply.List = result
- return nil
- }
- func getThirdPartyInterfaceAnalyzeWhere(req *apis.ThirdPartyInterfaceErrorAnalyzeReq) string {
- sql := "SELECT COUNT(*) as `count`, raw_code as code, msg FROM " + req.TabName + " WHERE"
- if req.GroupByState {
- sql = "SELECT COUNT(*) as `count`, raw_code as code, state, msg FROM " + req.TabName + " WHERE"
- }
- if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
- sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, 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()
- //startTimeStamp := endTimeStamp - 24*60*60
- sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, startTimeStamp, endTimeStamp)
- }
- if req.ApiId != 0 {
- sql = fmt.Sprintf("%s AND `provider_api_id`=%d", sql, req.ApiId)
- }
- if req.GroupByState {
- return sql + " GROUP BY `raw_code`, `state`"
- }
- return sql + " GROUP BY `raw_code`"
- }
- func thirdPartyInterfaceErrorAnalyze(req *apis.ThirdPartyInterfaceErrorAnalyzeReq, m map[string]int) error {
- //获取sql
- result := []apis.ThirdPartyInterfaceErrorAnalyze{}
- sql := getThirdPartyInterfaceAnalyzeWhere(req)
- _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- for _, v := range result {
- key := fmt.Sprintf("%v----%v----%v", v.Code, v.Msg, v.State)
- if _, ok := m[key]; ok == false {
- m[key] = v.Count
- } else {
- m[key] += v.Count
- }
- }
- return nil
- }
- func ThirdPartyInterfaceErrorAnalyzeNew(req *apis.ThirdPartyInterfaceErrorAnalyzeReq, reply *apis.ThirdPartyInterfaceErrorAnalyzeReply) error {
- if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
- now := time.Now()
- req.EndTimestamp = now.Unix()
- req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
- }
- m := map[string]int{}
- if req.TabName == "t_gd_thirdpart_log_day" {
- if err := thirdPartyInterfaceErrorAnalyze(req, m); err != nil {
- return err
- }
- } else {
- exportTimes := getExportTimes(req.StartTimestamp, req.EndTimestamp)
- for _, v := range exportTimes {
- req.StartTimestamp = v.start
- req.EndTimestamp = v.end
- req.TabName = getMonthTab("t_gd_thirdpart_log_month", req.StartTimestamp)
- err := thirdPartyInterfaceErrorAnalyze(req, m)
- if err != nil {
- return err
- }
- }
- }
- i := 0
- reply.List = make([]apis.ThirdPartyInterfaceErrorAnalyze, len(m))
- for k, v := range m {
- array := strings.Split(k, "----")
- if len(array) > 2 {
- reply.List[i].Code = array[0]
- reply.List[i].Msg = array[1]
- if array[2] == "false" {
- reply.List[i].State = false
- } else {
- reply.List[i].State = true
- }
- }
- reply.List[i].Count = v
- i++
- }
- return nil
- }
- // 数据源异常分析
- func ThirdPartyInterfaceErrorAnalyze(ctx context.Context, req *apis.ThirdPartyInterfaceErrorAnalyzeReq, reply *apis.ThirdPartyInterfaceErrorAnalyzeReply) error {
- if true {
- return ThirdPartyInterfaceErrorAnalyzeNew(req, reply)
- }
- if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
- now := time.Now()
- req.EndTimestamp = now.Unix()
- req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
- }
- result := make([]apis.ThirdPartyInterfaceErrorAnalyze, 0)
- if req.TabName == "" {
- req.TabName = "t_gd_thirdpart_access_log"
- }
- //获取sql
- sql := getThirdPartyInterfaceAnalyzeWhere(req)
- _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- reply.List = result
- return nil
- }
|