作者 yangfu

load chart data

  1 +package chart
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/result"
  5 + "net/http"
  6 +
  7 + "github.com/zeromicro/go-zero/rest/httpx"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/logic/chart"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
  11 +)
  12 +
  13 +func LoadChartDataHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  14 + return func(w http.ResponseWriter, r *http.Request) {
  15 + var req types.LoadChartDataRequest
  16 + if err := httpx.Parse(r, &req); err != nil {
  17 + httpx.ErrorCtx(r.Context(), w, err)
  18 + return
  19 + }
  20 +
  21 + l := chart.NewLoadChartDataLogic(r.Context(), svcCtx)
  22 + resp, err := l.LoadChartData(&req)
  23 + result.HttpResult(r, w, resp, err)
  24 + }
  25 +}
  1 +package chart
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/result"
  5 + "net/http"
  6 +
  7 + "github.com/zeromicro/go-zero/rest/httpx"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/logic/chart"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
  11 +)
  12 +
  13 +func SaveAsChartHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  14 + return func(w http.ResponseWriter, r *http.Request) {
  15 + var req types.ChartSaveAsRequest
  16 + if err := httpx.Parse(r, &req); err != nil {
  17 + httpx.ErrorCtx(r.Context(), w, err)
  18 + return
  19 + }
  20 +
  21 + l := chart.NewSaveAsChartLogic(r.Context(), svcCtx)
  22 + resp, err := l.SaveAsChart(&req)
  23 + result.HttpResult(r, w, resp, err)
  24 + }
  25 +}
@@ -25,6 +25,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -25,6 +25,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
25 Handler: chart.SaveChartHandler(serverCtx), 25 Handler: chart.SaveChartHandler(serverCtx),
26 }, 26 },
27 { 27 {
  28 + Method: http.MethodPost,
  29 + Path: "/chart/saveas",
  30 + Handler: chart.SaveAsChartHandler(serverCtx),
  31 + },
  32 + {
28 Method: http.MethodDelete, 33 Method: http.MethodDelete,
29 Path: "/chart/:id", 34 Path: "/chart/:id",
30 Handler: chart.DeleteChartHandler(serverCtx), 35 Handler: chart.DeleteChartHandler(serverCtx),
@@ -63,9 +68,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -63,9 +68,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
63 []rest.Route{ 68 []rest.Route{
64 { 69 {
65 Method: http.MethodPost, 70 Method: http.MethodPost,
66 - Path: "/table/search-by-module",  
67 - Handler: table.SearchTableByModuleHandler(serverCtx), 71 + Path: "/chart/load-data",
  72 + Handler: chart.LoadChartDataHandler(serverCtx),
68 }, 73 },
  74 + },
  75 + rest.WithPrefix("/v1"),
  76 + )
  77 +
  78 + server.AddRoutes(
  79 + []rest.Route{
69 { 80 {
70 Method: http.MethodPost, 81 Method: http.MethodPost,
71 Path: "/table/field-optional-values", 82 Path: "/table/field-optional-values",
@@ -82,6 +93,17 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -82,6 +93,17 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
82 Handler: table.SearchTableDataHandler(serverCtx), 93 Handler: table.SearchTableDataHandler(serverCtx),
83 }, 94 },
84 }, 95 },
  96 + rest.WithPrefix("/v1"),
  97 + )
  98 +
  99 + server.AddRoutes(
  100 + []rest.Route{
  101 + {
  102 + Method: http.MethodPost,
  103 + Path: "/table/search-by-module",
  104 + Handler: table.SearchTableByModuleHandler(serverCtx),
  105 + },
  106 + },
85 rest.WithJwt(serverCtx.Config.JwtAuth.AccessSecret), 107 rest.WithJwt(serverCtx.Config.JwtAuth.AccessSecret),
86 rest.WithPrefix("/v1"), 108 rest.WithPrefix("/v1"),
87 ) 109 )
  1 +package chart
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"
  8 +
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type LoadChartDataLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewLoadChartDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoadChartDataLogic {
  22 + return &LoadChartDataLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *LoadChartDataLogic) LoadChartData(req *types.LoadChartDataRequest) (resp interface{}, err error) {
  30 + var (
  31 + conn = l.svcCtx.DefaultDBConn()
  32 + chartSetting *domain.ChartSetting
  33 + )
  34 +
  35 + if chartSetting, err = l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, req.ChartId); err != nil {
  36 + return nil, xerr.NewErrMsgErr("加载图表数据失败", err)
  37 + }
  38 + resp, err = l.LoadData(chartSetting)
  39 + if err != nil {
  40 + return nil, xerr.NewErrMsgErr("加载图表数据失败", err)
  41 + }
  42 + return
  43 +}
  44 +
  45 +type SeriesData struct {
  46 + Name string `json:"name"`
  47 + Data interface{} `json:"data"`
  48 +}
  49 +
  50 +func (l *LoadChartDataLogic) LoadData(chartSetting *domain.ChartSetting) (data interface{}, err error) {
  51 + var seriesData interface{}
  52 + switch chartSetting.ChartType {
  53 + case domain.MetricsCard1:
  54 + seriesData, err = l.LoadMetricsCardData(chartSetting)
  55 + case domain.ContainerCard1:
  56 + seriesData, err = l.LoadTableData(chartSetting, 5)
  57 + default:
  58 + seriesData, err = l.LoadTableData(chartSetting, 0)
  59 + }
  60 + data = map[string]interface{}{
  61 + "chartId": chartSetting.ChartId,
  62 + //"title": chartSetting.Title,
  63 + //"table": chartSetting.TableAbility,
  64 + "series": seriesData,
  65 + }
  66 + return
  67 +}
  68 +
  69 +func (l *LoadChartDataLogic) LoadMetricsCardData(chartSetting *domain.ChartSetting) (data interface{}, err error) {
  70 + var (
  71 + list = make([]SeriesData, 0)
  72 + response bytelib.TablePreviewResponse
  73 + )
  74 + for _, series := range chartSetting.Series {
  75 + seriesData := SeriesData{
  76 + Name: series.Name,
  77 + }
  78 + if series.SourceFrom == domain.SourceFromUser {
  79 + seriesData.Data = series.CustomText
  80 + list = append(list, seriesData)
  81 + continue
  82 + }
  83 + response, err = l.svcCtx.ByteMetadataService.FastTableData(l.ctx, series.DataSourceId, 1)
  84 + if err != nil {
  85 + return
  86 + }
  87 + cellData := response.FirstCellData()
  88 + if series.Name == "指标1" && len(series.MatchExpressions) > 0 {
  89 + for _, exp := range series.MatchExpressions {
  90 + if ok, value := exp.Match(cellData); ok {
  91 + cellData = value
  92 + }
  93 + }
  94 + }
  95 + seriesData.Data = cellData
  96 + list = append(list, seriesData)
  97 + continue
  98 + }
  99 + data = list
  100 + return
  101 +}
  102 +
  103 +func (l *LoadChartDataLogic) LoadTableData(chartSetting *domain.ChartSetting, limit int) (data interface{}, err error) {
  104 + var (
  105 + list = make([]SeriesData, 0)
  106 + response bytelib.TablePreviewResponse
  107 + )
  108 + for _, series := range chartSetting.Series {
  109 + seriesData := SeriesData{
  110 + Name: series.Name,
  111 + }
  112 + response, err = l.svcCtx.ByteMetadataService.FastTableData(l.ctx, series.DataSourceId, limit)
  113 + if err != nil {
  114 + return
  115 + }
  116 + seriesData.Data = map[string]interface{}{
  117 + "list": response.Grid.List,
  118 + "fields": response.Fields,
  119 + "total": response.Grid.Total,
  120 + }
  121 + list = append(list, seriesData)
  122 + }
  123 + data = list
  124 + return
  125 +}
  1 +package chart
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"
  7 +
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
  10 +
  11 + "github.com/zeromicro/go-zero/core/logx"
  12 +)
  13 +
  14 +type SaveAsChartLogic struct {
  15 + logx.Logger
  16 + ctx context.Context
  17 + svcCtx *svc.ServiceContext
  18 +}
  19 +
  20 +func NewSaveAsChartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveAsChartLogic {
  21 + return &SaveAsChartLogic{
  22 + Logger: logx.WithContext(ctx),
  23 + ctx: ctx,
  24 + svcCtx: svcCtx,
  25 + }
  26 +}
  27 +
  28 +func (l *SaveAsChartLogic) SaveAsChart(req *types.ChartSaveAsRequest) (resp *types.ChartSaveResponse, err error) {
  29 + var (
  30 + conn = l.svcCtx.DefaultDBConn()
  31 + chartSetting *domain.ChartSetting
  32 + chart *domain.Chart
  33 + )
  34 + chart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.ChartId)
  35 + if err != nil {
  36 + return nil, xerr.NewErrMsgErr("未找到图表", err)
  37 + }
  38 + chartSetting, err = l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, req.ChartId)
  39 + if err != nil {
  40 + return nil, xerr.NewErrMsgErr("未找到图表", err)
  41 + }
  42 + saveChartLogic := NewSaveChartLogic(l.ctx, l.svcCtx)
  43 +
  44 + resp, err = saveChartLogic.SaveChart(&types.ChartSaveRequest{
  45 + Pid: 0,
  46 + Type: chart.Type,
  47 + ChartType: chart.ChartType,
  48 + ChartProperty: types.NewPropertyItem(chartSetting.ChartProperty(chart.Cover)),
  49 + })
  50 + return
  51 +}
@@ -30,8 +30,8 @@ func NewSaveChartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveCha @@ -30,8 +30,8 @@ func NewSaveChartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveCha
30 30
31 func (l *SaveChartLogic) SaveChart(req *types.ChartSaveRequest) (resp *types.ChartSaveResponse, err error) { 31 func (l *SaveChartLogic) SaveChart(req *types.ChartSaveRequest) (resp *types.ChartSaveResponse, err error) {
32 var ( 32 var (
33 - conn = l.svcCtx.DefaultDBConn()  
34 - parentChart *domain.Chart 33 + //conn = l.svcCtx.DefaultDBConn()
  34 + //parentChart *domain.Chart
35 chart *domain.Chart 35 chart *domain.Chart
36 chartSetting *domain.ChartSetting 36 chartSetting *domain.ChartSetting
37 tenantId = contextdata.GetTenantFromCtx(l.ctx) 37 tenantId = contextdata.GetTenantFromCtx(l.ctx)
@@ -51,16 +51,16 @@ func (l *SaveChartLogic) SaveChart(req *types.ChartSaveRequest) (resp *types.Cha @@ -51,16 +51,16 @@ func (l *SaveChartLogic) SaveChart(req *types.ChartSaveRequest) (resp *types.Cha
51 if chart.Name == "" { 51 if chart.Name == "" {
52 chart.Name = chart.RandName() 52 chart.Name = chart.RandName()
53 } 53 }
54 - if req.Pid > 0 {  
55 - if parentChart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.Pid); err != nil {  
56 - return nil, xerr.NewErrMsgErr("父级不存在", err)  
57 - }  
58 - chart.Group = parentChart.Group  
59 - } 54 + //if req.Pid > 0 {
  55 + // if parentChart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.Pid); err != nil {
  56 + // return nil, xerr.NewErrMsgErr("父级不存在", err)
  57 + // }
  58 + // chart.Group = parentChart.Group
  59 + //}
60 // current sort 60 // current sort
61 - if currentSortChart, _ := l.svcCtx.ChartRepository.FindOneByGroup(l.ctx, conn, tenantId, req.Pid); currentSortChart != nil {  
62 - chart.Sort = currentSortChart.Sort + 1  
63 - } 61 + //if currentSortChart, _ := l.svcCtx.ChartRepository.FindOneByGroup(l.ctx, conn, tenantId, req.Pid); currentSortChart != nil {
  62 + // chart.Sort = currentSortChart.Sort + 1
  63 + //}
64 if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { 64 if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
65 if chart, err = l.svcCtx.ChartRepository.Insert(ctx, conn, chart); err != nil { 65 if chart, err = l.svcCtx.ChartRepository.Insert(ctx, conn, chart); err != nil {
66 return err 66 return err
@@ -11,15 +11,6 @@ func NewChartItem(chart *domain.Chart) ChartItem { @@ -11,15 +11,6 @@ func NewChartItem(chart *domain.Chart) ChartItem {
11 return item 11 return item
12 } 12 }
13 13
14 -func NewChartItemWithProperty(chart *domain.Chart, property *ChartProperty) ChartItem {  
15 - item := ChartItem{}  
16 - copier.Copy(&item, chart)  
17 - if property != nil {  
18 - item.ChartProperty = property  
19 - }  
20 - return item  
21 -}  
22 -  
23 func NewChartItemWithSetting(chart *domain.Chart, chartSetting *domain.ChartSetting) ChartItem { 14 func NewChartItemWithSetting(chart *domain.Chart, chartSetting *domain.ChartSetting) ChartItem {
24 item := ChartItem{} 15 item := ChartItem{}
25 copier.Copy(&item, chart) 16 copier.Copy(&item, chart)
@@ -21,6 +21,15 @@ type ChartSaveResponse struct { @@ -21,6 +21,15 @@ type ChartSaveResponse struct {
21 Chart ChartItem `json:"chart"` 21 Chart ChartItem `json:"chart"`
22 } 22 }
23 23
  24 +type ChartSaveAsRequest struct {
  25 + ChartId int64 `json:"chartId"` // 图表Id
  26 + Name string `json:"name"` // 名称
  27 +}
  28 +
  29 +type ChartSaveAsResponse struct {
  30 + Chart ChartItem `json:"chart"`
  31 +}
  32 +
24 type ChartDeleteRequest struct { 33 type ChartDeleteRequest struct {
25 Id int64 `path:"id"` 34 Id int64 `path:"id"`
26 } 35 }
@@ -73,6 +82,13 @@ type ChartItem struct { @@ -73,6 +82,13 @@ type ChartItem struct {
73 ChartProperty *ChartProperty `json:"property,optional,omitempty"` //属性 82 ChartProperty *ChartProperty `json:"property,optional,omitempty"` //属性
74 } 83 }
75 84
  85 +type LoadChartDataRequest struct {
  86 + ChartId int64 `json:"chartId"`
  87 +}
  88 +
  89 +type LoadChartDataResponse struct {
  90 +}
  91 +
76 type ChartComponentSearchRequest struct { 92 type ChartComponentSearchRequest struct {
77 Name string `json:"name,optional"` 93 Name string `json:"name,optional"`
78 } 94 }
@@ -113,11 +129,11 @@ type TableAbility struct { @@ -113,11 +129,11 @@ type TableAbility struct {
113 } 129 }
114 130
115 type Series struct { 131 type Series struct {
116 - Name string `json:"name"` // 名称 (例如 指标1、指标2)  
117 - SourceFrom string `json:"from,options=[ByteBank,User]"` // 数据源类型 ByteBank:字库 User:用户自定义  
118 - DataSourceId int64 `json:"dataSourceId,optional,omitempty"` // 数据源ID(from值为ByteBank时有值)  
119 - CustomText string `json:"customText,optional,omitempty"` // 自定义数据文本(from值为User时有值)  
120 - MatchExpressions []Expression `json:"matchExpressions,omitempty"` // 条件匹配表达式(总体指标) 132 + Name string `json:"name"` // 名称 (例如 指标1、指标2)
  133 + SourceFrom string `json:"from,options=[ByteBank,User]"` // 数据源类型 ByteBank:字库 User:用户自定义
  134 + DataSourceId int64 `json:"dataSourceId,optional,omitempty"` // 数据源ID(from值为ByteBank时有值)
  135 + CustomText string `json:"customText,optional,omitempty"` // 自定义数据文本(from值为User时有值)
  136 + MatchExpressions []Expression `json:"matchExpressions,optional,omitempty"` // 条件匹配表达式(总体指标)
121 } 137 }
122 138
123 type Expression struct { 139 type Expression struct {
@@ -132,14 +148,14 @@ type Dimension struct { @@ -132,14 +148,14 @@ type Dimension struct {
132 } 148 }
133 149
134 type SearchTableByModuleRequest struct { 150 type SearchTableByModuleRequest struct {
135 - Token string `header:"x-mmm-accesstoken"` 151 + Token string `header:"x-mmm-accesstoken,optional"`
136 } 152 }
137 153
138 type SearchTableByModuleResponse struct { 154 type SearchTableByModuleResponse struct {
139 } 155 }
140 156
141 type SearchTableFieldOptionalValuesRequest struct { 157 type SearchTableFieldOptionalValuesRequest struct {
142 - Token string `header:"x-mmm-accesstoken"` 158 + Token string `header:"x-mmm-accesstoken,optional"`
143 ObjectId int `json:"objectId"` // 对象ID 159 ObjectId int `json:"objectId"` // 对象ID
144 Field string `json:"field"` // 当前选择的字段 160 Field string `json:"field"` // 当前选择的字段
145 SqlName string `json:"sqlName"` // 字段SqlName 161 SqlName string `json:"sqlName"` // 字段SqlName
@@ -160,7 +176,7 @@ type Condition struct { @@ -160,7 +176,7 @@ type Condition struct {
160 } 176 }
161 177
162 type GetTableDetailRequest struct { 178 type GetTableDetailRequest struct {
163 - Token string `header:"x-mmm-accesstoken"` 179 + Token string `header:"x-mmm-accesstoken,optional"`
164 TableId int `path:"tableId"` // 表ID 180 TableId int `path:"tableId"` // 表ID
165 } 181 }
166 182
@@ -168,7 +184,7 @@ type GetTableDetailResponse struct { @@ -168,7 +184,7 @@ type GetTableDetailResponse struct {
168 } 184 }
169 185
170 type SearchTableDataRequest struct { 186 type SearchTableDataRequest struct {
171 - Token string `header:"x-mmm-accesstoken"` 187 + Token string `header:"x-mmm-accesstoken,optional"`
172 ObjectId int `json:"objectId"` // 对象ID 188 ObjectId int `json:"objectId"` // 对象ID
173 PageNumber int `json:"page,optional"` // 分页数 189 PageNumber int `json:"page,optional"` // 分页数
174 PageSize int `json:"size,optional"` // 页码 190 PageSize int `json:"size,optional"` // 页码
@@ -9,12 +9,13 @@ import ( @@ -9,12 +9,13 @@ import (
9 ) 9 )
10 10
11 type Chart struct { 11 type Chart struct {
12 - Id int64 // ID  
13 - Pid int64 `gorm:"index:idx_chart_t_pid_sort"` // 父级ID  
14 - Type string // 类型  
15 - Sort int `gorm:"index:idx_chart_t_pid_sort"` // 排序  
16 - Name string // 名称  
17 - Group string `gorm:"index:idx_chart_group"` // 分组 12 + Id int64 // ID
  13 + Pid int64 `gorm:"index:idx_chart_t_pid_sort"` // 父级ID
  14 + Type string // 类型
  15 + Sort int `gorm:"index:idx_chart_t_pid_sort"` // 排序
  16 + Name string // 名称
  17 + Group string `gorm:"index:idx_chart_group"` // 分组
  18 + ChartType string
18 19
19 TenantId int64 `gorm:"index:idx_chart_t_pid_sort"` // 租户ID 20 TenantId int64 `gorm:"index:idx_chart_t_pid_sort"` // 租户ID
20 CreatedAt int64 `json:",omitempty"` 21 CreatedAt int64 `json:",omitempty"`
@@ -119,7 +119,7 @@ func (repository *ChartRepository) Find(ctx context.Context, conn transaction.Co @@ -119,7 +119,7 @@ func (repository *ChartRepository) Find(ctx context.Context, conn transaction.Co
119 total int64 119 total int64
120 ) 120 )
121 queryFunc := func() (interface{}, error) { 121 queryFunc := func() (interface{}, error) {
122 - tx = tx.Model(&ms).Order("pid asc").Order("sort asc") 122 + tx = tx.Model(&ms).Order("id desc") //.Order("pid asc").Order("sort asc")
123 if v, ok := queryOptions["tenantId"]; ok { 123 if v, ok := queryOptions["tenantId"]; ok {
124 tx.Where("tenant_id = ?", v) 124 tx.Where("tenant_id = ?", v)
125 } 125 }
1 package domain 1 package domain
2 2
  3 +import "strconv"
  4 +
  5 +const (
  6 + SourceFromByteBank = "ByteBank"
  7 + SourceFromUser = "User"
  8 +)
  9 +
3 var ( 10 var (
4 RecordTable1 = "RecordTable" 11 RecordTable1 = "RecordTable"
5 MetricsCard1 = "MetricsCard" 12 MetricsCard1 = "MetricsCard"
@@ -47,6 +54,57 @@ type Expression struct { @@ -47,6 +54,57 @@ type Expression struct {
47 ToValue string `json:"toValue"` // 显示值(转为) 54 ToValue string `json:"toValue"` // 显示值(转为)
48 } 55 }
49 56
  57 +func (exp Expression) Match(value string) (result bool, toValue string) {
  58 + fValue, ferr := strconv.ParseFloat(value, 64)
  59 + cValue, cerr := strconv.ParseFloat(exp.CompareValue, 64)
  60 + if ferr == nil && cerr == nil {
  61 + if exp.FloatMatch(exp.Operator, fValue, cValue) {
  62 + return true, exp.ToValue
  63 + }
  64 + return false, value
  65 + }
  66 + if exp.StringMatch(exp.Operator, value, exp.CompareValue) {
  67 + return true, exp.ToValue
  68 + }
  69 + return false, value
  70 +}
  71 +
  72 +func (exp Expression) FloatMatch(op string, v, c float64) bool {
  73 + switch op {
  74 + case "<":
  75 + return v < c
  76 + case ">":
  77 + return v > c
  78 + case "==":
  79 + return v == c
  80 + case "<>":
  81 + return v != c
  82 + case "<=":
  83 + return v <= c
  84 + case ">=":
  85 + return v >= c
  86 + }
  87 + return false
  88 +}
  89 +
  90 +func (exp Expression) StringMatch(op string, v, c string) bool {
  91 + switch op {
  92 + case "<":
  93 + return v < c
  94 + case ">":
  95 + return v > c
  96 + case "==":
  97 + return v == c
  98 + case "<>":
  99 + return v != c
  100 + case "<=":
  101 + return v <= c
  102 + case ">=":
  103 + return v >= c
  104 + }
  105 + return false
  106 +}
  107 +
50 type Dimension struct { 108 type Dimension struct {
51 Name string `json:"name"` 109 Name string `json:"name"`
52 Value string `json:"value"` 110 Value string `json:"value"`
  1 +package bytelib
  2 +
  3 +type TableFrames struct {
  4 +}
  5 +
  6 +func NewTableFrames(tableData TableData) {
  7 + var mapData = make([]map[string]interface{}, 0)
  8 + for i := range tableData.Grid.List {
  9 + mapItem := make(map[string]interface{})
  10 + for k, v := range tableData.Grid.List[i] {
  11 + mapItem[k] = v
  12 + }
  13 + mapData = append(mapData, mapItem)
  14 + }
  15 + //df := dataframe.LoadMaps(mapData)
  16 + //if df!=nil{
  17 + //
  18 + //}
  19 +}
@@ -13,6 +13,17 @@ func (gateway *ByteMetadataService) TableDataPreview(ctx context.Context, reques @@ -13,6 +13,17 @@ func (gateway *ByteMetadataService) TableDataPreview(ctx context.Context, reques
13 return result, nil 13 return result, nil
14 } 14 }
15 15
  16 +func (gateway *ByteMetadataService) FastTableData(ctx context.Context, objectId int64, limit int) (TablePreviewResponse, error) {
  17 + return gateway.TableDataPreview(ctx, &TableDataPreviewRequest{
  18 + ObjectType: ObjectMetaTable,
  19 + ObjectId: objectId,
  20 + Where: &TableQueryWhere{
  21 + PageNumber: 0,
  22 + PageSize: limit,
  23 + },
  24 + })
  25 +}
  26 +
16 type TableDataPreviewRequest struct { 27 type TableDataPreviewRequest struct {
17 Token string `header:"x-mmm-accesstoken"` 28 Token string `header:"x-mmm-accesstoken"`
18 ObjectId int64 `json:"objectId"` //表ID 29 ObjectId int64 `json:"objectId"` //表ID
@@ -73,3 +84,17 @@ type TableDataGrid struct { @@ -73,3 +84,17 @@ type TableDataGrid struct {
73 //总记录数 84 //总记录数
74 Total int64 `json:"total"` 85 Total int64 `json:"total"`
75 } 86 }
  87 +
  88 +func (t TablePreviewResponse) FirstCellData() string {
  89 + var column string
  90 + if len(t.Fields) >= 1 {
  91 + column = t.Fields[0].SQLName
  92 + }
  93 + if len(t.Fields) >= 2 && t.Fields[0].SQLName == "id" {
  94 + column = t.Fields[1].SQLName
  95 + }
  96 + if len(t.Grid.List) > 0 {
  97 + return t.Grid.List[0][column]
  98 + }
  99 + return ""
  100 +}
@@ -71,6 +71,34 @@ @@ -71,6 +71,34 @@
71 ] 71 ]
72 } 72 }
73 }, 73 },
  74 + "v1/chart/load-data": {
  75 + "post": {
  76 + "summary": "加载图表数据",
  77 + "operationId": "loadChartData",
  78 + "responses": {
  79 + "200": {
  80 + "description": "A successful response.",
  81 + "schema": {
  82 + "$ref": "#/definitions/LoadChartDataResponse"
  83 + }
  84 + }
  85 + },
  86 + "parameters": [
  87 + {
  88 + "name": "body",
  89 + "in": "body",
  90 + "required": true,
  91 + "schema": {
  92 + "$ref": "#/definitions/LoadChartDataRequest"
  93 + }
  94 + }
  95 + ],
  96 + "requestBody": {},
  97 + "tags": [
  98 + "chart"
  99 + ]
  100 + }
  101 + },
74 "v1/chart/move": { 102 "v1/chart/move": {
75 "post": { 103 "post": {
76 "summary": "移动图表", 104 "summary": "移动图表",
@@ -347,6 +375,12 @@ @@ -347,6 +375,12 @@
347 "in": "path", 375 "in": "path",
348 "required": true, 376 "required": true,
349 "type": "string" 377 "type": "string"
  378 + },
  379 + {
  380 + "name": "x-mmm-accesstoken",
  381 + "in": "query",
  382 + "required": false,
  383 + "type": "string"
350 } 384 }
351 ], 385 ],
352 "requestBody": {}, 386 "requestBody": {},
@@ -386,10 +420,7 @@ @@ -386,10 +420,7 @@
386 "type": "string" 420 "type": "string"
387 } 421 }
388 }, 422 },
389 - "title": "ChartComponentSearchRequest",  
390 - "required": [  
391 - "name"  
392 - ] 423 + "title": "ChartComponentSearchRequest"
393 }, 424 },
394 "ChartComponentSearchResponse": { 425 "ChartComponentSearchResponse": {
395 "type": "object", 426 "type": "object",
@@ -479,14 +510,15 @@ @@ -479,14 +510,15 @@
479 "type": "string", 510 "type": "string",
480 "description": " 名称" 511 "description": " 名称"
481 }, 512 },
482 - "charts": {  
483 - "type": "array",  
484 - "items": {  
485 - "$ref": "#/definitions/ChartItem"  
486 - } 513 + "property": {
  514 + "$ref": "#/definitions/ChartProperty",
  515 + "description": "属性"
487 } 516 }
488 }, 517 },
489 - "title": "ChartItem" 518 + "title": "ChartItem",
  519 + "required": [
  520 + "property"
  521 + ]
490 }, 522 },
491 "ChartProperty": { 523 "ChartProperty": {
492 "type": "object", 524 "type": "object",
@@ -705,10 +737,26 @@ @@ -705,10 +737,26 @@
705 "order" 737 "order"
706 ] 738 ]
707 }, 739 },
  740 + "Dimension": {
  741 + "type": "object",
  742 + "properties": {
  743 + "name": {
  744 + "type": "string"
  745 + },
  746 + "value": {
  747 + "type": "string"
  748 + }
  749 + },
  750 + "title": "Dimension",
  751 + "required": [
  752 + "name",
  753 + "value"
  754 + ]
  755 + },
708 "Expression": { 756 "Expression": {
709 "type": "object", 757 "type": "object",
710 "properties": { 758 "properties": {
711 - "Operator": { 759 + "operator": {
712 "type": "string", 760 "type": "string",
713 "description": " 操作符号 \u003c,\u003e,==,\u003c\u003e,\u003c=,\u003e=" 761 "description": " 操作符号 \u003c,\u003e,==,\u003c\u003e,\u003c=,\u003e="
714 }, 762 },
@@ -723,7 +771,7 @@ @@ -723,7 +771,7 @@
723 }, 771 },
724 "title": "Expression", 772 "title": "Expression",
725 "required": [ 773 "required": [
726 - "Operator", 774 + "operator",
727 "compareValue", 775 "compareValue",
728 "toValue" 776 "toValue"
729 ] 777 ]
@@ -731,6 +779,9 @@ @@ -731,6 +779,9 @@
731 "GetTableDetailRequest": { 779 "GetTableDetailRequest": {
732 "type": "object", 780 "type": "object",
733 "properties": { 781 "properties": {
  782 + "Token": {
  783 + "type": "string"
  784 + },
734 "tableId": { 785 "tableId": {
735 "type": "integer", 786 "type": "integer",
736 "format": "int32", 787 "format": "int32",
@@ -746,8 +797,30 @@ @@ -746,8 +797,30 @@
746 "type": "object", 797 "type": "object",
747 "title": "GetTableDetailResponse" 798 "title": "GetTableDetailResponse"
748 }, 799 },
  800 + "LoadChartDataRequest": {
  801 + "type": "object",
  802 + "properties": {
  803 + "chartId": {
  804 + "type": "integer",
  805 + "format": "int64"
  806 + }
  807 + },
  808 + "title": "LoadChartDataRequest",
  809 + "required": [
  810 + "chartId"
  811 + ]
  812 + },
  813 + "LoadChartDataResponse": {
  814 + "type": "object",
  815 + "title": "LoadChartDataResponse"
  816 + },
749 "SearchTableByModuleRequest": { 817 "SearchTableByModuleRequest": {
750 "type": "object", 818 "type": "object",
  819 + "properties": {
  820 + "Token": {
  821 + "type": "string"
  822 + }
  823 + },
751 "title": "SearchTableByModuleRequest" 824 "title": "SearchTableByModuleRequest"
752 }, 825 },
753 "SearchTableByModuleResponse": { 826 "SearchTableByModuleResponse": {
@@ -757,6 +830,9 @@ @@ -757,6 +830,9 @@
757 "SearchTableDataRequest": { 830 "SearchTableDataRequest": {
758 "type": "object", 831 "type": "object",
759 "properties": { 832 "properties": {
  833 + "Token": {
  834 + "type": "string"
  835 + },
760 "objectId": { 836 "objectId": {
761 "type": "integer", 837 "type": "integer",
762 "format": "int32", 838 "format": "int32",
@@ -792,6 +868,9 @@ @@ -792,6 +868,9 @@
792 "SearchTableFieldOptionalValuesRequest": { 868 "SearchTableFieldOptionalValuesRequest": {
793 "type": "object", 869 "type": "object",
794 "properties": { 870 "properties": {
  871 + "Token": {
  872 + "type": "string"
  873 + },
795 "objectId": { 874 "objectId": {
796 "type": "integer", 875 "type": "integer",
797 "format": "int32", 876 "format": "int32",
@@ -872,6 +951,8 @@ @@ -872,6 +951,8 @@
872 "required": [ 951 "required": [
873 "name", 952 "name",
874 "from", 953 "from",
  954 + "dataSourceId",
  955 + "customText",
875 "matchExpressions" 956 "matchExpressions"
876 ] 957 ]
877 }, 958 },
@@ -886,7 +967,7 @@ @@ -886,7 +967,7 @@
886 "dimensionList": { 967 "dimensionList": {
887 "type": "array", 968 "type": "array",
888 "items": { 969 "items": {
889 - "type": "string" 970 + "$ref": "#/definitions/Dimension"
890 }, 971 },
891 "description": " 维度列表" 972 "description": " 维度列表"
892 } 973 }
@@ -22,6 +22,9 @@ service Core { @@ -22,6 +22,9 @@ service Core {
22 @doc "保存图表" 22 @doc "保存图表"
23 @handler saveChart 23 @handler saveChart
24 post /chart (ChartSaveRequest) returns (ChartSaveResponse) 24 post /chart (ChartSaveRequest) returns (ChartSaveResponse)
  25 + @doc "图表另存为"
  26 + @handler saveAsChart
  27 + post /chart/saveas (ChartSaveAsRequest) returns (ChartSaveAsResponse)
25 @doc "删除图表" 28 @doc "删除图表"
26 @handler deleteChart 29 @handler deleteChart
27 delete /chart/:id (ChartDeleteRequest) returns (ChartDeleteResponse) 30 delete /chart/:id (ChartDeleteRequest) returns (ChartDeleteResponse)
@@ -43,6 +46,18 @@ service Core { @@ -43,6 +46,18 @@ service Core {
43 post /chart/components/search(ChartComponentSearchRequest)returns(ChartComponentSearchResponse) 46 post /chart/components/search(ChartComponentSearchRequest)returns(ChartComponentSearchResponse)
44 } 47 }
45 48
  49 +@server(
  50 + prefix: v1
  51 + group: chart
  52 + //jwt: JwtAuth
  53 + //middleware: Authority
  54 +)
  55 +service Core {
  56 + @doc "加载图表数据"
  57 + @handler loadChartData
  58 + post /chart/load-data (LoadChartDataRequest) returns (LoadChartDataResponse)
  59 +}
  60 +
46 type ( 61 type (
47 ChartGetRequest { 62 ChartGetRequest {
48 Id int64 `path:"id"` 63 Id int64 `path:"id"`
@@ -62,6 +77,14 @@ type ( @@ -62,6 +77,14 @@ type (
62 Chart ChartItem `json:"chart"` 77 Chart ChartItem `json:"chart"`
63 } 78 }
64 79
  80 + ChartSaveAsRequest struct{
  81 + ChartId int64 `json:"chartId"` // 图表Id
  82 + Name string `json:"name"`// 名称
  83 + }
  84 + ChartSaveAsResponse struct{
  85 + Chart ChartItem `json:"chart"`
  86 + }
  87 +
65 ChartDeleteRequest struct{ 88 ChartDeleteRequest struct{
66 Id int64 `path:"id"` 89 Id int64 `path:"id"`
67 } 90 }
@@ -102,6 +125,12 @@ type ( @@ -102,6 +125,12 @@ type (
102 //Charts []ChartItem `json:"charts,optional"` 125 //Charts []ChartItem `json:"charts,optional"`
103 ChartProperty *ChartProperty `json:"property,optional,omitempty"` //属性 126 ChartProperty *ChartProperty `json:"property,optional,omitempty"` //属性
104 } 127 }
  128 + LoadChartDataRequest struct{
  129 + ChartId int64 `json:"chartId"`
  130 + }
  131 + LoadChartDataResponse struct{
  132 +
  133 + }
105 ) 134 )
106 135
107 type( 136 type(
@@ -146,7 +175,7 @@ type( @@ -146,7 +175,7 @@ type(
146 SourceFrom string `json:"from,options=[ByteBank,User]"` // 数据源类型 ByteBank:字库 User:用户自定义 175 SourceFrom string `json:"from,options=[ByteBank,User]"` // 数据源类型 ByteBank:字库 User:用户自定义
147 DataSourceId int64 `json:"dataSourceId,optional,omitempty"` // 数据源ID(from值为ByteBank时有值) 176 DataSourceId int64 `json:"dataSourceId,optional,omitempty"` // 数据源ID(from值为ByteBank时有值)
148 CustomText string `json:"customText,optional,omitempty"` // 自定义数据文本(from值为User时有值) 177 CustomText string `json:"customText,optional,omitempty"` // 自定义数据文本(from值为User时有值)
149 - MatchExpressions []Expression `json:"matchExpressions,omitempty"` // 条件匹配表达式(总体指标) 178 + MatchExpressions []Expression `json:"matchExpressions,optional,omitempty"` // 条件匹配表达式(总体指标)
150 } 179 }
151 Expression struct { 180 Expression struct {
152 Operator string `json:"operator"` // 操作符号 <,>,==,<>,<=,>= 181 Operator string `json:"operator"` // 操作符号 <,>,==,<>,<=,>=
@@ -11,14 +11,10 @@ info( @@ -11,14 +11,10 @@ info(
11 @server( 11 @server(
12 prefix: v1 12 prefix: v1
13 group: table 13 group: table
14 - jwt: JwtAuth 14 + //jwt: JwtAuth
15 //middleware: Authority 15 //middleware: Authority
16 ) 16 )
17 service Core { 17 service Core {
18 - @doc "源数据表-按模块搜索"  
19 - @handler searchTableByModule  
20 - post /table/search-by-module (SearchTableByModuleRequest) returns (SearchTableByModuleResponse)  
21 -  
22 @doc "源数据表-字段可选值" 18 @doc "源数据表-字段可选值"
23 @handler searchTableFieldOptionalValues 19 @handler searchTableFieldOptionalValues
24 post /table/field-optional-values (SearchTableByModuleRequest) returns (SearchTableByModuleResponse) 20 post /table/field-optional-values (SearchTableByModuleRequest) returns (SearchTableByModuleResponse)
@@ -32,16 +28,28 @@ service Core { @@ -32,16 +28,28 @@ service Core {
32 post /table/data (SearchTableDataRequest) returns (SearchTableDataResponse) 28 post /table/data (SearchTableDataRequest) returns (SearchTableDataResponse)
33 } 29 }
34 30
  31 +@server(
  32 + prefix: v1
  33 + group: table
  34 + jwt: JwtAuth
  35 + //middleware: Authority
  36 +)
  37 +service Core {
  38 + @doc "源数据表-按模块搜索"
  39 + @handler searchTableByModule
  40 + post /table/search-by-module (SearchTableByModuleRequest) returns (SearchTableByModuleResponse)
  41 +}
  42 +
35 type ( 43 type (
36 SearchTableByModuleRequest struct{ 44 SearchTableByModuleRequest struct{
37 - Token string `header:"x-mmm-accesstoken"` 45 + Token string `header:"x-mmm-accesstoken,optional"`
38 } 46 }
39 SearchTableByModuleResponse struct{ 47 SearchTableByModuleResponse struct{
40 48
41 } 49 }
42 50
43 SearchTableFieldOptionalValuesRequest struct{ 51 SearchTableFieldOptionalValuesRequest struct{
44 - Token string `header:"x-mmm-accesstoken"` 52 + Token string `header:"x-mmm-accesstoken,optional"`
45 ObjectId int `json:"objectId"` // 对象ID 53 ObjectId int `json:"objectId"` // 对象ID
46 Field string `json:"field"` // 当前选择的字段 54 Field string `json:"field"` // 当前选择的字段
47 SqlName string `json:"sqlName"` // 字段SqlName 55 SqlName string `json:"sqlName"` // 字段SqlName
@@ -63,7 +71,7 @@ type ( @@ -63,7 +71,7 @@ type (
63 } 71 }
64 72
65 GetTableDetailRequest struct { 73 GetTableDetailRequest struct {
66 - Token string `header:"x-mmm-accesstoken"` 74 + Token string `header:"x-mmm-accesstoken,optional"`
67 TableId int `path:"tableId"` // 表ID 75 TableId int `path:"tableId"` // 表ID
68 } 76 }
69 GetTableDetailResponse struct{ 77 GetTableDetailResponse struct{
@@ -71,7 +79,7 @@ type ( @@ -71,7 +79,7 @@ type (
71 } 79 }
72 80
73 SearchTableDataRequest struct{ 81 SearchTableDataRequest struct{
74 - Token string `header:"x-mmm-accesstoken"` 82 + Token string `header:"x-mmm-accesstoken,optional"`
75 ObjectId int `json:"objectId"` // 对象ID 83 ObjectId int `json:"objectId"` // 对象ID
76 PageNumber int `json:"page,optional"` // 分页数 84 PageNumber int `json:"page,optional"` // 分页数
77 PageSize int `json:"size,optional"` // 页码 85 PageSize int `json:"size,optional"` // 页码
@@ -37,6 +37,7 @@ require ( @@ -37,6 +37,7 @@ require (
37 github.com/garyburd/redigo v1.6.3 // indirect 37 github.com/garyburd/redigo v1.6.3 // indirect
38 github.com/gin-contrib/sse v0.1.0 // indirect 38 github.com/gin-contrib/sse v0.1.0 // indirect
39 github.com/gin-gonic/gin v1.5.0 // indirect 39 github.com/gin-gonic/gin v1.5.0 // indirect
  40 + github.com/go-gota/gota v0.12.0 // indirect
40 github.com/go-logr/logr v1.2.3 // indirect 41 github.com/go-logr/logr v1.2.3 // indirect
41 github.com/go-logr/stdr v1.2.2 // indirect 42 github.com/go-logr/stdr v1.2.2 // indirect
42 github.com/go-openapi/jsonpointer v0.19.6 // indirect 43 github.com/go-openapi/jsonpointer v0.19.6 // indirect