Ver código fonte

接口列表及数字视图优化

路佳明 3 semanas atrás
pai
commit
01f9e761e6

+ 6 - 0
app/observe/service/dto/urlmapping.go

@@ -352,6 +352,12 @@ type UrlMappingSubListResp struct {
 	AppAlias      string        `json:"app_alias"`
 	Total         int64         `json:"total"`
 	ErrorRate     float64       `json:"error_rate"`
+	ErrorNum      int64         `json:"error_num"`
+	DurationMax   float64       `json:"duration_max"`
+	DurationAvg   float64       `json:"duration_avg"`
+	DurationP50   float64       `json:"duration_p50"`
+	DurationP90   float64       `json:"duration_p90"`
+	DurationP99   float64       `json:"duration_p99"`
 	DurationStats DurationStats `json:"duration_stats"`
 }
 

+ 60 - 48
app/observe/service/urlmapping.go

@@ -1141,7 +1141,7 @@ func (s *UrlMapping) SubDigits(req *dto.UrlMappingSubDigitsReq, resp *dto.UrlMap
 		Where("Timestamp>=? and Timestamp<?", req.StartTime, req.EndTime)
 	if req.ID > 0 {
 		db.Where("AppAlias=? and ServiceName=?", um.AppAlias, um.ServiceName)
-		db.Where("(Route=? OR Path=?) and Method=?", um.Url, um.Url, um.Method)
+		db.Where("Route=? and Method=?", um.Url, um.Method)
 	} else if req.AppAlias != "" {
 		db.Where("AppAlias", req.AppAlias)
 	}
@@ -1198,7 +1198,7 @@ func (s *UrlMapping) SubList(req *dto.UrlMappingSubListReq, resp *[]dto.UrlMappi
 		Where("app_alias=?", req.AppAlias).
 		// Where("url like ? AND is_perfect_match=?", um.Url+"%", 1)
 		Where("is_perfect_match=?", 1).
-		Where("url!=?", "")
+		Where("route!=?", "")
 	var serviceNames []string
 	if req.ServiceName != "" {
 		serviceNames = strings.Split(req.ServiceName, ",")
@@ -1221,7 +1221,7 @@ func (s *UrlMapping) SubList(req *dto.UrlMappingSubListReq, resp *[]dto.UrlMappi
 		db.Where("id in ?", urlMappingIds)
 	}
 	if req.Url != "" {
-		db.Where("url like ?", "%"+req.Url+"%")
+		db.Where("route like ?", "%"+req.Url+"%")
 	}
 	if err := db.
 		Scopes(cDto.Paginate(req.GetPageSize(), req.GetPageIndex())).
@@ -1268,16 +1268,16 @@ func (s *UrlMapping) subStatsFromClickhouse(wg *sync.WaitGroup, req *dto.UrlMapp
 		Where("Timestamp>toDateTime(?) and Timestamp<toDateTime(?)", req.StartTime, req.EndTime)
 	fields := []string{
 		"count() as Total",
-		"max(Duration) as MaxDuration",
-		"avg(Duration) as AvgDuration",
+		"max(Duration)/1e6 as MaxDuration",
+		"avg(Duration)/1e6 as AvgDuration",
 		"sum(if(StatusCode>=400, 1, 0)) as ErrorNum",
-		"quantile(0.5)(Duration) as P50Duration",
-		"quantile(0.90)(Duration) as P90Duration",
-		"quantile(0.99)(Duration) as P99Duration",
+		"quantile(0.5)(Duration)/1e6 as P50Duration",
+		"quantile(0.90)(Duration)/1e6 as P90Duration",
+		"quantile(0.99)(Duration)/1e6 as P99Duration",
 	}
 	row := struct {
 		Total       int64
-		MaxDuration int64
+		MaxDuration float64
 		ErrorNum    int64
 		AvgDuration float64
 		P50Duration float64
@@ -1288,47 +1288,59 @@ func (s *UrlMapping) subStatsFromClickhouse(wg *sync.WaitGroup, req *dto.UrlMapp
 	if err != nil {
 		return
 	}
-	if row.Total == 0 {
-		item.DurationStats = dto.DurationStats{
-			Time: []string{},
-			P50:  []float64{},
-			P90:  []float64{},
-			P99:  []float64{},
-		}
-		return
-	}
-	timeField := "formatDateTime(toStartOfMinute(Timestamp), '%F %H:%i', 'PRC') as StartTime"
-	if req.EndTime-req.StartTime >= 60*60 {
-		timeField = "formatDateTime(toStartOfFiveMinutes(Timestamp), '%F %H:%i', 'PRC') as StartTime"
-	}
-	fields = []string{
-		timeField,
-		"quantile(0.5)(Duration) as P50Duration",
-		"quantile(0.90)(Duration) as P90Duration",
-		"quantile(0.99)(Duration) as P99Duration",
-	}
-	quantiles := []struct {
-		StartTime   string
-		P50Duration float64
-		P90Duration float64
-		P99Duration float64
-	}{}
-	if err := db.Select(fields).Group(timeField).Order(fmt.Sprintf("%s asc", timeField)).Find(&quantiles).Error; err != nil {
-		return
-	}
 	item.Total = row.Total
-	item.DurationStats = dto.DurationStats{
-		Time: make([]string, len(quantiles)),
-		P50:  make([]float64, len(quantiles)),
-		P90:  make([]float64, len(quantiles)),
-		P99:  make([]float64, len(quantiles)),
-	}
-	for i, quantile := range quantiles {
-		item.DurationStats.Time[i] = quantile.StartTime
-		item.DurationStats.P50[i] = math.Round(quantile.P50Duration/1e6*100) / 100
-		item.DurationStats.P90[i] = math.Round(quantile.P90Duration/1e6*100) / 100
-		item.DurationStats.P99[i] = math.Round(quantile.P99Duration/1e6*100) / 100
+	if item.Total > 0 {
+		item.ErrorRate = float64(row.ErrorNum) / float64(row.Total)
 	}
+	item.ErrorNum = row.ErrorNum
+	item.DurationMax = row.MaxDuration
+	item.DurationAvg = row.AvgDuration
+	item.DurationP50 = row.P50Duration
+	item.DurationP90 = row.P90Duration
+	item.DurationP99 = row.P99Duration
+
+	// 由于效率原因,以下指标暂时去掉
+	// if row.Total == 0 {
+	// 	item.DurationStats = dto.DurationStats{
+	// 		Time: []string{},
+	// 		P50:  []float64{},
+	// 		P90:  []float64{},
+	// 		P99:  []float64{},
+	// 	}
+	// 	return
+	// }
+	// timeField := "formatDateTime(toStartOfMinute(Timestamp), '%F %H:%i', 'PRC') as StartTime"
+	// if req.EndTime-req.StartTime >= 60*60 {
+	// 	timeField = "formatDateTime(toStartOfFiveMinutes(Timestamp), '%F %H:%i', 'PRC') as StartTime"
+	// }
+	// fields = []string{
+	// 	timeField,
+	// 	"quantile(0.5)(Duration) as P50Duration",
+	// 	"quantile(0.90)(Duration) as P90Duration",
+	// 	"quantile(0.99)(Duration) as P99Duration",
+	// }
+	// quantiles := []struct {
+	// 	StartTime   string
+	// 	P50Duration float64
+	// 	P90Duration float64
+	// 	P99Duration float64
+	// }{}
+	// if err := db.Select(fields).Group(timeField).Order(fmt.Sprintf("%s asc", timeField)).Find(&quantiles).Error; err != nil {
+	// 	return
+	// }
+	// item.Total = row.Total
+	// item.DurationStats = dto.DurationStats{
+	// 	Time: make([]string, len(quantiles)),
+	// 	P50:  make([]float64, len(quantiles)),
+	// 	P90:  make([]float64, len(quantiles)),
+	// 	P99:  make([]float64, len(quantiles)),
+	// }
+	// for i, quantile := range quantiles {
+	// 	item.DurationStats.Time[i] = quantile.StartTime
+	// 	item.DurationStats.P50[i] = math.Round(quantile.P50Duration/1e6*100) / 100
+	// 	item.DurationStats.P90[i] = math.Round(quantile.P90Duration/1e6*100) / 100
+	// 	item.DurationStats.P99[i] = math.Round(quantile.P99Duration/1e6*100) / 100
+	// }
 }
 
 func (s *UrlMapping) SubStats(wg *sync.WaitGroup, um *models.UrlMapping, req *dto.UrlMappingSubListReq, resp *dto.UrlMappingSubListResp) error {