123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package service
- import (
- "fmt"
- aModel "go-admin/app/admin/models"
- "go-admin/app/observe/models"
- "go-admin/app/observe/service/dto"
- cDto "go-admin/common/dto"
- "go-admin/utils"
- "time"
- "github.com/pkg/errors"
- "gorm.io/gorm"
- )
- type Event struct {
- utils.OtService
- }
- func (e *Event) GetPage(c *dto.EventListReq, result *[]dto.EventListResp, count *int64) error {
- db := e.ChOrm.Table(models.TableNameEvent)
- if c.AppId > 0 {
- db.Where("AppId", c.AppId)
- }
- if c.AlertStatus == 2 || c.AlertStatus == 4 {
- db.Where("AlertStatus", c.AlertStatus)
- }
- // db.Scopes(cDto.MakeCondition(*c))
- if c.StartTime > 0 {
- db.Where("AppendTime>=?", c.StartTime)
- }
- if c.EndTime > 0 {
- db.Where("AppendTime<?", c.EndTime)
- }
- if c.ExceptionName != "" {
- db.Where("ExceptionName LIKE ?", fmt.Sprintf("%%%s%%", c.ExceptionName))
- }
- err := db.Debug().Scopes(cDto.Paginate(c.GetPageSize(), c.GetPageIndex())).Order("AppendTime DESC").Find(result).
- Limit(-1).Offset(-1).Count(count).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil
- }
- return errors.Wrap(err, "查询事件信息列表失败")
- }
- return nil
- }
- func (e *Event) ExceptionNums(c *dto.EventExceptionNumsReq, result *[]dto.EventExceptionNumsResp, count *int64) error {
- db := e.Orm.Model(&aModel.OtFireEvents{}).Scopes(
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- ).Select("app_id", "app_alias", "app_name", "COUNT(1) AS exception_num").
- Where("is_know = 0 AND is_resolve = 0 AND is_ignore = 0")
- if c.StartTime > 0 {
- db.Where("created_at>=?", time.Unix(c.StartTime, 0).Format(time.DateTime))
- }
- if c.EndTime > 0 {
- db.Where("created_at<?", time.Unix(c.EndTime, 0).Format(time.DateTime))
- }
- appAliases := c.GetAppAliases()
- if len(appAliases) > 0 {
- db.Where("app_alias in ?", appAliases)
- } else { // 兼容旧逻辑, 后期去掉
- appIds := c.GetAppIds()
- if len(appIds) > 0 {
- db.Where("app_id IN ?", appIds)
- }
- }
- if err := db.Debug().Group("app_id, app_alias, app_name").Find(result).Limit(-1).Offset(-1).Count(count).Error; err != nil {
- return errors.Wrap(err, "执行查询应用异常数量失败")
- }
- return nil
- }
- func (e *Event) ExceptionNumByID(c *dto.EventExecptionNumGetByIDReq, count *int64) error {
- tbl := aModel.OtFireEvents{}.TableName()
- db := e.Orm.Table(fmt.Sprintf("%s t", tbl))
- db.Select([]string{
- "app_id",
- "COUNT(*) AS exception_num",
- })
- db.Where("app_id = ?", c.Id)
- db.Where("is_know = 0 AND is_resolve = 0 AND is_ignore = 0")
- if err := db.Count(count).Error; err != nil {
- return errors.Wrap(err, "执行查询业务异常数量失败")
- }
- return nil
- }
- func (e *Event) EventStatistic(c *dto.EventStatisticReq, result *[]dto.EventStatisticResp) error {
- db := e.ChOrm.Table(models.TableNameTracesError)
- db.Select([]string{
- "toTimeZone(toStartOfFiveMinutes(Timestamp), 'Asia/Shanghai') AS StartTime",
- "COUNT() AS Total",
- })
- db.Where("AppAlias = ?", c.AppAlias)
- db.Where("Timestamp >= ? AND Timestamp <= ?", c.StartTime, c.EndTime)
- db.Where("StatusCode='STATUS_CODE_ERROR' and StatusMessage!='' and StatusMessage != 'status code:0'")
- if c.Kind != "" && c.SubconditionValue != "" {
- switch c.Kind {
- case "app":
- db.Where("((ParentSpanId='' and SpanKind='SPAN_KIND_SERVER' and SpanAttributes['http.method'] != '' and SpanAttributes['rpc.system']='') OR SpanAttributes['db.system'] != '')") // 要求 只要http请求 和数据库 两种异常
- case "biz":
- // 改为错误分布, 与错误率*业务请求总数对上
- start := time.Unix(c.StartTime, 0).Truncate(time.Hour).Format(time.DateTime)
- end := time.Unix(c.EndTime, 0).Truncate(time.Hour).Add(time.Hour).Format(time.DateTime)
- return e.Orm.Model(&models.BizStatsHourly{}).
- Select("start_time, sum(error_num) as total").
- Where("biz_hash=?", c.BizHash).
- Where("start_time>=? and end_time<?", start, end).
- Group("start_time").Order("start_time").Find(result).Error
- case "interface":
- case "service":
- db.Where("ServiceName = ", c.SubconditionValue)
- default:
- }
- }
- // db.Where("StatusCode = 'STATUS_CODE_ERROR' AND StatusMessage != '' AND StatusMessage != 'status code:0'")
- db.Group("StartTime")
- if err := db.Order("StartTime Desc").Find(&result).Error; err != nil {
- return errors.Wrap(err, "计算范围时间内的异常失败")
- }
- return nil
- }
- func (e *Event) EventLists(c *dto.EventStatisticReq, result *[]dto.EventListsResp) error {
- res := make([]dto.EventListsResp, 0)
- db := e.ChOrm.Table(models.TableNameTracesError)
- db.Select(
- "ServiceName AS ServiceName",
- "any(if(SpanAttributes['db.system'] != '', '数据库异常', '业务异常')) AS ErrorType",
- "any(StatusMessage) as StatusMessage1",
- "COUNT() AS StatusMessageCount",
- )
- if c.AppAlias != "" {
- db.Where("AppAlias = ?", c.AppAlias)
- }
- if c.Kind != "" && c.SubconditionValue != "" {
- switch c.Kind {
- case "app":
- case "biz":
- case "interface":
- db.Where("")
- case "service":
- db.Where("ServiceName = ", c.SubconditionValue)
- default:
- }
- }
- db.Where("Timestamp >= ? AND Timestamp <= ?", c.StartTime, c.EndTime)
- // db.Where("StatusCode = 'STATUS_CODE_ERROR' AND StatusMessage != '' AND StatusMessage != 'status code:0'")
- db.Where("StatusCode='STATUS_CODE_ERROR' AND StatusMessage!='' and StatusMessage != 'status code:0'").
- Where("((ParentSpanId='' and SpanKind='SPAN_KIND_SERVER' and SpanAttributes['http.method'] != '' and SpanAttributes['rpc.system']='') OR SpanAttributes['db.system'] != '')") // 要求 只要root span和数据库 两种异常
- db.Group("substring(StatusMessage, 1, 15)")
- db.Group("ServiceName")
- if err := db.Order("StatusMessageCount DESC;").Find(&res).Error; err != nil {
- return errors.Wrap(err, "统计范围时间内的具体异常失败")
- }
- svcNodes := []struct {
- ServiceName string
- Name string
- Id int64
- }{}
- if err := e.Orm.Model(&models.ServiceNode{}).Where("app_alias", c.AppAlias).Find(&svcNodes).Error; err != nil {
- return errors.Wrap(err, "获取服务列表失败")
- }
- svcMap := map[string]string{}
- svcId := map[string]int64{}
- for _, node := range svcNodes {
- svcMap[node.ServiceName] = node.Name
- svcId[node.ServiceName] = node.Id
- }
- for _, r := range res {
- r.ServiceNameCN = svcMap[r.ServiceName]
- r.ServiceId = svcId[r.ServiceName]
- *result = append(*result, r)
- }
- return nil
- }
|