log_query_interface_count.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package crontab
  2. import (
  3. "context"
  4. "gd_crontab/apis"
  5. "gd_crontab/errors"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "gd_crontab/common.in/utils"
  11. "github.com/astaxie/beego/orm"
  12. "go.uber.org/zap"
  13. )
  14. func getInterfaceCountWhere(req *apis.LogQueryInterfaceCountReq, querySet orm.QuerySeter) orm.QuerySeter {
  15. if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
  16. cond := orm.NewCondition()
  17. cond = cond.And("timestamp__gte", req.StartTimestamp).And("timestamp__lt", req.EndTimestamp)
  18. querySet = querySet.SetCond(cond)
  19. } else {
  20. nowTime := time.Now()
  21. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  22. startTimeStamp := zeroTime.Unix()
  23. endTimeStamp := nowTime.Unix()
  24. cond := orm.NewCondition()
  25. cond = cond.And("timestamp__gte", startTimeStamp).And("timestamp__lt", endTimeStamp)
  26. querySet = querySet.SetCond(cond)
  27. }
  28. if req.ApiId != 0 {
  29. querySet = querySet.Filter("api_id", req.ApiId)
  30. }
  31. if req.MerchantId != 0 {
  32. querySet = querySet.Filter("merchant_id", req.MerchantId)
  33. }
  34. return querySet
  35. }
  36. func LogQueryInterfaceCount(ctx context.Context, req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceCountReply) error {
  37. querySet := orm.NewOrm().QueryTable("t_gd_access_log")
  38. querySet = getInterfaceCountWhere(req, querySet)
  39. /*count, err := querySet.Count()
  40. if err != nil {
  41. return errors.DataBaseError
  42. }
  43. reply.Total = count*/
  44. reply.LogQuerySuccessCount.Query, _ = querySet.Filter("code", 0).Count() // 查得
  45. reply.LogQuerySuccessCount.UnQuery, _ = querySet.Filter("code", 1100).Count() // 查无
  46. reply.LogQuerySuccessCount.Reuse, _ = querySet.Filter("is_reuse", 1).Filter("code", 0).Count() // 复用
  47. reply.LogQueryFailCount.ProviderFail, _ = querySet.Filter("code", 1101).Count() // 三方失败
  48. reply.LogQueryFailCount.ServiceFail, _ = querySet.Filter("code__in", 1000, 1001).Count() // 平台错误
  49. reply.LogQueryInvalidCount.ParamError, _ = querySet.Filter("code", 1103).Count() // 参数错误
  50. reply.LogQueryInvalidCount.ParamFormatError, _ = querySet.Filter("code", 1102).Count() // 参数格式错误
  51. validCount := (reply.LogQuerySuccessCount.UnQuery + reply.LogQuerySuccessCount.Query + reply.LogQueryFailCount.ProviderFail + reply.LogQueryFailCount.ServiceFail)
  52. reply.Total = validCount
  53. if validCount == 0 {
  54. return nil
  55. }
  56. reply.SuccessRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.UnQuery+reply.LogQuerySuccessCount.Query)*100/float64(validCount))/100), 64)
  57. reply.ReuseRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.Reuse)*100/float64(validCount))/100), 64)
  58. reply.QueryRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.Query)*100/float64(validCount))/100), 64)
  59. reply.FailRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQueryFailCount.ProviderFail+reply.LogQueryFailCount.ServiceFail)*100/float64(validCount))/100), 64)
  60. reply.UnQueryRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQuerySuccessCount.UnQuery)*100/float64(validCount))/100), 64)
  61. reply.ProviderFailRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQueryFailCount.ProviderFail)*100/float64(validCount))/100), 64)
  62. reply.ServiceFailRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (float64(reply.LogQueryFailCount.ServiceFail)*100/float64(validCount))/100), 64)
  63. return nil
  64. }
  65. func getInterfaceAnalyzeErrorWhere(req *apis.LogQueryInterfaceCountReq) string {
  66. sql := "SELECT COUNT(*) as `count`, raw_code as code, msg FROM " + req.TabName + " WHERE"
  67. if req.GroupByState {
  68. sql = "SELECT COUNT(*) as `count`, raw_code as code, state, msg FROM " + req.TabName + " WHERE"
  69. }
  70. if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
  71. sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, req.StartTimestamp, req.EndTimestamp)
  72. } else {
  73. nowTime := time.Now()
  74. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  75. startTimeStamp := zeroTime.Unix()
  76. endTimeStamp := nowTime.Unix()
  77. sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, startTimeStamp, endTimeStamp)
  78. }
  79. if req.ApiId != 0 {
  80. sql = fmt.Sprintf("%s AND `api_id`=%d", sql, req.ApiId)
  81. }
  82. if req.MerchantId != 0 {
  83. sql = fmt.Sprintf("%s AND `merchant_id`=%d", sql, req.MerchantId)
  84. }
  85. if req.Option == 1 {
  86. sql = fmt.Sprintf("%s AND (`code`=0 OR `code`=1100)", sql)
  87. }
  88. if req.Option == 2 {
  89. sql = fmt.Sprintf("%s AND (`code`<>0 AND `code`<>1100)", sql)
  90. }
  91. if req.GroupByState {
  92. return sql + " GROUP BY `raw_code`, `state`"
  93. }
  94. return sql + " GROUP BY `raw_code`"
  95. }
  96. func logQueryInterfaceAnalyzeError(req *apis.LogQueryInterfaceCountReq, m map[string]int) error {
  97. o := orm.NewOrm()
  98. array := make([]apis.LogQueryInterfaceAnalyzeError, 0)
  99. //sql组装
  100. sql := getInterfaceAnalyzeErrorWhere(req)
  101. _, err := o.Raw(sql).QueryRows(&array)
  102. if err != nil && err != orm.ErrNoRows {
  103. l.Error("mysql",
  104. zap.String("sql", sql),
  105. zap.String("fields", utils.MarshalJsonString(req)),
  106. zap.String("error", err.Error()))
  107. return errors.DataBaseError
  108. }
  109. for _, v := range array {
  110. key := fmt.Sprintf("%v----%v----%v", v.Code, v.Msg, v.State)
  111. if _, ok := m[key]; ok == false {
  112. m[key] = v.Count
  113. } else {
  114. m[key] += v.Count
  115. }
  116. }
  117. return nil
  118. }
  119. func LogQueryInterfaceAnalyzeErrorNew(req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceAnalyzeErrorReply) error {
  120. if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
  121. now := time.Now()
  122. req.EndTimestamp = now.Unix()
  123. req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
  124. }
  125. m := map[string]int{}
  126. if req.TabName == "t_gd_access_log_day" {
  127. if err := logQueryInterfaceAnalyzeError(req, m); err != nil {
  128. return err
  129. }
  130. } else {
  131. exportTimes := getExportTimes(req.StartTimestamp, req.EndTimestamp)
  132. for _, v := range exportTimes {
  133. req.StartTimestamp = v.start
  134. req.EndTimestamp = v.end
  135. req.TabName = getMonthTab("t_gd_access_log_month", req.StartTimestamp)
  136. err := logQueryInterfaceAnalyzeError(req, m)
  137. if err != nil {
  138. return err
  139. }
  140. }
  141. }
  142. i := 0
  143. reply.List = make([]apis.LogQueryInterfaceAnalyzeError, len(m))
  144. for k, v := range m {
  145. array := strings.Split(k, "----")
  146. if len(array) > 2 {
  147. reply.List[i].Code, _ = strconv.Atoi(array[0])
  148. reply.List[i].Msg = array[1]
  149. if array[2] == "false" {
  150. reply.List[i].State = false
  151. } else {
  152. reply.List[i].State = true
  153. }
  154. }
  155. reply.List[i].Count = v
  156. i++
  157. }
  158. return nil
  159. }
  160. // 接口查询错误分析
  161. func LogQueryInterfaceAnalyzeError(ctx context.Context, req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceAnalyzeErrorReply) error {
  162. if true {
  163. return LogQueryInterfaceAnalyzeErrorNew(req, reply)
  164. }
  165. if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
  166. now := time.Now()
  167. req.EndTimestamp = now.Unix()
  168. req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
  169. }
  170. result := make([]apis.LogQueryInterfaceAnalyzeError, 0)
  171. if req.TabName == "" {
  172. req.TabName = "t_gd_access_log"
  173. }
  174. //sql组装
  175. sql := getInterfaceAnalyzeErrorWhere(req)
  176. _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
  177. if err != nil && err != orm.ErrNoRows {
  178. l.Error("mysql",
  179. zap.String("sql", sql),
  180. zap.String("fields", utils.MarshalJsonString(req)),
  181. zap.String("error", err.Error()))
  182. return errors.DataBaseError
  183. }
  184. reply.List = result
  185. return nil
  186. }
  187. func getLogQueryInterfaceThirdPartyCountWhere(req *apis.LogQueryInterfaceCountReq) string {
  188. sql := "SELECT COUNT(*) as `count`, provider_name, provider_api_name FROM t_gd_thirdpart_access_log WHERE"
  189. if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
  190. sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, req.StartTimestamp, req.EndTimestamp)
  191. } else {
  192. nowTime := time.Now()
  193. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  194. startTimeStamp := zeroTime.Unix()
  195. endTimeStamp := nowTime.Unix()
  196. sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, startTimeStamp, endTimeStamp)
  197. }
  198. if req.ApiId != 0 {
  199. sql = fmt.Sprintf("%s AND `api_id`=%d", sql, req.ApiId)
  200. }
  201. if req.MerchantId != 0 {
  202. sql = fmt.Sprintf("%s AND `merchant_id`=%d", sql, req.MerchantId)
  203. }
  204. return sql + " GROUP BY `provider_api_id`"
  205. }
  206. // 数据源请求分析
  207. func LogQueryInterfaceThirdPartyCount(ctx context.Context, req *apis.LogQueryInterfaceCountReq, reply *apis.LogQueryInterfaceThirdPartyCountReply) error {
  208. result := make([]apis.LogQueryInterfaceThirdPartyCount, 0)
  209. //获取sql
  210. sql := getLogQueryInterfaceThirdPartyCountWhere(req)
  211. _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
  212. if err != nil && err != orm.ErrNoRows {
  213. l.Error("mysql",
  214. zap.String("sql", sql),
  215. zap.String("fields", utils.MarshalJsonString(req)),
  216. zap.String("error", err.Error()))
  217. return errors.DataBaseError
  218. }
  219. reply.List = result
  220. return nil
  221. }
  222. func getThirdPartyInterfaceAnalyzeWhere(req *apis.ThirdPartyInterfaceErrorAnalyzeReq) string {
  223. sql := "SELECT COUNT(*) as `count`, raw_code as code, msg FROM " + req.TabName + " WHERE"
  224. if req.GroupByState {
  225. sql = "SELECT COUNT(*) as `count`, raw_code as code, state, msg FROM " + req.TabName + " WHERE"
  226. }
  227. if req.EndTimestamp != 0 && req.StartTimestamp != 0 {
  228. sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, req.StartTimestamp, req.EndTimestamp)
  229. } else {
  230. nowTime := time.Now()
  231. zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
  232. startTimeStamp := zeroTime.Unix()
  233. endTimeStamp := nowTime.Unix()
  234. //startTimeStamp := endTimeStamp - 24*60*60
  235. sql = fmt.Sprintf("%s `timestamp`>=%d AND `timestamp`<%d", sql, startTimeStamp, endTimeStamp)
  236. }
  237. if req.ApiId != 0 {
  238. sql = fmt.Sprintf("%s AND `provider_api_id`=%d", sql, req.ApiId)
  239. }
  240. if req.GroupByState {
  241. return sql + " GROUP BY `raw_code`, `state`"
  242. }
  243. return sql + " GROUP BY `raw_code`"
  244. }
  245. func thirdPartyInterfaceErrorAnalyze(req *apis.ThirdPartyInterfaceErrorAnalyzeReq, m map[string]int) error {
  246. //获取sql
  247. result := []apis.ThirdPartyInterfaceErrorAnalyze{}
  248. sql := getThirdPartyInterfaceAnalyzeWhere(req)
  249. _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
  250. if err != nil && err != orm.ErrNoRows {
  251. l.Error("mysql",
  252. zap.String("sql", sql),
  253. zap.String("fields", utils.MarshalJsonString(req)),
  254. zap.String("error", err.Error()))
  255. return errors.DataBaseError
  256. }
  257. for _, v := range result {
  258. key := fmt.Sprintf("%v----%v----%v", v.Code, v.Msg, v.State)
  259. if _, ok := m[key]; ok == false {
  260. m[key] = v.Count
  261. } else {
  262. m[key] += v.Count
  263. }
  264. }
  265. return nil
  266. }
  267. func ThirdPartyInterfaceErrorAnalyzeNew(req *apis.ThirdPartyInterfaceErrorAnalyzeReq, reply *apis.ThirdPartyInterfaceErrorAnalyzeReply) error {
  268. if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
  269. now := time.Now()
  270. req.EndTimestamp = now.Unix()
  271. req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
  272. }
  273. m := map[string]int{}
  274. if req.TabName == "t_gd_thirdpart_log_day" {
  275. if err := thirdPartyInterfaceErrorAnalyze(req, m); err != nil {
  276. return err
  277. }
  278. } else {
  279. exportTimes := getExportTimes(req.StartTimestamp, req.EndTimestamp)
  280. for _, v := range exportTimes {
  281. req.StartTimestamp = v.start
  282. req.EndTimestamp = v.end
  283. req.TabName = getMonthTab("t_gd_thirdpart_log_month", req.StartTimestamp)
  284. err := thirdPartyInterfaceErrorAnalyze(req, m)
  285. if err != nil {
  286. return err
  287. }
  288. }
  289. }
  290. i := 0
  291. reply.List = make([]apis.ThirdPartyInterfaceErrorAnalyze, len(m))
  292. for k, v := range m {
  293. array := strings.Split(k, "----")
  294. if len(array) > 2 {
  295. reply.List[i].Code = array[0]
  296. reply.List[i].Msg = array[1]
  297. if array[2] == "false" {
  298. reply.List[i].State = false
  299. } else {
  300. reply.List[i].State = true
  301. }
  302. }
  303. reply.List[i].Count = v
  304. i++
  305. }
  306. return nil
  307. }
  308. // 数据源异常分析
  309. func ThirdPartyInterfaceErrorAnalyze(ctx context.Context, req *apis.ThirdPartyInterfaceErrorAnalyzeReq, reply *apis.ThirdPartyInterfaceErrorAnalyzeReply) error {
  310. if true {
  311. return ThirdPartyInterfaceErrorAnalyzeNew(req, reply)
  312. }
  313. if req.EndTimestamp == 0 || req.StartTimestamp == 0 {
  314. now := time.Now()
  315. req.EndTimestamp = now.Unix()
  316. req.StartTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
  317. }
  318. result := make([]apis.ThirdPartyInterfaceErrorAnalyze, 0)
  319. if req.TabName == "" {
  320. req.TabName = "t_gd_thirdpart_access_log"
  321. }
  322. //获取sql
  323. sql := getThirdPartyInterfaceAnalyzeWhere(req)
  324. _, err := orm.NewOrm().Raw(sql).QueryRows(&result)
  325. if err != nil && err != orm.ErrNoRows {
  326. l.Error("mysql",
  327. zap.String("sql", sql),
  328. zap.String("fields", utils.MarshalJsonString(req)),
  329. zap.String("error", err.Error()))
  330. return errors.DataBaseError
  331. }
  332. reply.List = result
  333. return nil
  334. }