load_chart_data_logic.go 3.5 KB
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
}