From 94d881cc18787f27930a06b03cab7107d9d1fca8 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Wed, 30 Apr 2025 15:05:47 +0800 Subject: [PATCH] Optimize chart data --- module/monitor/driver/driver.go | 1 + module/monitor/driver/influxdb-v2/executor.go | 28 ++++++++++++++ module/monitor/dto/output.go | 38 +++++++++++++------ module/monitor/iml.go | 38 +++++++++++-------- 4 files changed, 78 insertions(+), 27 deletions(-) diff --git a/module/monitor/driver/driver.go b/module/monitor/driver/driver.go index 8e5fc1cf..d9d93701 100644 --- a/module/monitor/driver/driver.go +++ b/module/monitor/driver/driver.go @@ -37,6 +37,7 @@ type IBasicOverview interface { type IRestOverview interface { TrafficOverviewByStatusCode(ctx context.Context, start time.Time, end time.Time, wheres []monitor.MonWhereItem) ([]time.Time, *monitor.StatusCodeOverview, []*monitor.StatusCodeOverview, error) AvgResponseTimeOverview(ctx context.Context, start time.Time, end time.Time, wheres []monitor.MonWhereItem) ([]time.Time, *monitor.Aggregate, []int64, error) + SumResponseTimeOverview(ctx context.Context, start time.Time, end time.Time, wheres []monitor.MonWhereItem) ([]time.Time, *monitor.Aggregate, []int64, error) } type IAIOverview interface { diff --git a/module/monitor/driver/influxdb-v2/executor.go b/module/monitor/driver/influxdb-v2/executor.go index e52b3e3b..b558c7fc 100644 --- a/module/monitor/driver/influxdb-v2/executor.go +++ b/module/monitor/driver/influxdb-v2/executor.go @@ -513,6 +513,34 @@ func (e *executor) aggregateSummary(ctx context.Context, start time.Time, end ti } +func (e *executor) SumResponseTimeOverview(ctx context.Context, start time.Time, end time.Time, wheres []monitor.MonWhereItem) ([]time.Time, *monitor.Aggregate, []int64, error) { + newStartTime, every, windowOffset, bucket := getTimeIntervalAndBucket(start, end) + filters := formatFilter(wheres) + + fieldsConditions := []string{"timing"} + + agg, err := e.aggregateSummary(ctx, newStartTime, end, "request", bucket, filters, []string{"timing"}) + if err != nil { + return nil, nil, nil, err + } + + dates, groupValues, err := e.fluxQuery.CommonTendency(ctx, e.openApi, newStartTime, end, bucket, "request", filters, fieldsConditions, every, windowOffset, flux.SumFn) + if err != nil { + return nil, nil, nil, err + } + + timing := groupValues["timing"] + timingLen := len(timing) + result := make([]int64, 0, len(dates)) + for i := range dates { + if timingLen > i { + result = append(result, timing[i]) + } + } + + return dates, agg["timing"], result, nil +} + func (e *executor) AvgResponseTimeOverview(ctx context.Context, start time.Time, end time.Time, wheres []monitor.MonWhereItem) ([]time.Time, *monitor.Aggregate, []int64, error) { newStartTime, every, windowOffset, bucket := getTimeIntervalAndBucket(start, end) filters := formatFilter(wheres) diff --git a/module/monitor/dto/output.go b/module/monitor/dto/output.go index c394b585..bb9c8e40 100644 --- a/module/monitor/dto/output.go +++ b/module/monitor/dto/output.go @@ -159,8 +159,13 @@ type ChartAIOverview struct { AvgRequestPerSubscriber string `json:"avg_request_per_subscriber"` //请求概况 AvgRequestPerSubscriberOverview []int64 `json:"avg_request_per_subscriber_overview"` //平均响应时间概况 RequestTotal string `json:"request_total"` - TokenTotal string `json:"token_total"` //总token流量 - TokenOverview []*TokenOverview `json:"token_overview"` //token概况 + Request2xxTotal string `json:"request_2xx_total"` + Request4xxTotal string `json:"request_4xx_total"` + Request5xxTotal string `json:"request_5xx_total"` + TokenTotal string `json:"token_total"` //总token流量 + InputTokenTotal string `json:"input_token_total"` + OutputTokenTotal string `json:"output_token_total"` //最大token流量 + TokenOverview []*TokenOverview `json:"token_overview"` //token概况 AvgTokenOverview []int64 `json:"avg_token_overview"` AvgTokenPerSubscriberOverview []*TokenOverview `json:"avg_token_per_subscriber_overview"` AvgToken string `json:"avg_token"` @@ -174,16 +179,25 @@ type ChartRestOverview struct { RequestOverview []*StatusCodeOverview `json:"request_overview"` //请求概况 AvgRequestPerSubscriber string `json:"avg_request_per_subscriber"` AvgRequestPerSubscriberOverview []int64 `json:"avg_request_per_subscriber_overview"` //平均响应时间概况 - RequestTotal string `json:"request_total"` - TrafficOverview []*StatusCodeOverview `json:"traffic_overview"` //流量概况 - AvgResponseTimeOverview []int64 `json:"avg_response_time_overview"` //平均响应时间概况 - AvgTrafficPerSubscriberOverview []int64 `json:"avg_traffic_per_subscriber_overview"` - TrafficTotal string `json:"traffic_total"` - AvgResponseTime string `json:"avg_response_time"` //平均响应时间 - MaxResponseTime string `json:"max_response_time"` //最大响应时间 - MinResponseTime string `json:"min_response_time"` //最小响应时间 - AvgTrafficPerSubscriber string `json:"avg_traffic_per_subscriber"` - Date []string `json:"date"` + + RequestTotal string `json:"request_total"` + Request2xxTotal string `json:"request_2xx_total"` + Request4xxTotal string `json:"request_4xx_total"` + Request5xxTotal string `json:"request_5xx_total"` + + TrafficOverview []*StatusCodeOverview `json:"traffic_overview"` //流量概况 + Traffic2xxTotal string `json:"traffic_2xx_total"` + Traffic4xxTotal string `json:"traffic_4xx_total"` //流量概况 + Traffic5xxTotal string `json:"traffic_5xx_total"` //流量概况 + + AvgResponseTimeOverview []int64 `json:"avg_response_time_overview"` //平均响应时间概况 + AvgTrafficPerSubscriberOverview []int64 `json:"avg_traffic_per_subscriber_overview"` + TrafficTotal string `json:"traffic_total"` + AvgResponseTime string `json:"avg_response_time"` //平均响应时间 + MaxResponseTime string `json:"max_response_time"` //最大响应时间 + MinResponseTime string `json:"min_response_time"` //最小响应时间 + AvgTrafficPerSubscriber string `json:"avg_traffic_per_subscriber"` + Date []string `json:"date"` } type ServiceChartRestOverview struct { diff --git a/module/monitor/iml.go b/module/monitor/iml.go index f5e50b07..2ca4587e 100644 --- a/module/monitor/iml.go +++ b/module/monitor/iml.go @@ -135,17 +135,20 @@ func (i *imlMonitorStatisticModule) AIChartOverview(ctx context.Context, service } result.AvgRequestPerSubscriber = common.FormatCount(summary.StatusTotal / subscriberNum) result.RequestTotal = common.FormatCount(summary.StatusTotal) + result.Request2xxTotal = common.FormatCount(summary.Status2xx) + result.Request4xxTotal = common.FormatCount(summary.Status4xx) + result.Request5xxTotal = common.FormatCount(summary.Status5xx) }() - avgResponseTimes := make([]int64, 0) + sumResponseTimes := make([]int64, 0) go func() { defer wg.Done() - _, _, items, err := executor.AvgResponseTimeOverview(ctx, formatTimeByMinute(start), formatTimeByMinute(end), wheres) + _, _, items, err := executor.SumResponseTimeOverview(ctx, formatTimeByMinute(start), formatTimeByMinute(end), wheres) if err != nil { errChan <- err return } for _, item := range items { - avgResponseTimes = append(avgResponseTimes, item) + sumResponseTimes = append(sumResponseTimes, item) } }() totalTokens := make([]int64, 0) @@ -183,10 +186,9 @@ func (i *imlMonitorStatisticModule) AIChartOverview(ctx context.Context, service } result.AvgTokenPerSubscriber = common.FormatCount(summary.TotalToken / subscriberNum) - //result.MaxToken = fmt.Sprintf("%s/s", common.FormatCount(maxToken/timeInterval)) - //result.MinToken = fmt.Sprintf("%s/s", common.FormatCount(minToken/timeInterval)) - //result.AvgToken = fmt.Sprintf("%s/s", common.FormatCount(summary.OutputToken/timeInterval)) result.TokenTotal = common.FormatCount(summary.TotalToken) + result.InputTokenTotal = common.FormatCount(summary.InputToken) + result.OutputTokenTotal = common.FormatCount(summary.OutputToken) }() go func() { wg.Wait() @@ -204,9 +206,9 @@ func (i *imlMonitorStatisticModule) AIChartOverview(ctx context.Context, service var maxTokenPerSecond, minTokenPerSecond, avgTokenPerSecond int64 = 0, 0, 0 for index, token := range totalTokens { var p int64 = 0 - if len(avgResponseTimes) > index && avgResponseTimes[index] > 0 { + if len(sumResponseTimes) > index && sumResponseTimes[index] > 0 { // 由于时间单位是ms,因此需要✖️1000 - p = int64(float64(token) * 1000 / float64(avgResponseTimes[index])) + p = int64(float64(token) * 1000 / float64(sumResponseTimes[index])) } result.AvgTokenOverview = append(result.AvgTokenOverview, p) if maxTokenPerSecond < p { @@ -217,11 +219,11 @@ func (i *imlMonitorStatisticModule) AIChartOverview(ctx context.Context, service } avgTokenPerSecond += p } - if len(avgResponseTimes) > 0 { - result.AvgToken = fmt.Sprintf("%s/s", common.FormatCount(avgTokenPerSecond/int64(len(avgResponseTimes)))) + if len(sumResponseTimes) > 0 { + result.AvgToken = fmt.Sprintf("%s Token/s", common.FormatCount(avgTokenPerSecond/int64(len(sumResponseTimes)))) } - result.MaxToken = fmt.Sprintf("%s/s", common.FormatCount(maxTokenPerSecond)) - result.MinToken = fmt.Sprintf("%s/s", common.FormatCount(minTokenPerSecond)) + result.MaxToken = fmt.Sprintf("%s Token/s", common.FormatCount(maxTokenPerSecond)) + result.MinToken = fmt.Sprintf("%s Token/s", common.FormatCount(minTokenPerSecond)) return result, nil } @@ -286,6 +288,9 @@ func (i *imlMonitorStatisticModule) RestChartOverview(ctx context.Context, servi } result.AvgRequestPerSubscriber = common.FormatCount(summary.StatusTotal / subscriberNum) result.RequestTotal = common.FormatCount(summary.StatusTotal) + result.Request2xxTotal = common.FormatCount(summary.Status2xx) + result.Request4xxTotal = common.FormatCount(summary.Status4xx) + result.Request5xxTotal = common.FormatCount(summary.Status5xx) }() go func() { @@ -298,9 +303,9 @@ func (i *imlMonitorStatisticModule) RestChartOverview(ctx context.Context, servi return } result.AvgResponseTimeOverview = items - result.AvgResponseTime = fmt.Sprintf("%dms", summary.Avg) - result.MaxResponseTime = fmt.Sprintf("%dms", summary.Max) - result.MinResponseTime = fmt.Sprintf("%dms", summary.Min) + result.AvgResponseTime = fmt.Sprintf("%d ms", summary.Avg) + result.MaxResponseTime = fmt.Sprintf("%d ms", summary.Max) + result.MinResponseTime = fmt.Sprintf("%d ms", summary.Min) }() go func() { @@ -323,6 +328,9 @@ func (i *imlMonitorStatisticModule) RestChartOverview(ctx context.Context, servi result.AvgTrafficPerSubscriberOverview = append(result.AvgTrafficPerSubscriberOverview, item.StatusTotal/subscriberNum) } result.TrafficTotal = common.FormatByte(summary.StatusTotal) + result.Traffic2xxTotal = common.FormatByte(summary.Status2xx) + result.Traffic4xxTotal = common.FormatByte(summary.Status4xx) + result.Traffic5xxTotal = common.FormatByte(summary.Status5xx) result.AvgTrafficPerSubscriber = common.FormatCount(summary.StatusTotal / subscriberNum) }() go func() {