Browse Source

优化pg获取数据逻辑,添加时间聚合

路佳明 1 month ago
parent
commit
7b8382ea70
2 changed files with 7 additions and 8 deletions
  1. 1 1
      app/observe/service/ot_app_score.go
  2. 6 7
      app/observe/service/service.go

+ 1 - 1
app/observe/service/ot_app_score.go

@@ -68,7 +68,7 @@ func (e *AppScore) GetServicesDetails(ctx context.Context, list *[]dto.ServiceAp
 	err := e.PgOrm.Table(tbl).
 		Select("service_name, sum(request_count) as total, sum(span_status_error_count) as error_num, avg(apdex) as apdex").
 		Where("timestamp>=? and timestamp<?", start, end).
-		Where("app_name", params.AppAlias).Group("service_name").
+		Where("app_name", params.AppAlias).Group("service_name, timestamp").
 		Find(list).Error
 	if err != nil {
 		return err

+ 6 - 7
app/observe/service/service.go

@@ -758,9 +758,9 @@ func (s *Service) CompareServiceLiveness(ctx context.Context, params *dto.SpanSc
 	// err = s.ChOrm.Raw(sql, params.AppAlias, params.ServiceName, startTime.Unix(), endTime.Unix()).Scan(&list).Error
 
 	tbl, star, end := cUtils.MetricTableName("service_metrics", startTime, endTime)
-	err = s.PgOrm.Table(tbl).Select("timestamp as start, request_count as total").
+	err = s.PgOrm.Table(tbl).Select("timestamp as start, sum(request_count) as total").
 		Where("app_name=? and service_name=?", params.AppAlias, params.ServiceName).
-		Where("timestamp>=? and timestamp<?", star, end).Order("start").Scan(&list).Error
+		Where("timestamp>=? and timestamp<?", star, end).Group("timestamp").Order("start").Scan(&list).Error
 
 	// 使用视图效率低,会扫描全表
 	// err = s.ChOrm.Model(&models.TracesAggbysvcMerge{}).Debug().
@@ -831,7 +831,8 @@ func (s *Service) CompareServiceErrors(ctx context.Context, params *dto.SpanScat
 	tbl, start, end := cUtils.MetricTableName("service_metrics", startTime, endTime)
 	err = s.PgOrm.Table(tbl).Where("app_name=? and service_name=?", params.AppAlias, params.ServiceName).
 		Where("timestamp>=? and timestamp<?", start, end).
-		Select("timestamp as start_time, span_status_error_count as total").
+		Select("timestamp as start_time, sum(span_status_error_count) as total").
+		Group("timestamp").
 		Order("timestamp ASC").Scan(&list).Error
 	if err != nil {
 		return
@@ -1476,7 +1477,7 @@ func (s *Service) List(req *dto.ServiceListReq, resp *[]dto.ServiceListResp, tot
 		}
 	}
 	pageScopes := cDto.Paginate(req.GetPageSize(), req.GetPageIndex())
-	start, end := time.Unix(req.StartTime, 0).Local(), time.Unix(req.EndTime, 0).Local()
+	// start, end := time.Unix(req.StartTime, 0).Local(), time.Unix(req.EndTime, 0).Local()
 	fieldMap := map[string]string{
 		"total":     "sum(request_count)",
 		"error_num": "sum(span_status_error_count)",
@@ -1494,9 +1495,7 @@ func (s *Service) List(req *dto.ServiceListReq, resp *[]dto.ServiceListResp, tot
 	}
 	req.SortInfo.Field = fieldMap[req.SortInfo.Field] // 之所以这么写,是因为order by 后跟 total desc不知道为什么报错,提示total不存在,但在直接执行sql是可以的
 	orderby := req.SortInfo.OrderBy(fieldVals, fieldMap["total"], "desc")
-	fmt.Println(start, end)
 	tbl, start, end := cUtils.MetricTableName("service_metrics", req.StartTime, req.EndTime)
-	fmt.Println(start, end)
 	db := s.PgOrm.Debug().Table(tbl).
 		Where("app_name=?", appAlias).
 		Where("timestamp>=? and timestamp<?", start, end)
@@ -1506,7 +1505,7 @@ func (s *Service) List(req *dto.ServiceListReq, resp *[]dto.ServiceListResp, tot
 	err := db.Scopes(pageScopes).
 		Select(
 			"service_name, " + strings.Join(fields, ", "),
-		).Group("service_name").Order(orderby).Scan(resp).Limit(-1).Offset(-1).Count(total).Error
+		).Group("service_name, timestamp").Order(orderby).Scan(resp).Limit(-1).Offset(-1).Count(total).Error
 	if err != nil {
 		return errors.Wrap(err, "获取服务统计数据失败")
 	}