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 0 { db.Where("created_at>=?", time.Unix(c.StartTime, 0).Format(time.DateTime)) } if c.EndTime > 0 { db.Where("created_at 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= ? 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 }