load_chart_data_logic.go
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package chart
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type LoadChartDataLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewLoadChartDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoadChartDataLogic {
return &LoadChartDataLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *LoadChartDataLogic) LoadChartData(req *types.LoadChartDataRequest) (resp interface{}, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
chartSetting *domain.ChartSetting
)
if chartSetting, err = l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, req.ChartId); err != nil {
return nil, xerr.NewErrMsgErr("加载图表数据失败", err)
}
resp, err = l.LoadData(chartSetting)
if err != nil {
return nil, xerr.NewErrMsgErr("加载图表数据失败", err)
}
return
}
type SeriesData struct {
Name string `json:"name"`
Data interface{} `json:"data"`
}
func (l *LoadChartDataLogic) LoadData(chartSetting *domain.ChartSetting) (data interface{}, err error) {
var seriesData interface{}
switch chartSetting.ChartType {
case domain.MetricsCard1:
seriesData, err = l.LoadMetricsCardData(chartSetting)
case domain.ContainerCard1:
seriesData, err = l.LoadTableData(chartSetting, 5)
default:
seriesData, err = l.LoadTableData(chartSetting, 0)
}
data = map[string]interface{}{
"chartId": chartSetting.ChartId,
//"title": chartSetting.Title,
//"table": chartSetting.TableAbility,
"series": seriesData,
}
return
}
func (l *LoadChartDataLogic) LoadMetricsCardData(chartSetting *domain.ChartSetting) (data interface{}, err error) {
var (
list = make([]SeriesData, 0)
response bytelib.TablePreviewResponse
)
for _, series := range chartSetting.Series {
seriesData := SeriesData{
Name: series.Name,
}
if series.SourceFrom == domain.SourceFromUser {
seriesData.Data = series.CustomText
list = append(list, seriesData)
continue
}
response, err = l.svcCtx.ByteMetadataService.FastTableData(l.ctx, series.DataSourceId, 1)
if err != nil {
return
}
cellData := response.FirstCellData()
if series.Name == "指标1" && len(series.MatchExpressions) > 0 {
for _, exp := range series.MatchExpressions {
if ok, value := exp.Match(cellData); ok {
cellData = value
}
}
}
seriesData.Data = cellData
list = append(list, seriesData)
continue
}
data = list
return
}
func (l *LoadChartDataLogic) LoadTableData(chartSetting *domain.ChartSetting, limit int) (data interface{}, err error) {
var (
list = make([]SeriesData, 0)
response bytelib.TablePreviewResponse
)
for _, series := range chartSetting.Series {
seriesData := SeriesData{
Name: series.Name,
}
response, err = l.svcCtx.ByteMetadataService.FastTableData(l.ctx, series.DataSourceId, limit)
if err != nil {
return
}
seriesData.Data = map[string]interface{}{
"list": response.Grid.List,
"fields": response.Fields,
"total": response.Grid.Total,
}
list = append(list, seriesData)
}
data = list
return
}