作者 yangfu

chore: some optimize

# fix #3549 #3592
# add whitelist auth
@@ -21,3 +21,4 @@ STARROCKS_PORT = 9030 @@ -21,3 +21,4 @@ STARROCKS_PORT = 9030
21 21
22 BLACK_LIST_USER = 0 22 BLACK_LIST_USER = 0
23 BLACK_LIST_COMPANY = 1612991734952759296 23 BLACK_LIST_COMPANY = 1612991734952759296
  24 +WHITE_LIST_USERS = 22,23
@@ -18,4 +18,7 @@ CREATE INDEX IF NOT EXISTS idx_logs_company_id_operator_name ON metadata.logs US @@ -18,4 +18,7 @@ CREATE INDEX IF NOT EXISTS idx_logs_company_id_operator_name ON metadata.logs US
18 CREATE INDEX IF NOT EXISTS idx_logs_company_id_created_at ON metadata.logs USING btree((context->>'companyId'),created_at); 18 CREATE INDEX IF NOT EXISTS idx_logs_company_id_created_at ON metadata.logs USING btree((context->>'companyId'),created_at);
19 19
20 /*mapping_rules*/ 20 /*mapping_rules*/
21 -CREATE INDEX IF NOT EXISTS idx_mapping_rules_company_id_table_id_file_id ON metadata.mapping_rules USING btree((context->>'companyId'),table_id,file_id);  
  21 +CREATE INDEX IF NOT EXISTS idx_mapping_rules_company_id_table_id_file_id ON metadata.mapping_rules USING btree((context->>'companyId'),table_id,file_id);
  22 +
  23 +/*query_sets*/
  24 +CREATE INDEX IF NOT EXISTS idx_query_sets_company_id_type_deleted_at ON metadata.query_sets USING btree((context->>'companyId'),type,deleted_at);
@@ -117,6 +117,8 @@ spec: @@ -117,6 +117,8 @@ spec:
117 value: "1" 117 value: "1"
118 - name: BLACK_LIST_COMPANY 118 - name: BLACK_LIST_COMPANY
119 value: "1646025721363042304" 119 value: "1646025721363042304"
  120 + - name: WHITE_LIST_USERS
  121 + value: "0"
120 volumes: 122 volumes:
121 - name: accesslogs 123 - name: accesslogs
122 emptyDir: {} 124 emptyDir: {}
@@ -426,6 +426,15 @@ func (tableService *TableService) ApplyOn(ctx *domain.Context, cmd *command.Appl @@ -426,6 +426,15 @@ func (tableService *TableService) ApplyOn(ctx *domain.Context, cmd *command.Appl
426 } else { 426 } else {
427 table.TableInfo.SetApplyOn(cmd.Module) 427 table.TableInfo.SetApplyOn(cmd.Module)
428 } 428 }
  429 + if !ctx.Access() {
  430 + for i := range cmd.Modules {
  431 + // 字库应用于数控目前限制在3万条记录以内,在字库的应用于数控按钮上增加判断,如果超过3万条,
  432 + // 错误提示:普通用户数据限制在3万条记录,如需使用更多数据,请联系管理员,需针对公司配置应用记录上限数值,未配置默认3万条
  433 + if cmd.Modules[i] == domain.ModuleDigitalCenter && table.RowCount > 30000 {
  434 + return nil, factory.FastError(fmt.Errorf("普通用户数据限制在3万条记录,如需使用更多数据,请联系管理员,需针对公司配置应用记录上限数值,未配置默认3万条"))
  435 + }
  436 + }
  437 + }
429 438
430 table, err = tableRepository.Save(table) 439 table, err = tableRepository.Save(table)
431 if err != nil { 440 if err != nil {
@@ -2,6 +2,8 @@ package constant @@ -2,6 +2,8 @@ package constant
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "strconv"
  6 + "strings"
5 ) 7 )
6 8
7 var SERVICE_NAME = "character-library-metadata-bastion" 9 var SERVICE_NAME = "character-library-metadata-bastion"
@@ -26,8 +28,9 @@ var BYTE_CORE_HOST = "http://192.168.100.34:8303" @@ -26,8 +28,9 @@ var BYTE_CORE_HOST = "http://192.168.100.34:8303"
26 28
27 var AUTH_SERVER_HOST = "http://digital-platform-dev.fjmaimaimai.com" 29 var AUTH_SERVER_HOST = "http://digital-platform-dev.fjmaimaimai.com"
28 30
29 -var BlacklistUser int64  
30 -var BlackListCompany int64 31 +var BLACK_LIST_USER int64
  32 +var BLACK_LIST_COMPANY int64
  33 +var WHITE_LIST_USERS []int
31 34
32 //var CUSTOMER_ACCOUNT = []int64{3129687560814592, 3129687690100739, 3492238958608384} 35 //var CUSTOMER_ACCOUNT = []int64{3129687560814592, 3129687690100739, 3492238958608384}
33 36
@@ -53,6 +56,12 @@ func init() { @@ -53,6 +56,12 @@ func init() {
53 PPROF_ON = Configurator.DefaultBool("PPROF_ON", PPROF_ON) 56 PPROF_ON = Configurator.DefaultBool("PPROF_ON", PPROF_ON)
54 CACHE_PREFIX = SERVICE_NAME + ":" + SERVICE_ENV 57 CACHE_PREFIX = SERVICE_NAME + ":" + SERVICE_ENV
55 58
56 - BlacklistUser = Configurator.DefaultInt64("BLACK_LIST_USER", BlacklistUser)  
57 - BlackListCompany = Configurator.DefaultInt64("BLACK_LIST_COMPANY", BlackListCompany) 59 + BLACK_LIST_USER = Configurator.DefaultInt64("BLACK_LIST_USER", BLACK_LIST_USER)
  60 + BLACK_LIST_COMPANY = Configurator.DefaultInt64("BLACK_LIST_COMPANY", BLACK_LIST_COMPANY)
  61 +
  62 + whiteListUsers := strings.Split(Configurator.DefaultString("WHITE_LIST_USERS", ""), ",")
  63 + for _, userId := range whiteListUsers {
  64 + v, _ := strconv.Atoi(userId)
  65 + WHITE_LIST_USERS = append(WHITE_LIST_USERS, v)
  66 + }
58 } 67 }
1 package domain 1 package domain
2 2
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
  4 +
3 type Context struct { 5 type Context struct {
4 // 公司 6 // 公司
5 CompanyId int `json:"companyId"` 7 CompanyId int `json:"companyId"`
@@ -38,3 +40,12 @@ const ( @@ -38,3 +40,12 @@ const (
38 ContextWithLogLevel = "WithLogLevel" 40 ContextWithLogLevel = "WithLogLevel"
39 ContextWithLogMsg = "WithLogMsg" 41 ContextWithLogMsg = "WithLogMsg"
40 ) 42 )
  43 +
  44 +func (c *Context) Access() bool {
  45 + for _, userId := range constant.WHITE_LIST_USERS {
  46 + if userId == c.OperatorId {
  47 + return true
  48 + }
  49 + }
  50 + return false
  51 +}
@@ -141,6 +141,11 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon @@ -141,6 +141,11 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon
141 if queryComponent.Formula != nil { 141 if queryComponent.Formula != nil {
142 formula := queryComponent.Formula 142 formula := queryComponent.Formula
143 res.DatabaseTableName = formula.TableFields[0].TableSqlName 143 res.DatabaseTableName = formula.TableFields[0].TableSqlName
  144 + // TODO:支持常量
  145 + //res.DatabaseTableName = ""
  146 + //if len(formula.TableFields) > 0 {
  147 + // res.DatabaseTableName = formula.TableFields[0].TableSqlName
  148 + //}
144 exprSql := formula.ExprSql 149 exprSql := formula.ExprSql
145 if queryComponent.Formula.MixTableModel() { 150 if queryComponent.Formula.MixTableModel() {
146 exprSql = queryComponent.Formula.Complete() 151 exprSql = queryComponent.Formula.Complete()
@@ -46,6 +46,17 @@ func (f *TemporaryFileInfo) SetFile(file *domain.File) *TemporaryFileInfo { @@ -46,6 +46,17 @@ func (f *TemporaryFileInfo) SetFile(file *domain.File) *TemporaryFileInfo {
46 } 46 }
47 47
48 func (f *TemporaryFileInfo) SetFields(fields []*domain.Field) *TemporaryFileInfo { 48 func (f *TemporaryFileInfo) SetFields(fields []*domain.Field) *TemporaryFileInfo {
  49 + // 保留原有字段的类型(底层拆分的时候类型会变掉,无法处理,此处做特殊处理)
  50 + for i := range fields {
  51 + for j := range f.Fields {
  52 + if f.Fields[j].Name == fields[i].Name {
  53 + if f.Fields[j].SQLType != fields[i].SQLType {
  54 + fields[i].SQLType = f.Fields[j].SQLType
  55 + break
  56 + }
  57 + }
  58 + }
  59 + }
49 f.Fields = fields 60 f.Fields = fields
50 return f 61 return f
51 } 62 }
@@ -6,12 +6,14 @@ import ( @@ -6,12 +6,14 @@ import (
6 "github.com/beego/beego/v2/server/web" 6 "github.com/beego/beego/v2/server/web"
7 "github.com/beego/beego/v2/server/web/context" 7 "github.com/beego/beego/v2/server/web/context"
8 "github.com/linmadan/egglib-go/web/beego/filters" 8 "github.com/linmadan/egglib-go/web/beego/filters"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
9 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/controllers" 11 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/controllers"
11 "net/http" 12 "net/http"
12 "os" 13 "os"
13 "strconv" 14 "strconv"
14 "strings" 15 "strings"
  16 + "time"
15 17
16 . "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log" 18 . "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
17 _ "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/routers" 19 _ "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/routers"
@@ -53,9 +55,13 @@ func init() { @@ -53,9 +55,13 @@ func init() {
53 55
54 web.InsertFilter("/*", web.BeforeRouter, filters.AllowCors()) 56 web.InsertFilter("/*", web.BeforeRouter, filters.AllowCors())
55 web.InsertFilter("/*", web.BeforeRouter, JwtFilter()) 57 web.InsertFilter("/*", web.BeforeRouter, JwtFilter())
  58 + web.InsertFilter("/*", web.BeforeRouter, RequestCostBefore())
56 web.InsertFilter("/*", web.BeforeExec, controllers.BlacklistFilter(controllers.BlacklistRouters)) 59 web.InsertFilter("/*", web.BeforeExec, controllers.BlacklistFilter(controllers.BlacklistRouters))
57 web.InsertFilter("/*", web.BeforeExec, CreateRequestLogFilter(true)) // filters.CreateRequstLogFilter(Logger) 60 web.InsertFilter("/*", web.BeforeExec, CreateRequestLogFilter(true)) // filters.CreateRequstLogFilter(Logger)
58 - web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false)) 61 + if constant.SERVICE_ENV == "dev" { //|| web.BConfig.RunMode =="test"
  62 + web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false))
  63 + }
  64 + web.InsertFilter("/*", web.AfterExec, RequestCostAfter(150), web.WithReturnOnOutput(false))
59 } 65 }
60 66
61 func CreateRequestLogFilter(console bool) func(ctx *context.Context) { 67 func CreateRequestLogFilter(console bool) func(ctx *context.Context) {
@@ -89,3 +95,23 @@ func JwtFilter() func(ctx *context.Context) { @@ -89,3 +95,23 @@ func JwtFilter() func(ctx *context.Context) {
89 } 95 }
90 } 96 }
91 } 97 }
  98 +func RequestCostBefore() func(ctx *context.Context) {
  99 + return func(ctx *context.Context) {
  100 + ctx.Input.SetData("cost-begin", time.Now().UnixMilli())
  101 + }
  102 +}
  103 +
  104 +func RequestCostAfter(maxCost int64) func(ctx *context.Context) {
  105 + return func(ctx *context.Context) {
  106 + t := ctx.Input.GetData("cost-begin")
  107 + if t != nil {
  108 + costBegin := t.(int64)
  109 + costEnd := time.Now().UnixMilli()
  110 + cost := costEnd - costBegin
  111 + if cost > 0 && maxCost > 0 && cost > maxCost {
  112 + msg := fmt.Sprintf("beego | %v | %v | 耗时:%v \n %v", ctx.Input.Method(), ctx.Input.URL(), time.Duration(cost)*time.Millisecond, string(ctx.Input.RequestBody))
  113 + logs.Warn(msg)
  114 + }
  115 + }
  116 + }
  117 +}
@@ -129,9 +129,9 @@ func BlacklistFilter(black map[string]bool) func(ctx *context.Context) { @@ -129,9 +129,9 @@ func BlacklistFilter(black map[string]bool) func(ctx *context.Context) {
129 if !ok { 129 if !ok {
130 return 130 return
131 } 131 }
132 - if userToken.UserId > 0 && userToken.UserId == constant.BlacklistUser { 132 + if userToken.UserId > 0 && userToken.UserId == constant.BLACK_LIST_USER {
133 goto CheckBlackList 133 goto CheckBlackList
134 - } else if userToken.CompanyId > 0 && userToken.UserId == 0 && userToken.CompanyId == constant.BlackListCompany { 134 + } else if userToken.CompanyId > 0 && userToken.UserId == 0 && userToken.CompanyId == constant.BLACK_LIST_COMPANY {
135 goto CheckBlackList 135 goto CheckBlackList
136 } else { 136 } else {
137 return 137 return